Getting string from what appears to be a hash-map in PowerShell - powershell

In PowerShell, I do this command:
$w = iwr -usedefaultcredentials -uri http://acme.corp/anvils.aspx -method 'POST' -Body '{"ctl00_MainEntityNumber:"Wiley"}'
That returns an object, part of which is an object called InputFiles, one of which is an object called __VIEWSTATE. I want to get the string value of __VIEWSTATE. So I do this:
$f = $w.inputfields | where id -eq __VIEWSTATE | select-object value | select-string value
which gives me a MatchInfo object. If I just print out the object, I get
#{value=/wEPDwUJODU5MDM5MTc0DxYEHhBQYWdlRW50aXR5TnVtYmVyZh4Yb0luc3VyYW5jZVNlYXJjaENyaXRlcmlhMv0BAAEAAAD/////AQAAAAAAAAAMAgAAAFFJRE9JLlJlZ3VsYXRlZEV ....}
which looks to me like a Hashmap or something. But when I try $f.value or $f['value'] I get nothing, or if I $f[0], I get the whole thing (including the #{value=.. part) So the question is, how do I get just the string after the #{value=\ ? Do I have to parse it out manually?

If you're just wanting the value itself then change it to
$f = $w.inputfields | where id -eq __VIEWSTATE | Select-Object -ExpandProperty value

Related

How can I store the headers of a CSV file in an array?

I have looked around and it should be fairly simple , the code I found was like this:
$headers = import-csv $file |
select-object -first 1 | foreach-object { $_.PSObject.Properties } |
select-object -expandproperty Name
but did not work, checking teh $headers will show as empty or null , i have another array which I use to check by using write-host , and the other array works fine, but the one created with the code above does not at all.
I just want to store on $headers an array that has the headers of the csv file.

How do I get the data results from Invoke-RestMethod into CSV file?

I'm trying to get the results (data) from a REST call into a CSV file.
My code is:
$results = Invoke-RestMethod -Method Get -Uri $url -Header $bearer -ContentType 'application/json'
$results
and it comes back with:
data
----
{#{type=flight-offer; id=1559566119876--1838046263; offerItems=System.Object[]}, #{type=flight-offer; id=15595...
I want to get the values from each offerItems object into a CSV file.
Expand the data property and export the nested objects to a CSV:
$results |
Select-Object -Expand data |
Export-Csv 'C:\path\to\output.csv' -NoType
However, since your data objects apparently contain an array (offerItems=System.Object[]) you'll have to mangle that array into a string first, e.g. like this:
... |
Select-Object -Expand data |
Select-Object *,#{n='offerItems';e={$_.offerItems -join ','}} -Exclude offerItems |
...
The merge operation may differ depending on what kind of data the array holds.

How to Display the Last Half of a String in the Results Returned by windows "findstr" command?

I have a windows 2012 instance on AWS in which I am trying to return the instance ID from the CLI. I can successfully return that info into a variable with this command:
$instanceId = Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id
I can then echo the contents of that variable and filter out the pertinent line:
PS C:\Users\Administrator> echo $instanceId | findstr /b /c:"Content "
Content : i-4bee88888bd72g2a
The problem I have is that I wish to return only the string after the colon, so the output would look like:
i-4bee88888bd72g2a
What switch can I add to findstr to filter out that string? What is the Microsoft equivalent to sed?
PowerShell outputs objects, not text. When you run this command:
Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id
it outputs a string representation of an object with a Content property. To select only the value of that property, you can use Select-Object -ExpandProperty as follows:
Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id | Select-Object -ExpandProperty Content
This tells PowerShell: "There's an output object, and I want only the value of its Content property."
You can assign this to your variable:
$instanceId = Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id | Select-Object -ExpandProperty Content
You can also probably write it this way:
$instanceId = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id).Content
(That is, the ( ) enclose an expression, and you are getting the Content property of the expression's output object.)
I found a better solution that returns the exact results; it uses 'replace', the MS version of 'sed':
$instanceId = Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id
$contentString = $instanceId | findstr /b /c:"Content "
$desiredresult = $contentString -replace "(Content :)\s([a-z]+)",'$2'
$desiredresult
Which returns the exact results:
i-4bee88888bd72g2a
I may have been barking up the wrong tree with the command findstr. I found that I can display the exact string withe command select-string:
$instanceId | select-string -Pattern "i-"
Which returns the results:
i-4bee88888bd72g2a
(But it included blank lines before and after the results, which I may have to discard, TBD.)

Get first value that is not false from json mixed types

This seems like it should be straight forward but I'm not sure why Powershell is having trouble.
I'm getting data from Node.js converting it to JSON and then I want to get the first object which is not false.
(Invoke-WebRequest -UseBasicParsing -Uri "https://nodejs.org/dist/index.json").Content |
ConvertFrom-Json | ? { $_.lts -ne 'False' }
I also tried but it didn't work either:
| ? { -not (-not $_.lts) }
I know the above doesn't actually get me the first value. I haven't found that solution yet. But help with that would be nice too!
The data set is something like this:
[
{"lts": false},
{"lts": 'Carbon'}
]
You can see the complete data set here.
Update
When I set the JSON value to a variable it works. Strange.
Invoke-WebRequest -UseBasicParsing -Uri 'https://nodejs.org/dist/index.json' `
|% Content `
| ConvertFrom-Json `
|% { $_ } `
|? lts -ne $False `
;
The ConvertFrom-Json converts the JSON array into Object[] which has to be exploded to be processed record-by-record. The ForEach-Object after ConvertFrom-Json splits them up nicely.

Parsing System.Object in PowerShell

I have a Powershell command that returns an object from an Invoke-RestMethod that is doing an HTTP Get. If I echo the return it looks like this:
policies
--------
{#{id=12345; name=xxxx; ... }
I want to parse out the value of id. What approach would I take to accomplish this?
As stated in #Will comment -> Use Get-Member to get the object type, like:
$returnVal = Invoke-RestMethod ...
$returnVal | Get-Member
This should return the type of the returned object (including methods and properties you're able to call on the object).
It seems that policies might be a dictionary, but I'm not sure. If it is a dictionary you can try:
$returnVal = Invoke-RestMethod ...
$dict = $returnVal | select -ExpandProperty policies
$id = $dict.Get_Item("id")
If it is simple JSON you can deserialize it via:
$returnVal = Invoke-RestMethod ...
$derserializedJsonObject = $returnVal | select policies | ConvertTo-Json
Hope that helps