Hi I am trying to create Environment variable through Powershell that consist Password but I guess it needs to encrypted. But When i am trying with below code it's not working. But I need to encrypt it. Can it's possible to encrypt it and no one can see the password.
try {
$sourceFile1 = "C:\vault\service_acct_pass.json"
$RowValues = Get-Content $sourceFile1 | Select -skip 1 | select-string -pattern '---' -notmatch
$RowValues = $RowValues -replace '\s{1,}',"`t" | out-file -filepath C:\vault\output.txt
$sourceFile = "C:\vault\output.txt"
$RowValues = Get-Content $sourceFile
$Line = $RowValues.split("`t")
$columnCount = (Import-Csv $sourceFile -Delimiter "`t" | get-member -type NoteProperty).count
$myArray = @()
for ($i=0; $i -lt $columnCount; $i++) {
$myArray += $Line[$i]
}
$columnSource = import-csv $sourceFile -Delimiter "`t"
foreach ($element in $myArray) {
ConvertTo-SecureString "[System.Environment]::SetEnvironmentVariable($element,$columnSource.$element,[System.EnvironmentVariableTarget]::Machine)" -AsPlainText -Force
}
Remove-Item $sourceFile -Recurse
}
catch {
"Unforeseen Errors"
$error[0]
}
Note: In the above code I am reading a file that is having key and password. When I am only using this line [System.Environment]::SetEnvironmentVariable($element,$columnSource.$element,[System.EnvironmentVariableTarget]::Machine) instead of ConvertTo-SecureString "[System.Environment]::SetEnvironmentVariable($element,$columnSource.$element,[System.EnvironmentVariableTarget]::Machine)" -AsPlainText -Force this its works fine and save the variables but it's visible to everyone.