我如何 return 精确 属性 一个对象在 powershell (Azure)

How do I return one exact property of an object in powershell (Azure)

我正在尝试制作一组​​服务主体应用程序 ID,即显示名称,所有这些都以相同的前缀开头

示例:

Get-AzAdServicePrincipal -DisplayNameBeginswith "ecp-"

ServicePrincipalNames : {xxxx-75xx71-xxx-xxxx-xxx}
ApplicationId         : xxxxx-xxxx-xxxx-93x85-xxxxxxxxx
ObjectType            : ServicePrincipal
DisplayName           : ecp-test-1
Id                    : xxxxxx-xxx-xxxx-xxxx-xxxxxxxx
Type                  : 

我希望它只输出应用程序 ID 字段

Get-AzAdServicePrincipal -DisplayNameBeginswith "ecp-"

ApplicationId         : xxxxx-xxxx-xxxx-93x85-xxxxxxxxx

您可以使用 Select-Object 命令(通常缩写为 Select)来指定 return.

的属性

你的情况:

Get-AzAdServicePrincipal -DisplayNameBeginswith "ecp-" | Select ApplicationId

ApplicationId
-------------
xxxxx-xxxx-xxxx-93x85-xxxxxxxxx

您可以像这样将其提交给变量。

$myAzPrincipleAppId = Get-AzAdServicePrincipal -DisplayNameBeginswith "ecp-" | Select ApplicationId

这是一个 PowerShell 对象,有一个 属性、ApplicationId。通常,您只需要获取 属性 而没有 属性 名称,这称为解除引用。为此,只需使用此语法。

Write-host "Our AzPrincipleAppId is $($myAzPrincipleAppId.ApplicationId)"
>
Our AzPrincipleAppId is xxxxx-xxxx-xxxx-93x85-xxxxxxxxx

你也可以在任何地方传递它作为$myAzPrincipleAppId.ApplicationId