[英]How can I pass a specific item property to a variable in PowerShell?
I'm currently working on a script to manage Azure AD (as opposed to the console GUI) and am having issues at one particular part.我目前正在编写一个脚本来管理 Azure AD(而不是控制台 GUI),并且在某个特定部分遇到问题。 I'm trying to do an 'add user to group' module, but with Add-AzureADGroupMember requiring the group ObjectId and not display name, it's not initially user-friendly.
我正在尝试执行“将用户添加到组”模块,但是 Add-AzureADGroupMember 需要组 ObjectId 而不是显示名称,它最初对用户不友好。
Here's what I tried initially:这是我最初尝试的:
>> $UPN = "someuser@domain.com"
>> $Selected = "Group Display Name"
>> $Group = Get-AzureADGroup -Filter "DisplayName eq '$Selected'" -All $true | Select-Object -Property ObjectID
>> Add-AzureADGroupMember -ObjectID $Group -RefObjectID $UPN
The problem I have with this, is that $Group is returning '@{ObjectId=fba435cc-913c-46a0-9932-17c01733e143}'
as opposed to '{fba435cc-913c-46a0-9932-17c01733e143}'
我
'@{ObjectId=fba435cc-913c-46a0-9932-17c01733e143}'
的问题是 $Group 返回'@{ObjectId=fba435cc-913c-46a0-9932-17c01733e143}'
而不是'{fba435cc-913c-46a0-9932-17c01733e143}'
Is there a better way I can pass the group's ObjectID to a variable?有没有更好的方法可以将组的 ObjectID 传递给变量? I'd like for users to be able to select the display name and have the variable return the objectID.
我希望用户能够选择显示名称并让变量返回 objectID。
要仅获取属性的值,请使用ForEach-Object -MemberName
而不是Select-Object -Property
:
$Group = Get-AzureADGroup -Filter "DisplayName eq '$Selected'" -All $true | ForEach-Object -MemberName ObjectID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.