SOLVED

powershell how to convert text in a text file.

Occasional Contributor

one text file is below:

"Null"

"Null"

AccessKeyId        : abcde

AccessKeySecret : 217peach

Expiration            : 2022-05-31T18:38:52Z

SecurityToken      : CAIS/Qaa

LastUpdated        : 2022-05-31T12:38:52Z

Code                    : Success

 

how to convert text in this file to below:

[Credentials]
language=EN
endpoint=tempstock
accessKeyID=abcde
accessKeySecret=217peach

stsToken=CAIS/Qaa

 

2 Replies
best response confirmed by silver1979 (Occasional Contributor)
Solution

@silver1979 

 

Here's a condensed option for doing what you asked.

 

$Dictionary = [hashtable]::new();

# Fetch the data from the specified file and transform it to a Dictionary.
Get-Content -Path .\forums.txt | where { $_ -and $_ -notmatch "null" } | ForEach-Object { $kvp = $_.Replace(" ", "").Split(':', 2); $Dictionary.Add($kvp[0], $kvp[1]); };

# Using Write-Host to dump to the screen. Use Out-File if you prefer it goes to file.
"[Credentials]`nlanguage=EN`nendpoint=tempstock`naccessKeyID=$($Dictionary["AccessKeyId"])`naccessKeySecret=$($Dictionary["AccessKeySecret"])`nstsToken=$($Dictionary["SecurityToken"])" | Write-Host;

 

Cheers,

Lain

you are awesome! It works