以前工作中经常需要对新入职和离职员工邮箱进行处理,如激活邮箱(分配License)和回收邮箱(回收License)。在没有写这套脚本之前,一直在Office365控制台操作,由于大部分都是批量处理,效率非常低,下面的脚本可以单一操作,也可以批量处理。
#region## New-License 分配Licensefunction New-License{ if($args.Count -eq 0){ Write-Warning 请提供正确的Email地址或文件路径 break } $maildomain='公司邮箱域名' $availableLicense=Get-MsolAccountSku|%{$_.ActiveUnits - $_.ConsumedUnits} if($args[1] -eq $null -and $availableLicense -eq 0){ Write-Warning License不足! break } $O365E3_OnlyExchange = New-MsolLicenseOptions -AccountSkuId syndication-account:ENTERPRISEPACK_NO_RMS -DisabledPlans SHAREPOINTWAC,SHAREPOINTENTERPRISE,MCOSTANDARD,OFFICESUBSCRIPTION if(Test-Path $args[0]){ $users=gc $args[0] if($users.Length -gt $availableLicense -and $args[1] -eq $null){ Write-Warning 待激活邮箱用户数大于可用Licnese数! break } if($users.Length -gt 0){ foreach($user in $users){ if($user -match " "){ $user=$user -replace " ","." } if($user -notmatch $maildomain){ $user=$user + $maildomain } if($args[1] -eq $null){ Set-MsolUser -UserPrincipalName $user -UsageLocation "CN" -ErrorAction SilentlyContinue Set-MsolUserLicense -UserPrincipalName $user -AddLicenses "syndication-account:ENTERPRISEPACK_NO_RMS" -LicenseOptions $O365E3_OnlyExchange -ErrorAction c Get-MsolUser -UserPrincipalName $user }else{ Set-CASMailbox -ActiveSyncEnabled $false -MAPIEnabled $true -Identity $user -ErrorAction SilentlyContinue Set-Mailbox -Identity $user -LitigationHoldEnabled $true -ErrorAction SilentlyContinue } } }else{ Write-Warning 数据格式不对 } }else{ if($args[0] -match " "){ $args[0]=$args[0] -replace " ","." } if($args[0] -notmatch $maildomain){ $args[0]=$args[0] + $maildomain } if($args[1] -eq $null){ Set-MsolUser -UserPrincipalName $args[0] -UsageLocation "CN" -ErrorAction SilentlyContinue Set-MsolUserLicense -UserPrincipalName $args[0] -AddLicenses "syndication-account:ENTERPRISEPACK_NO_RMS" -LicenseOptions $O365E3_OnlyExchange -ErrorAction c Get-MsolUser -UserPrincipalName $args[0] }else{ Set-CASMailbox -ActiveSyncEnabled $false -OWAEnabled $true -MAPIEnabled $true -Identity $args[0] -ErrorAction SilentlyContinue Set-Mailbox -Identity $args[0] -LitigationHoldEnabled $true -ErrorAction SilentlyContinue } }}#endregion#Example#New-License D:\PSTemplate\buildLicense.txt 1#New-License xxx@xxx.com 1#################################################################################region回收Licensefunction Remove-License{ if($args.Count -eq 0){ Write-Warning 请提供正确的Email地址或文件路径 break } $flag=$false $maildomain='公司邮箱域名' $recoveryBefore=Get-MsolAccountSku|%{$_.ActiveUnits - $_.ConsumedUnits} if($args[0] -match $maildomain){ Set-MsolUserLicense -UserPrincipalName $args[0] -RemoveLicenses syndication-account:ENTERPRISEPACK_NO_RMS -ErrorAction SilentlyContinue Set-CASMailbox -ActiveSyncEnabled $false -OWAEnabled $false -PopEnabled $false -MAPIEnabled $false -ImapEnabled $false -Identity $args[0] -ErrorAction SilentlyContinue Get-MsolUser -UserPrincipalName $args[0] $flag=$true } elseif(Test-Path $args[0]){ $Users=gc $args[0] if($Users.Length -eq 0){ break } ForEach($u in $Users){ if($u -match " "){ $u=$u -replace " ","." } if($u -notmatch $maildomain){ $u=$u+$maildomain } Set-MsolUserLicense -UserPrincipalName $u -RemoveLicenses syndication-account:ENTERPRISEPACK_NO_RMS -ErrorAction SilentlyContinue Set-CASMailbox -ActiveSyncEnabled $false -OWAEnabled $false -PopEnabled $false -MAPIEnabled $false -ImapEnabled $false -Identity $u -ErrorAction SilentlyContinue Get-MsolUser -UserPrincipalName $u } }else{ Write-Warning 参数不正确!处理单个用户请输入完整Email地址,批处理输入txt文件路径。 } $recoveryAfter=Get-MsolAccountSku|%{$_.ActiveUnits - $_.ConsumedUnits} $psobject=New-Object psobject $psobject|Add-Member -MemberType NoteProperty -Name 回收前 -Value $recoveryBefore $psobject|Add-Member -MemberType NoteProperty -Name 回收后 -Value $recoveryAfter $psobject|Add-Member -MemberType NoteProperty -Name 实际回收 -Value ($recoveryAfter - $recoveryBefore) if($flag){ $psobject|Add-Member -MemberType NoteProperty -Name 实际用户数 -Value 1 }else{ $psobject|Add-Member -MemberType NoteProperty -Name 实际用户数 -Value $Users.Length } return $psobject|fl}#endregion#Example#Remove-License xxx@xxx.com#Remove-License D:\PSTemplate\removeLicense.txt