解析 Powershell 变量
Parsing a Powershell variable
你们都帮了大忙——让我先这么说。
我从函数 I 运行 中得到以下输出 - 数据存储在一个名为 $response
的变量中,该变量是从 Invoke-RestMethod
调用中获得的。
@{ResourceType=UserStory; Id=202847; Name=Professional Lines: 2,800,000 Policy Aggregate Limit update}
我如何将这些值解析为新变量以便最终结果是
TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
您显示的是自定义对象的字符串化形式使用 哈希表-like 文本表示.
该自定义对象的定义如下所示:
# Construct a custom object with .ResourceType, .Id, and .Name properties.
$response = [pscustomobject] @{
ResourceType = 'UserStory'
Id = 202847
Name = 'Professional Lines: 2,800,000 Policy Aggregate Limit update'
}
如果将此变量包含在 可扩展 字符串(内插字符串) 中,您将在问题中得到表示[1] - 请注意,如果将对象传递给 Write-Host
cmdlet[,您将获得相同的表示形式2]:
PS> "$response" # Note the enclosing "..."
@{ResourceType=UserStory; Id=202847; Name=Professional Lines: 2,800,000 Policy Aggregate Limit update}
请注意,如果没有封闭的 "..."
,您将获得更好的表格表示形式,由 PowerShell 的输出格式化系统提供。
PS> $response # direct output of the object
ResourceType Id Name
------------ -- ----
UserStory 202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
假设 $response
实际上仍然是一个 自定义对象 ,而不是它的 字符串表示形式 ,您可以简单地访问自定义对象的属性来构建您的字符串,再次使用可扩展字符串:
PS> "TP Id:$($response.Id) $($response.Name)"
TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
注意需要在 属性 引用周围使用 $(...)
- 有关 PowerShell 字符串扩展规则的摘要,请参阅 this answer。
替代方法 是使用 -f
运算符,它使用格式化字符串,如 .NET String.Format()
方法:
PS> 'TP ID:{0} {1}' -f $response.Id, $response.Name
TP ID:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
[1] 您也可以通过在自定义对象上调用 .psobject.ToString()
来获得此表示;奇怪的是,从 PowerShell Core 6.1.0 开始,只是 .ToString()
不起作用; this GitHub issue.
中讨论了这一令人惊讶的差异
[2] 请注意 Write-Host
的目的是提供 for-display-only (to-host) 输出,这绕过 PowerShell 的成功输出流,从而能够捕获或重定向其输出 - 有关详细信息,请参阅 。
你们都帮了大忙——让我先这么说。
我从函数 I 运行 中得到以下输出 - 数据存储在一个名为 $response
的变量中,该变量是从 Invoke-RestMethod
调用中获得的。
@{ResourceType=UserStory; Id=202847; Name=Professional Lines: 2,800,000 Policy Aggregate Limit update}
我如何将这些值解析为新变量以便最终结果是
TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
您显示的是自定义对象的字符串化形式使用 哈希表-like 文本表示.
该自定义对象的定义如下所示:
# Construct a custom object with .ResourceType, .Id, and .Name properties.
$response = [pscustomobject] @{
ResourceType = 'UserStory'
Id = 202847
Name = 'Professional Lines: 2,800,000 Policy Aggregate Limit update'
}
如果将此变量包含在 可扩展 字符串(内插字符串) 中,您将在问题中得到表示[1] - 请注意,如果将对象传递给 Write-Host
cmdlet[,您将获得相同的表示形式2]:
PS> "$response" # Note the enclosing "..."
@{ResourceType=UserStory; Id=202847; Name=Professional Lines: 2,800,000 Policy Aggregate Limit update}
请注意,如果没有封闭的 "..."
,您将获得更好的表格表示形式,由 PowerShell 的输出格式化系统提供。
PS> $response # direct output of the object
ResourceType Id Name
------------ -- ----
UserStory 202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
假设 $response
实际上仍然是一个 自定义对象 ,而不是它的 字符串表示形式 ,您可以简单地访问自定义对象的属性来构建您的字符串,再次使用可扩展字符串:
PS> "TP Id:$($response.Id) $($response.Name)"
TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
注意需要在 属性 引用周围使用 $(...)
- 有关 PowerShell 字符串扩展规则的摘要,请参阅 this answer。
替代方法 是使用 -f
运算符,它使用格式化字符串,如 .NET String.Format()
方法:
PS> 'TP ID:{0} {1}' -f $response.Id, $response.Name
TP ID:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
[1] 您也可以通过在自定义对象上调用 .psobject.ToString()
来获得此表示;奇怪的是,从 PowerShell Core 6.1.0 开始,只是 .ToString()
不起作用; this GitHub issue.
[2] 请注意 Write-Host
的目的是提供 for-display-only (to-host) 输出,这绕过 PowerShell 的成功输出流,从而能够捕获或重定向其输出 - 有关详细信息,请参阅