32121111 发表于 2016-11-30 08:39:10

Office365 之分配、回收License工具

以前工作中经常需要对新入职和离职员工邮箱进行处理,如激活邮箱(分配License)和回收邮箱(回收License)。在没有写这套脚本之前,一直在Office365控制台操作,由于大部分都是批量处理,效率非常低,下面的脚本可以单一操作,也可以批量处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#region## New-License 分配License
function New-License{
    if($args.Count -eq 0){
      Write-Warning 请提供正确的Email地址或文件路径
      break
    }
    $maildomain='公司邮箱域名'
    $availableLicense=Get-MsolAccountSku|%{$_.ActiveUnits - $_.ConsumedUnits}
    if($args -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){
      $users=gc $args
      if($users.Length -gt $availableLicense -and $args -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 -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 -match " "){
            $args=$args -replace " ","."
      }
      if($args -notmatch $maildomain){
            $args=$args + $maildomain
      }
      if($args -eq $null){
            Set-MsolUser -UserPrincipalName $args -UsageLocation "CN" -ErrorAction SilentlyContinue
            Set-MsolUserLicense -UserPrincipalName $args -AddLicenses "syndication-account:ENTERPRISEPACK_NO_RMS" -LicenseOptions $O365E3_OnlyExchange -ErrorAction c
            Get-MsolUser -UserPrincipalName $args
      }else{
            Set-CASMailbox -ActiveSyncEnabled $false -OWAEnabled $true-MAPIEnabled $true -Identity $args -ErrorAction SilentlyContinue
            Set-Mailbox -Identity $args -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 -match $maildomain){
      Set-MsolUserLicense -UserPrincipalName $args -RemoveLicenses syndication-account:ENTERPRISEPACK_NO_RMS -ErrorAction SilentlyContinue
      Set-CASMailbox -ActiveSyncEnabled $false -OWAEnabled $false -PopEnabled $false -MAPIEnabled $false -ImapEnabled $false -Identity $args -ErrorAction SilentlyContinue
      Get-MsolUser -UserPrincipalName $args
      $flag=$true
    }
    elseif(Test-Path $args){
      $Users=gc $args
      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







页: [1]
查看完整版本: Office365 之分配、回收License工具