Powershell 输出为数组

Powershell output as array

你好,我有一个看起来像这样的 powershell 命令。

$result = Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information" | Out-String

然后像这样输出

Id                  : xxx69add-xxd1-xx43-xxba-xxcd2fac8790
Title               : Create customer tracking list
Description         : Creates list for tracking customer contact information
Content             : xxx
Version             : 0
IsSiteScriptPackage : False

我想做的是以某种方式从中获取 Id,但我不知道如何操作。我不知道如何将其输出为数组,因此我可以使用 $result[0] 或任何其他方式来提取它。

感谢您的帮助。

Out-String 把所有的输入都变成一个字符串,所以在这里用处不大。

将表达式括在括号中并使用 . 成员访问运算符引用 Id 参数:

$result = (Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information").Id

或者用 Select-Object 获取 Id 属性 值:

$result = Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information" |Select-Object -ExpandProperty Id

... 或使用 ForEach-Object 成员调用:

$result = Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information" |ForEach-Object Id