1

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!

5
  • 2
    "how can I replace the "PSKfor***123!" with the line of text from my password.txt?" replace where? your question is not clear. Commented Dec 11, 2024 at 16:45
  • @TangentiallyPerpendicular it was indeed a typo, I have corrected it now Commented Dec 11, 2024 at 16:50
  • @SantiagoSquarzon Where it says "PSKformonth123" in my json file, I am trying to replace it with a random string to use as a password. Commented Dec 11, 2024 at 16:52
  • Which one of the PSKformonth123 ? The one that corresponds with the current month (i.e.: 12 in this case) ? These clarifications should be in your question Commented Dec 11, 2024 at 16:57
  • @SantiagoSquarzon Sorry, all of them. I want to replace all of them with the same single password or just one line which doesn't specify a month. Once i have this worming I am going to run the script once a month so it generates a new one without having to check what month it is. It will change it every month regardless. Commented Dec 11, 2024 at 17:12

2 Answers 2

1

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

$json = Get-Content path\to\json.txt -Raw | ConvertFrom-Json
$pass = Get-Content path\to\password.txt
$json.PSKs.PSObject.Properties | ForEach-Object { $_.Value = $pass }
$json | ConvertTo-Json | Set-Content path\to\json.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 path\to\json.txt -Raw | ConvertFrom-Json
$pass = Get-Content path\to\password.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 path\to\json.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 path\to\json.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 path\to\json.txt
Sign up to request clarification or add additional context in comments.

3 Comments

Thats awesome! Thank you so much, this fixed my problem! I finally went with this: ''$json = Get-Content $PSScriptRoot\config.json -Raw | ConvertFrom-Json $pass = Get-Content $PSScriptRoot\luxion-guests.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 $PSScriptRoot\config.json''
Sorry I cannot get the hang of formatting on here :(
np glad it helped @Beckyboo
0

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.

1 Comment

Thanks for the response @sirtao, I have gone with the response above for now but I may come back to this at a later date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.