2013-06-19 141 views
2

假设我的脚本如下:PowerShell和美元符号

.\myDollarParam.ps1 -Password Pa$sword 
Pa 

我想下面的输出:

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

如果我启动它这样这将返回

Pa$sword 

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

谢谢。

回答

7

简单的方法是用单引号括起来。对于前:

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

否则PowerShell的解释“$”作为变量的开始,所以你基本上是在你的例子说,是分配变量存储在值“霸”,然后将值变量“$剑”

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

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

通过在单引号包围,你说通过文字字符在此字符串。

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

+0

非常感谢,我如何从中获得:'$ password = ConvertTo-SecureString $ password -asplaintext -force' to a string?我得到这个时,我打印$密码'System.Security.SecureString' – HelloToTheWorld

+0

@HelloToTheWorld这完全是一个完全不同的问题。发布一个新问题,你会很快得到一个答案。 – zdan

+0

从安全字符串转换回文本并不容易。这里是一个链接到一个函数的代码:[link](http://pastebin.com/yPAYBGpH) – HAL9256