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[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
|