使用Powershell-encodedcommand传递参数

使用Powershell-encodedcommand传递参数,powershell,parameters,command-line-arguments,Powershell,Parameters,Command Line Arguments,我试图找到一种优雅的方法,将参数传递给powershell脚本,其中字符串可以包含任何数量的需要转义的特殊字符。例如,具有特殊字符的复杂密码 我正在查看-encodedcommand选项,但它似乎只用于传递编码的脚本块,而不是参数的编码版本 例如,考虑以下脚本: param( [Parameter()][Alias("un")][string]$Username, [Parameter()][Alias("pw")][string]$Password ) Write-Host "Usernam

我试图找到一种优雅的方法,将参数传递给powershell脚本,其中字符串可以包含任何数量的需要转义的特殊字符。例如,具有特殊字符的复杂密码

我正在查看-encodedcommand选项,但它似乎只用于传递编码的脚本块,而不是参数的编码版本

例如,考虑以下脚本:

param(
[Parameter()][Alias("un")][string]$Username,
[Parameter()][Alias("pw")][string]$Password
)

Write-Host "Username: $Username"
Write-Host "Password: $Password"
字符串'-un testuser-pw testpw'的base64编码如下: LQB1AG4AIAB0AGUACWB00AHUACWBLAHAIAATAHAADWAGAHQAZQBZAHQA3AA==

我尝试以.ps1文件的形式调用脚本,并使用上述字符串传递-encodedcommand,但出现错误“找不到与参数名称“encodedcommand”匹配的参数”

好吧,这必须是直接调用powershell.exe

还尝试了以下方法: powershell.exe-encodedcommand LQB1AG4AIAB0AGUACWBLAHIAIAIAATHAADWAGAHQAZQBZAHQACAB3A==-文件Base64ParamTest.ps1

这将运行脚本,但参数没有值


这是我所期望的,但不是我所希望的。是否有办法将我的参数本身作为安全编码字符串传递?

您必须将脚本调用作为命令的一部分,例如:

PS> $command = "& '$pwd\login.ps1' -un testuser -pw testpw"
PS> $bytes = [Text.Encoding]::Unicode.GetBytes($command)
PS> $encodedCommand = [Convert]::ToBase64String($bytes)
PS> powershell.exe -noprofile -encodedCommand $encodedCommand
Username: testuser
Password: testpw
以下是我过去在脚本中处理密码的一些注意事项:

###########################################################
#
# Stashing passwords to avoid interactive password prompting
#

# NOT RECOMMENDED BUT IF PASSWORD IS DYNAMIC OR WIDELY KNOWN

$passwd = ConvertTo-SecureString "Not Very Secret Password" -AsPlainText -Force

# Need a way to prompt for password and use clear text password for use with net use
$cred = Get-Credential
$cred.GetNetworkCredential().UserName 
$cred.GetNetworkCredential().Password

#
# SAFE BUT NOT NECESSARILY PORTABLE APPROACH 
# Depends on how DPAPI works with roaming profiles
#

# Capture once and store to file
$passwd = Read-Host "Enter password" -AsSecureString
$encpwd = ConvertFrom-SecureString $passwd
$encpwd
$encpwd > $path\password.bin

# Later pull this in and restore to a secure string
$encpwd = Get-Content $path\password.bin
$passwd = ConvertTo-SecureString $encpwd

# Let's see if the rehydrate worked?
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwd)
$str =  [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$str

$cred = new-object System.Management.Automation.PSCredential 'john',$passwd
$cred

# NOTE: The "secret" required to rehyrdate correctly is stored in DPAPI - consequence:
#       You can only rehydrate on the same machine that did the ConvertFrom-SecureString


#
# PORTABLE BUT NOT NECESSARILY SAFE APPROACH
#

# Let's do this so that it will work on multiple machines:

$key = 1..32 | ForEach-Object { Get-Random -Maximum 256 }
$passwd = Read-Host "Enter password" -AsSecureString
$encpwd = ConvertFrom-SecureString $passwd -Key $key
$encpwd
# Could easily modify this to store username also
$record = new-object psobject -Property @{Key = $key; EncryptedPassword = $encpwd}
$record
$record | Export-Clixml $path\portablePassword.bin

# Later pull this in and restore to a secure string
$record = Import-Clixml $path\portablePassword.bin
$passwd = ConvertTo-SecureString $record.EncryptedPassword -Key $record.Key

# Let's see if the rehydrate worked?
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwd)
$str =  [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$str

$cred = new-object System.Management.Automation.PSCredential 'john',$passwd
$cred

Start-Process powershell.exe -Credential $cred -NoNewWindow

# Portable is better BUT the secret (Key) is shared (stored with the password file)
# Can be reversed to original password - still much better than clear-text password
# stored in your script.

我的计划B是只对密码值本身进行Base64编码,而不是对整个参数字符串进行编码,然后在脚本中对其进行解码。很简单,但是我想知道我是否缺少一种更简单的方法来自动完成它,包括编码字符串中的脚本。简单的解决方案,但我在任何地方都没有看到这样的例子。非常感谢你!