问题
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
回答1:
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]:
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.
来源:https://stackoverflow.com/questions/53107236/parsing-a-powershell-variable