Securely Loading Credentials from settings.json in PowerShell
Example settings.json
1
2
3
4
{
"Username": "DOMAIN\\User",
"Password": "StrongPassword123!"
}
PowerShell Script to Load and Use the Credentials
1
2
3
4
5
6
7
8
9
$jsonPath = Join-Path -Path $PSScriptRoot -ChildPath 'settings.json'
$config = Get-Content -Path $jsonPath -Raw | ConvertFrom-Json
$securePassword = ConvertTo-SecureString -String $config.Password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($config.Username, $securePassword)
$session = New-PSSession -ComputerName "TargetComputer" -Credential $credential
Invoke-Command -Session $session -ScriptBlock { hostname }
Remove-PSSession -Session $session
β οΈ Avoid using plain-text credentials in shared environments.