May 31 2022 06:55 AM - edited May 31 2022 07:49 AM
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
May 31 2022 05:21 PM
Solution
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
May 31 2022 05:21 PM
Solution
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