将设备对象添加到PowerShell Foreach中的Azure组

y53ybaqx  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(113)

我正在尝试运行一个简单的PowerShell命令let,该命令列出所有匹配名称条件的设备,并在下一步中将这些设备移动到选定的Azure组中。
我尝试过:

$result = Get-AzureADDevice -All $True -SearchString "LAP-BK" | ForEach-Object -Process {Add-AzureADGroupMember -ObjectId "25f94620-d850-4ec6-9476-050429d44926" -RefObjectId "$result.ObjectId"}

但是投掷失误。我还试着用

$result = Get-AzureADDevice -All $True -SearchString "LAP-BK" |Select-Object ObjectId
forEach ($item in $result)
{

Add-AzureADGroupMember -ObjectId "25f94620-d850-4ec6-9476-050429d44926" -RefObjectId "$item"
}
exit

错误多种多样,我收到的最后一个错误是:

Error occurred while executing AddGroupMember Code: Request_BadRequest Message: Invalid object identifier ' @{ObjectId=debb95af-9h1f-49d6-ad84-8438f9c99b10}'. RequestId: 6df36830-7708-4f6b-b836-cd065f5f60b1
qltillow

qltillow1#

  • 我试图通过逐行运行您的脚本从我这一端找出错误,我能够准确地找到它发生的位置:*
  • 参考Jamesyumnam article,我成功创建并运行了以下脚本。*
$groupName = "<groupName>"
Connect-AzureAD
$groupObject = Get-AzureADGroup -SearchString $groupName
$outcome = Get-AzureADDevice -SearchString "xxxxxx" | select-object objectID
try{
foreach ($item in $outcome)
{
 $deviceObj = Get-AzureADDevice -SearchString $item.DeviceName
 Add-AzureADGroupMember -ObjectId $groupObject.ObjectId -RefObjectId $item.ObjectId
 }
 }
 catch {
   Write-Host "Device not existed"
}
  • 在Azure PowerShell中执行:*

  • Azure门户新增成员:*

  • 注意*:如果您的代码中包含try{} catch{}块来查找此类拦截器,会更有帮助。

相关问题