Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

string formatting - Parsing a Powershell variable

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'
}

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}

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

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

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


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...