skip to Main Content

I have a really simple question which I am sure someone can answer really quickly for me.

I have one text file (password.txt) containing a random string of characters which has been generated by a Poweshell script to be used as a wifi password.

I have another script (change-password.ps1) which logs into my router and changes the password. I have managed to get this script to look at a json file (config.json) and use one of static passphrases.

This is my json file

{ "PSKs": {
            "01":"PSKforJanuary123!!",
            "02":"PSKforFebruary123!",
            "03":"PSKforMarch123!",
            "04":"PSKforApril123!",
            "05":"PSKforMay123!",
            "06":"PSKforJune123!",
            "07":"PSKforJuly123!",
            "08":"PSKforAugust123!",
            "09":"PSKforSeptember123!",
            "10":"PSKforOctober123!",
            "11":"PSKforNovember123!",
            "12":"PSKforDecember123!"
        }
    }

So instead of using one of these from the list above, how can I replace the "PSKfor***123!" with the line of text from my password.txt?

Or is there a way I can create this random string of characters within my json file without having to run another script to generate it?

Thanks in advance!

2

Answers


  1. How to update all properties in the Json using a password stored in a file:

    $json = Get-Content pathtojson.txt -Raw | ConvertFrom-Json
    $pass = Get-Content pathtopassword.txt
    $json.PSKs.PSObject.Properties | ForEach-Object { $_.Value = $pass }
    $json | ConvertTo-Json | Set-Content pathtojson.txt
    

    How to update a single property in the Json using a password stored in a file, say for example, to dynamically update the property that corresponds with the current month:

    $json = Get-Content pathtojson.txt -Raw | ConvertFrom-Json
    $pass = Get-Content pathtopassword.txt
    $month = [datetime]::Now.ToString('MM')
    # `$json.PSKs.$month` would work too here
    $json.PSKs.PSObject.Properties[$month].Value = $pass
    $json | ConvertTo-Json | Set-Content pathtojson.txt
    

    How to update all properties using a randomly generated password, for this you will need to figure out what logic you want to use to generate a random password. A very easy way is via RandomNumberGenerator.GetString(ReadOnlySpan<Char>, Int32) Method but this method only works in PowerShell 7.

    # `$chars` can be whatever you like, this is just an example
    $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%&_-+='.ToCharArray()
    $json = Get-Content pathtojson.txt -Raw | ConvertFrom-Json
    $json.PSKs.PSObject.Properties | ForEach-Object {
        # `20` is for the string length
        $_.Value = [System.Security.Cryptography.RandomNumberGenerator]::GetString($chars, 20)
    }
    $json | ConvertTo-Json | Set-Content pathtojson.txt
    
    Login or Signup to reply.
  2. NOTE: my solutions require Powershell 7.x

    Given you know the key, replacing all the values it’s easy

    $JsonHashTable = Get-Content -Path $JsonFile | ConvertFrom-Json -AsHashtable -Depth 100
    $PasswordString = Get-Content -Path $PasswordFile
    
    # .Clone() necessary because otherwise the Keys list gets refreshed
    # at every change and that causes a error.   
    foreach ($Key in $JsonHashTable.PSKs.Keys.Clone()) {      
        $JsonHashTable.PSKs.$Key = $PasswordString 
    }
    
    $JsonHashTable | ConvertTo-Json -Depth 100 | Set-Content -Path $JsonFile
    

    To change one specific month:

    while ($MonthToChange -notin 1..12) {
        # Evaluate some error handling   
        [int]$MonthToChange = Read-Host -Prompt 'Insert number of the month to change [0-12]'
    }
    
    $MonthKey = '{0:d2}' -f $MonthToChange
    
    $JsonHashTable.PSKs.$MonthKey=$PasswordString
    

    To dynamically generate a random string to use as password, given the apparent low-risk environment(you will have those passwords store in plain text after all) I’d suggest using

    $PasswordString = New-Guid | Select-Object -ExpandProperty Guid
    

    Depending on the real use situation, evaluate implementing some random string generator module.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search