2017-07-26 84 views
0

我需要通过PowerShell配置O365许可证,我没有任何问题,但希望在此之前添加if语句以检查产品在修改之前是否已启用,否则将失败。检查是否启用了O365产品许可证

目前,我有如下修改(启用所有服务)已经启用的Office 365企业E3产品:

$O365EntE3 = New-MsolLicenseOptions -AccountSkuId tenent:ENTERPRISEPACK 

Set-MsolUserLicense -UserPrincipalName $Upn -LicenseOptions $O365EntE3 

什么会我需要运行检查是否tenent:ENTERPRISEPACK首次启用?

干杯 杰米

回答

0

要检查企业组是否是存在的,你可以使用下面的代码片段(在PowerShell中V5测试):

$user = Get-MsolUser -UserPrincipalName $upn 
if($user.Licenses.AccountSku.SkuPartNumber -contains 'Enterprisepack'){ 
    write-output "Enterprise Pack is there" 
} 
else{ 
    write-output "No Enterprise Pack" 
} 
+0

感谢您的支持。看起来它会完成这项工作,但自发布以来,我对try-catch块进行了一点实验,并设法根据需要进行工作。我只是进一步测试,并会在代码完成后发布。 – jshizzle

0

好了,下面做什么,我想也是。因为即使服务已经启用,-LicenseOptions参数也不会终止并且不会产生错误,这意味着我可以在使用-AddLicenses参数的初始脚本块之后运行该参数。同样用这种方法,我只在一个地方配置服务,尽管在上面的if-else语句中不难实现。

  Try 
     { 
      Set-MsolUserLicense -UserPrincipalName $Upn -AddLicenses tenent:ENTERPRISEPACK -ErrorAction Stop 
      "$(Get-Date -f HH:mm:ss): $($Upn): Office 365 Enterprise E3 Product License enabled" | Tee-Object $UserMigrationLog -Append 
     } 
     Catch 
     { 
      "$(Get-Date -f HH:mm:ss): $($Upn): Office 365 Enterprise E3 Product License already assigned" | Tee-Object $UserMigrationLog -Append 
     } 
     #Present so specific configurations can be set if required 
     Set-MsolUserLicense -UserPrincipalName $Upn -LicenseOptions $O365EntE3 
     "$(Get-Date -f HH:mm:ss): $($Upn): Office 365 Enterprise E3 configured services enabled" | Tee-Object $UserMigrationLog -Append 

再次感谢,并会记住您的未来参考。