Powershell和美元签到参数

Hel*_*rld 3 windows powershell

我们假设我的脚本如下:

param([string]$password)
Write-Host $password
Run Code Online (Sandbox Code Playgroud)

如果我以这种方式启动它将返回:

.\myDollarParam.ps1 -Password Pa$sword
 Pa
Run Code Online (Sandbox Code Playgroud)

我想要以下输出:

 Pa$sword
Run Code Online (Sandbox Code Playgroud)

有没有办法在参数中使用`或\来做到这一点?

谢谢.

HAL*_*256 10

简单的方法是将其用单引号括起来.例如:

.\myDollarParam.ps1 -Password 'Pa$sword'
Run Code Online (Sandbox Code Playgroud)

否则,PowerShell会将"$"解释为变量的开头,因此您在示例中基本上说的是将变量赋值为"Pa",然后将值存储在变量"$ sword"中

A证明这个理论运行以下给出了有趣的结果:

$sword = "Stuff"
.\myDollarParam.ps1 -Password Pa$sword
PaStuff
Run Code Online (Sandbox Code Playgroud)

通过用单引号括起来,你就是说传递这个字符串中的文字字符.

执行密码的"正确"方法是使用ConvertTo-SecureString cmdlt.