2
假設我的腳本如下:PowerShell和美元符號
.\myDollarParam.ps1 -Password Pa$sword
Pa
我想下面的輸出:
param([string]$password)
Write-Host $password
如果我啓動它這樣這將返回
Pa$sword
有沒有辦法做到這一點,在參數中使用`或\?
謝謝。
假設我的腳本如下:PowerShell和美元符號
.\myDollarParam.ps1 -Password Pa$sword
Pa
我想下面的輸出:
param([string]$password)
Write-Host $password
如果我啓動它這樣這將返回
Pa$sword
有沒有辦法做到這一點,在參數中使用`或\?
謝謝。
簡單的方法是用單引號括起來。對於前:
.\myDollarParam.ps1 -Password 'Pa$sword'
否則PowerShell的解釋「$」作爲變量的開始,所以你基本上是在你的例子說,是分配變量存儲在值「霸」,然後將值變量「$劍」
A到證明這個理論運行下面給我的有趣的結果:
$sword = "Stuff"
.\myDollarParam.ps1 -Password Pa$sword
PaStuff
通過在單引號包圍,你說通過文字字符在此字符串。
執行密碼的「正確」方法是使用ConvertTo-SecureString cmdlt。
非常感謝,我如何從中獲得:'$ password = ConvertTo-SecureString $ password -asplaintext -force' to a string?我得到這個時,我打印$密碼'System.Security.SecureString' – HelloToTheWorld
@HelloToTheWorld這完全是一個完全不同的問題。發佈一個新問題,你會很快得到一個答案。 – zdan
從安全字符串轉換回文本並不容易。這裏是一個鏈接到一個函數的代碼:[link](http://pastebin.com/yPAYBGpH) – HAL9256