I'm trying to find an elegant way to pass parameters in a powershell script, where a string can contain any number of special characters that will need to be escaped. For example, a complex password with special characters.
I looked at the -encodedcommand option, but it looks like it is only intended to pass an encoded script block, not an encoded version of the parameters.
For example, consider the following script:
param(
[Parameter()][Alias("un")][string]$Username,
[Parameter()][Alias("pw")][string]$Password
)
Write-Host "Username: $Username"
Write-Host "Password: $Password"
The string '-un testuser -pw testpw' is encoded by base64 as follows: LQB1AG4AIAB0AGUAcwB0AHUAcwBlAHIAIAAtAHAAdwAgAHQAZQBzAHQAcAB3AA ==
I tried to call the script as a .ps1 file and pass a -coded code with the line above, but I got the error "Parameter A could not be found, which matches the parameter name" encodedcommand "
So great, this should be a call to powershell.exe directly.
Also tried the following: powershell.exe -encodedcommand LQB1AG4AIAB0AGUAcwB0AHUAcwBlAHIAIAAtAHAAdwAgAHQAZQBzAHQAcAB3AA == -file Base64ParamTest.ps1
This ran the script, but the parameters did not matter.
This behaves the way I expected, but not the way I hope. Is there a way to pass my parameters on their own as safely encoded strings?
source
share