之前工做中常常須要對新入職和離職員工郵箱進行處理,如激活郵箱(分配License)和回收郵箱(回收License)。在沒有寫這套腳本以前,一直在Office365控制檯操做,因爲大部分都是批量處理,效率很是低,下面的腳本能夠單一操做,也能夠批量處理。dom
#region## New-License 分配License function 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回收License function 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