Powershell为用户批量分配特定许可证

oxcyiej7  于 2023-11-18  发布在  Shell
关注(0)|答案(1)|浏览(163)

我有这个脚本分配特定的许可证给用户的skuid.现在我想改变能够做同样的任务,但从csv文件的用户列表.我似乎不能弄清楚,在哪里添加foreach循环.将感谢任何帮助/提示.谢谢.

$UPN = "[email protected]"
$User = Get-AzureADUser -ObjectId $UPN
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$License.SkuId = "3b555118-da6a-4418-894f-7df1e2096870"
$LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$LicensesToAssign.RemoveLicenses = $License.SkuId
Set-AzureADUserLicense -ObjectId $User.ObjectId -AssignedLicenses $LicensesToAssign

字符串

m1m5dgzv

m1m5dgzv1#

当你有一个代码块,它只为一个用户执行任务,那么你会想把 * 整个东西 * 放在你的循环体中,然后确保在每次循环迭代开始时将当前用户的UPN分配给$UPN变量:

# read list of user records from disk
$users = Import-Csv path\to\users.csv

# iterate over list 
foreach($user in $users) {
  # grab upn value from the CSV record instead of using a hard-coded value
  $UPN = $user.UPN

  # everything else remains the same
  $User = Get-AzureADUser -ObjectId $UPN
  $License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
  $License.SkuId = "3b555118-da6a-4418-894f-7df1e2096870"
  $LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
  $LicensesToAssign.RemoveLicenses = $License.SkuId
  Set-AzureADUserLicense -ObjectId $User.ObjectId -AssignedLicenses $LicensesToAssign
}

字符串
以上假设CSV文件包含一个包含UPN值的UPN

相关问题