windows - Powershell 和美元符号参数

标签 windows powershell

假设我的脚本如下:

param([string]$password)
Write-Host $password

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

.\myDollarParam.ps1 -Password Pa$sword
 Pa

我想要以下输出:

 Pa$sword

除了在参数中使用 ` 或\之外,还有其他方法吗?

谢谢。

最佳答案

最简单的方法是用单引号括起来。例如:

.\myDollarParam.ps1 -Password 'Pa$sword'

否则 PowerShell 将“$”解释为变量的开头,所以您在示例中基本上是在为变量分配值“Pa”,然后将值存储在变量“$sword”中

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

$sword = "Stuff"
.\myDollarParam.ps1 -Password Pa$sword
PaStuff

通过用单引号引起来,您是说传递此字符串中的文字字符。

设置密码的“正确”方法是使用 ConvertTo-SecureString cmdlt。

关于windows - Powershell 和美元符号参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17195668/

相关文章:

arrays - 如何处理 “distorted” array-output

用于从 csv 文件创建计划任务的 Powershell 脚本

java - 即使应用程序在 java/javafx 中关闭,它也会在后台运行

php - Psexec 与 SSH - 从 Windows 执行远程命令

c++ - Windows (.lnk) 快捷方式 API?

firebase - VSC PowerShell。 npm 更新包 .ps1 后无法加载,因为在此系统上禁用了运行脚本

powershell - PowerShell 中使用命名空间的范围是什么?

java - 将使用 Netbeans 创建的 Java 程序固定在任务栏上?

c++ - QtSerialPort 有不可用的数据,仅在 Windows 8 下出现段错误

regex - Powershell RegEx 在字符串开头匹配负向后查找