【问题标题】:Stopping * been entered as user input停止 * 已作为用户输入输入
【发布时间】:2017-02-11 10:02:35
【问题描述】:

我有一个脚本,它要求用户输入,然后将其用作变量来验证目录路径是否存在,但是当用户输入 * 时,验证会在它应该失败的地方返回成功。

输入是通过Read-Host完成的,代码示例如下。

$userinput = Read-Host -Prompt "Enter Value"
if(Test-Path -Path "c:\$userinput\") {"Valid"} else {"not valid"}

【问题讨论】:

  • -Path -> -LiteralPath
  • 谢谢,这么简单你已经提到了

标签: powershell powershell-4.0


【解决方案1】:

只是写@PetSerAl 的评论作为答案

-path 将 * 视为可能导致成功的通配符。 解决方案是使用-LiteralPath,它按字面意思对待路径。

$userinput = Read-Host -Prompt "Enter Value"
if(Test-Path -LiteralPath "c:\$userinput\") 
{
    "Valid"
} 
else 
{
    "not valid"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-13
    • 2023-04-05
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多