LoginSignup
8
15

More than 3 years have passed since last update.

Office 365のライセンス関連のPowerShellコマンドまとめ

Last updated at Posted at 2019-09-28

はじめに

自分的によく使うOffice 365のライセンス関連のPowerShellコマンドまとめ。

コマンド

テナント上で利用可能なライセンスを確認

Get-MsolAccountSku

実行結果のAccountSkuId:の左の値はライセンス購入方法により異なる。
直接Microsoftから購入した場合はテナント名、販売店を経由して購入した場合はreseller-accountとなる。

個別のライセンスに含まれるServicePlanを確認

(Get-MsolAccountSku | where {$_.AccountSkuId -eq "<AccountSkuId>"}).ServiceStatus

個別のユーザーのUsageLocationを設定

ユーザーごとにライセンスを付与する前に必要な設定。忘れがち。

Set-MsolUser -UserPrincipalName "<Account>" -UsageLocation "<CountryCode>"

個別のユーザーのライセンス付与状態を確認

Get-MsolUser -UserPrincipalName "<Account>" | Format-List Licenses

個別のユーザーのライセンス付与、削除

Set-MsolUserLicense -UserPrincipalName "<Account>" `
    -RemoveLicenses "<削除するライセンスのAccountSkuId1>","<削除するライセンスのAccountSkuId2>" `
    -AddLicenses "<付与するライセンスのAccountSkuId1>","<付与するライセンスのAccountSkuId2>"

個別のユーザーに付与されたライセンスのServicePlanごとの有効・無効を確認

(Get-MsolUser -UserPrincipalName "<Account>").Licenses.ServiceStatus

個別のユーザーにServicePlanごとの有効・無効を指定してライセンス付与

無効にするServicePlanを指定して付与

$DisabledPlans = New-Object System.Collections.Generic.List[string]
$DisabledPlans.Add("<無効にするServicePlan1>")
$DisabledPlans.Add("<無効にするServicePlan2>")
・・・
$AccountSkuId = "<一部ServicePlanを無効にして付与するライセンスのAccountSkuId>"
$LicenseOption = New-MsolLicenseOptions -AccountSkuId $AccountSkuId -DisabledPlans $DisabledPlans
Set-MsolUserLicense -UserPrincipalName "<Account>" -AddLicenses $AccountSkuId -LicenseOptions $LicenseOption

有効にするServicePlanを指定して付与

$AccountSkuId = "<一部ServicePlanのみ有効にして付与するライセンスのAccountSkuId>"
$AccountSku = Get-MsolAccountSku | where {$_.AccountSkuId -eq $AccountSkuId}
$ServiceName = ($AccountSku | Select-Object -ExpandProperty ServiceStatus | Select-Object -ExpandProperty ServicePlan).ServiceName
$ServiceName = $ServiceName | Select-String -Pattern "<有効にするServicePlan1>" -NotMatch
$ServiceName = $ServiceName | Select-String -Pattern "<有効にするServicePlan2>" -NotMatch
・・・
$LicenseOption = New-MsolLicenseOptions -AccountSkuId $AccountSkuId -DisabledPlans $ServiceName
Set-MsolUserLicense -UserPrincipalName "<Account>" -AddLicenses $AccountSkuId -LicenseOptions $LicenseOption

すべてのユーザーのライセンス付与状況確認

Get-MsolUser -All | Select-Object DisplayName, UserPrincipalName, @{L="Licenses"; E={($_ | Select-Object -ExpandProperty Licenses).AccountSkuId -join ";"}}

以前まで7行のコマンドを記載していたが、1行に短縮できた。

以前のコマンド
#すべてのユーザーの情報を格納するオブジェクト$Datasを定義
$Datas = New-Object System.Collections.ArrayList

#Get-MsolUserで取得したユーザーごとの情報をForEachにより処理
Get-MsolUser -All | ForEach-Object {

    #ユーザーごとの情報を格納するオブジェクト$Dataを定義し、プロパティ値を設定して$Datasに格納
    $Data = New-Object PSObject | Select-Object DisplayName, UserPrincipalName, Licenses
    $Data.DisplayName = $_.DisplayName
    $Data.UserPrincipalName = $_.UserPrincipalName
    $Data.Licenses = ($_ | Select-Object -ExpandProperty Licenses).AccountSkuId -join ";"
    [void]$Datas.Add($Data)

}

#$Datasの内容をCSV出力
$Datas | Export-CSV export.csv -Encoding UTF8 -NoTypeInformation

ユーザーごとに付与すべきライセンスが指定されたCSVをもとにライセンス付与

CSVデータ

  • UPN:ユーザーのUserPrincipalName属性
  • E1orE3:E1ライセンスとE3ライセンスのいずれを付与するべきか。
import.csv
UPN,E1orE3
liam@contoso.com,E1
noah@contoso.com,E3
emma@contoso.com,E1
olivia@contoso.com,E1
ava@contoso.com,E3

コマンド

#付与するライセンスのAccountSkuIdを変数に格納(P1はすべてのユーザーに付与)
$E1AccountSkuId = "hoge:STANDARDPACK"
$E3AccountSkuId = "hoge:ENTERPRISEPACK"
$P1AccountSkuId = "hoge:AAD_PREMIUM"

#CSVデータをインポート
$ImportedCSV = Import-Csv import.csv -Encoding Default

#すべてのユーザーの現在のライセンス付与状況を取得
$MsolUsers = Get-MsolUser -All | Select-Object UserPrincipalName, Licenses

#インポートしたCSVデータ上のユーザーごとの情報をForEachにより処理
$ImportedCSV | ForEach-Object {

    #現在付与されているライセンスを配列で定義
    $CsvUPN = $_.UPN
    $MsolUser = $MsolUsers | Where-Object {
        $_.UserPrincipalName -eq $CsvUPN
    }
    $License_ary =@()
    $License_ary += ($MsolUser | Select-Object -ExpandProperty Licenses).AccountSkuId

    #E1を付与するユーザーの場合
    if($_.E1orE3 -eq "E1"){

        #現在E3ライセンスが付与されていれば削除
        if($License_ary.Contains($E3AccountSkuId)){
            Set-MsolUserLicense -UserPrincipalName $_.UPN -RemoveLicenses $E3AccountSkuId;
        }

        #現在E1ライセンスが付与されていなければ付与
        if($License_ary.Contains($E1AccountSkuId)){
            ;
        }else{
            Set-MsolUserLicense -UserPrincipalName $_.UPN -AddLicenses $E1AccountSkuId;
        }

    }

    #E3を付与するユーザーの場合
    if($_.E1orE3 -eq "E3"){

        #現在E1ライセンスが付与されていれば削除
        if($License_ary.Contains($E1AccountSkuId)){
            Set-MsolUserLicense -UserPrincipalName $_.UPN -RemoveLicenses $E1AccountSkuId;
        }

        #現在E3ライセンスが付与されていなければ付与
        if($License_ary.Contains($E3AccountSkuId)){
            ;
        }else{
            Set-MsolUserLicense -UserPrincipalName $_.UPN -AddLicenses $E3AccountSkuId;
        }

    }

    #すべてのユーザーに対して、現在P1ライセンスが付与されていなければ付与
    if($License_ary.Contains($P1AccountSkuId)){
        ;
    }else{
        Set-MsolUserLicense -UserPrincipalName $_.UPN -AddLicenses $P1AccountSkuId;
    }

}

参考

8
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
15