【问题标题】:Escape Character Needed for Password密码所需的转义字符
【发布时间】:2012-12-08 10:14:48
【问题描述】:

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

Import-Module WebAdministration
# 获取网站变量
$WebSite = Read-Host - 提示“网站的 DNS 名称”
$AppPoolName = Read-Host -Prompt "应用程序池名称"
[string]$AppPoolUser = Read-Host -Prompt “应用程序池用户名域不需要,例如 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 目录
New-Item -path d:\Logs -itemtype 目录
New-Item -path d:\Logs\$WebSite -itemtype 目录
New-Item -path d:\websites\$WebSite -itemtype 目录

【问题讨论】:

    标签: powershell escaping


    【解决方案1】:

    输入中的问题不是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))
    

    【讨论】:

    • 一定要使用Get-Credential。这就是它存在的原因。 Read-Host 不安全。
    【解决方案2】:

    这似乎不是 V3 的问题:

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

    【讨论】:

    • 你是对的,开头的感叹号只有-prompt参数有值才有这个效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多