I have a simple example pieced together from some other posts:
[CmdletBinding(
DefaultParameterSetName = 'Secret'
)]
Param (
[Parameter(Mandatory=$True)]
[string]$FileLocation,
[Parameter(
Mandatory = $True,
ParameterSetName = 'Secret'
)]
[Security.SecureString]${Type your secret password},
[Parameter(
Mandatory = $True,
ParameterSetName = 'Plain'
)]
[string]$Password
)
if ($Password) {
$SecretPassword = $Password | ConvertTo-SecureString -AsPlainText -Force
} else {
$SecretPassword = ${Type your secret password}
}
Write-Host $SecretPassword
$BSTR = `
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecretPassword)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
Write-Host $PlainPassword
How can I use powershell's read-host function to accept a password for an external service?
When running the script how can I make it possible for the entered password to be masked?
So instead of this:
PS C:\temp> .\test.ps1 -FileLocation C:\Temp\file.csv -Password secret
This
PS C:\temp> .\test.ps1 -FileLocation C:\Temp\file.csv -Password ******