设为首页 收藏本站
查看: 2534|回复: 0

[经验分享] Office365 之分配、回收License工具

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-30 08:39:10 | 显示全部楼层 |阅读模式
以前工作中经常需要对新入职和离职员工邮箱进行处理,如激活邮箱(分配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[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







运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307424-1-1.html 上篇帖子: Office 365 在邮件组中查询用户是否存在 下篇帖子: 大量删除contact导致Office365同步失败 回收
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表