Forum Discussion

silver1979's avatar
silver1979
Copper Contributor
May 31, 2022
Solved

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...
  • LainRobertson's avatar
    Jun 01, 2022

    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

Resources