密码需要逃脱字符

时间:2012-12-08 10:14:49

标签: powershell escaping

此脚本非常适合接受一些读取主机提示并创建一个绑定了应用程序池的网站。密码提示工作,但我们最常用的密码是!$ Pass!123 !!并且它不允许我接收它作为输入。你如何允许使用这些字符?

Import-Module WebAdministration
# Get Web Site Variables
$WebSite = Read-Host -Prompt "DNS name for Web Site"
$AppPoolName = Read-Host -Prompt "Application Pool Name"
[string]$AppPoolUser = Read-Host -Prompt "Application Pool Username Domain not required ex. PXML.Proxy$"
$Password = Read-Host -Prompt "Application Pool Account Password, try ` before $ in password"
$HostHeader = Read-Host -Prompt "Host Header Name"
# cmd /c C:\scripts\BaseIIS.cmd $WebServer $AppPool $AppPoolUser $Password $HostName
New-Item -path d:\Websites -itemtype directory
New-Item -path d:\Logs -itemtype directory
New-Item -path d:\Logs\$WebSite -itemtype directory
New-Item -path d:\websites\$WebSite -itemtype directory

2 个答案:

答案 0 :(得分:3)

输入中的问题不是dollar sign($),而是起始exclamation mark(!) 你需要用另一个来逃避它。

您需要输入密码:!! $ Pass!123 !!

Read here to know more about this

为什么不使用get-credential cmdlet:

$cred = Get-Credential -Message "Insert Username and Passsword"    
$cred.GetNetworkCredential().UserName
Jacob    
$cred.GetNetworkCredential().Password
!$Pass!123!!

或者您可以使用:

$Password = Read-Host -Prompt "Application Pool Account Password" -assecurestring
[String]$Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))

答案 1 :(得分:0)

这似乎不是V3的问题:

PS>$t=Read-Host
!$Pass!123!!
PS>$t
!$Pass!123!!