question

Walnut-6784 avatar image
0 Votes"
Walnut-6784 asked RichMatheisen-8856 edited

Newb powershell - skipping a row to convert a file to read with convert json

I’ve had a look online and haven’t quite found something that can help me.

I know I want to do something like this:

 $j = output of my aws session credentials | ConvertFrom-Json
    
 Write-Host "Acccess Key ID:" $j.AccessKeyId
 Write-Host "Secret Acccess Key:" $j.SecretAccessKey
 Write-Host "Session Token:" $j.SessionToken

But the problem I have is that my Aws session credentials isn’t clean, it looks like this:

DTS Request ID: hdhdhsbshdhdbsuxbd
{
“Accesskeyid” : “Juddn48373hdhdhd”,
“SecretAccessKey” : “bshdbsb//273!nshdbsb”,
“SessionToken” : “bsbdbdhdhxbche7373738nxjdj/////bdhdhdbxjdn83!!jsjdjs+/////jdhdhdbsbxhdj”,
“TokenExpiration” : “date goes here”
“Packedpolicysize” :86
}

So I figure if I can skip the first row, then I can use ConvertFrom-Json to pick up the keys I need and store them as environment variables.

How can I skip the first row to do this?
Is this the best way, or is there a better approach?

Thanks in advance!



windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

I'm also assuming that the missing comma at the end of the "TokenExpiration" line is just a typo on your part -- if it isn't you have another problem!

But, if you have an array of strings that look like this:

 DTS Request ID: hdhdhsbshdhdbsuxbd
 {
 "Accesskeyid" : "Juddn48373hdhdhd",
 "SecretAccessKey" : "bshdbsb//273!nshdbsb",
 "SessionToken" : "bsbdbdhdhxbche7373738nxjdj/////bdhdhdbxjdn83!!jsjdjs+/////jdhdhdbsbxhdj",
 "TokenExpiration" : "date goes here",
 "Packedpolicysize" :86
 }

Then this should work:

 $j = $j | Select-Object -skip 1    # remove the 1st element (DTS Request ID: hdhdhsbshdhdbsuxbd)
 $j = $j -replace '/', '\/'       # escape the "/" characters
 $j | ConvertFrom-Json            # now convert the JSON


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

Is the credential an array or a string? If it's an array, you may try

 $cred[1..$cred.Count] | ConvertFrom-Json

If it's a string you can split it first

 $cred=$str.split("`n")
 $cred[1..$cred.Count] | ConvertFrom-Json


Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.