解析 Powershell 变量 [英] Parsing a Powershell variable

查看:59
本文介绍了解析 Powershell 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们给了我很大的帮助 - 让我先说一下.

You have all been a great help - let me start by saying that.

我从我运行的函数中得到以下输出 - 数据存储在一个名为 $response 的变量中,该变量从 Invoke-RestMethod 调用中获得.

I get the below output from a function I run - the data is stored in a variable called $response, obtained from an Invoke-RestMethod call.

@{ResourceType=UserStory; Id=202847; Name=Professional Lines: 2,800,000 Policy Aggregate Limit update}    

我如何将这些值解析为新变量,以便最终结果是

How do i go about parsing these values into new variables so the end result is

TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update

推荐答案

您所展示的是自定义对象字符串化形式,它使用hashtable-like 文本表示.

What you're showing is the stringified form of a custom object, which uses a hashtable-like textual representation.

该自定义对象的定义如下所示:

That custom object's definition looks something like this:

# 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]:

If you include this variable in an expandable string (interpolating string), you'll get the representation in your question[1] - note that you'll get the same representation if you pass the object to the Write-Host cmdlet[2]:

PS> "$response"  # Note the enclosing "..."
@{ResourceType=UserStory; Id=202847; Name=Professional Lines: 2,800,000 Policy Aggregate Limit update}

请注意,如果没有封闭的 "...",您将获得更好的表格表示,这由 PowerShell 的输出格式系统提供.

Note that without the enclosing "...", you'd get a nicer, tabular representation, courtesy of PowerShell's output-formatting system.

PS> $response  # direct output of the object

ResourceType     Id Name
------------     -- ----
UserStory    202847 Professional Lines: 2,800,000 Policy Aggregate Limit update


假设 $response 实际上仍然是一个自定义对象,而不是它的字符串表示,你可以简单地访问自定义对象的属性来构建您的字符串,再次使用可扩展字符串:


Assuming $response is actually still a custom object, and not a string representation of it, you can simply access the custom object's properties to build your string, again using an expandable string:

PS> "TP Id:$($response.Id) $($response.Name)"
TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update

注意需要在属性引用周围使用 $(...) - 请参阅此答案 总结 PowerShell 的字符串扩展规则.

Note the need to use $(...) around the property references - see this answer for a summary of PowerShell's string-expansion rules.

一种替代方法是使用 -f 运算符,它使用像 .NET String.Format() 方法:

An alternative approach is to use the -f operator, which uses formatting strings like the .NET String.Format() method:

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 中讨论了这种令人惊讶的差异.


[1] You can also obtain this representation by calling .psobject.ToString() on a custom object; curiously, just .ToString() doesn't work, as of PowerShell Core 6.1.0; this surprising discrepancy is discussed in this GitHub issue.

[2] 请注意,Write-Host 的目的是提供 for-display-only(到主机)输出,绕过 PowerShell 的成功输出流,从而能够捕获或重定向其输出 - 有关详细信息,请参阅此答案.

[2] Do note that Write-Host's purpose is to provide for-display-only (to-host) output, which bypasses PowerShell's success output stream, and thereby the ability to capture or redirect its output - see this answer for more information.

这篇关于解析 Powershell 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆