Forum Discussion
silver1979
May 31, 2022Copper Contributor
powershell how to convert text in a text file.
one text file is below: "Null" "Null" AccessKeyId : abcde AccessKeySecret : 217peach Expiration : 2022-05-31T18:38:52Z SecurityToken : CAIS/Qaa LastUpdated : 2022...
- Jun 01, 2022
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
LainRobertson
Jun 01, 2022Silver Contributor
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
- silver1979Jun 01, 2022Copper Contributoryou are awesome! It works