HelperFunctions.psm1
function Save-ClearTextToEncryptedFile ($Password, $FileName) { $secureStringPwd = $Password | ConvertTo-SecureString -AsPlainText -Force $secureStringText = $secureStringPwd | ConvertFrom-SecureString Set-Content $FileName $secureStringText } function Save-SecureStringToEncryptedFile ($FileName, $Prompt) { if ($Prompt -eq $null) {$Prompt = "Enter Password:"} $secureStringPwd = Read-Host -Prompt $Prompt -AsSecureString $secureStringText = $secureStringPwd | ConvertFrom-SecureString Set-Content $FileName $secureStringText } function Get-SecureStringFromEncryptedFile ($FileName) { $pwdTxt = Get-Content $FileName $securePwd = $pwdTxt | ConvertTo-SecureString Write-Output $securePwd } function Get-ClearTextFromEncryptedFile ($FileName) { $pwdTxt = Get-Content $FileName $securePwd = $pwdTxt | ConvertTo-SecureString $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePwd) $clearText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) Write-Output $clearText } |