简体   繁体   English

如何将特定项目属性传递给 PowerShell 中的变量?

[英]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.

相关问题 如何将数组项中的项传递给使用过滤器的powershell命令 - How to pass item in array item to powershell command that uses a filter 如何将 azure cli 变量传递给 Azure Devops 变量? - How can i pass an azure cli variable to Azure Devops variables? 如何将 map 变量传递给 Azure Devops 管道作业? - How can I pass map variable to Azure Devops pipeline job? 如何修改逻辑应用中变量的属性? - How can I modify a property of a variable in a logic app? 如何在2个U-SQL脚本之间传递变量 - How can I pass a variable between 2 U-SQL scripts 如何在 Azure Devops/VSTS 中的任务之间传递变量 - How can i pass a variable between tasks in Azure Devops/VSTS 如何在 Powershell 中在运行时设置、重置或覆盖 Azure Pipelines 中的变量? - How can I set, reset or over-write a variable in Azure Pipelines at runtime in Powershell? 如何通过 PowerShell 脚本正确设置变量,然后使用 Azure DevOps 在另一个脚本中使用它? - How can I properly set a variable through a PowerShell script and then consume it in another using Azure DevOps? 如何将放置在蔚蓝文件存储中的.csv文件的内容复制到powershell变量? - How can I copy the contents of a .csv file placed in a azure file storage, to a powershell variable? 如何使用 powershell 在另一个 yaml 中使用一个 yaml 的变量? - How can I use a variable of one yaml in another yaml using powershell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM