2017-07-26 43 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 

再次感謝,並會記住您的未來參考。