Fit*_*ery 6 powershell if-statement
早上好!我正在编写一个脚本,我正在尝试包含一个基于具有多个特定文本选项的文本变量的 IF/Else 语句,并测试以该变量命名的目录路径,如果没有,则创建该目录不存在。所以例子是
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
If($text -match "Specific Text1") -and (!(Test-Path $Path\$text))){
new-item -ItemType directory -path $Path\$text
}
ElseIF($text -match "Specific Text2") -and (!(Test-Path $Path\$text))){
new-item -ItemType directory -path $Path\$text
}
ElseIF($text -match "Specific Text3") -and (!(Test-Path $Path\$text))){
new-item -ItemType directory -path $Path\$text
}
ElseIF($text -notmatch "Specific Text1","Specific Text2","Specific Text3"){
write-host "invalid input"
}
我为用户提供了一个有效输入的列表,可以输入到文本框中。当我尝试运行脚本时,我仍然收到错误消息,指出该文件夹已经存在,但它并没有像应有的那样忽略它。
一个附带问题是否有更简洁的方法来写这个?
编辑
以下是对我来说完美的答案。谢谢大家的回复!
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If ($text -in $AcceptableText)
{
    If (!(Test-Path $Path\$text)) 
    {
        new-item -ItemType directory -path $Path\$text
    }
}
Else
{
    write-host "invalid input"
}
如果您的用户选择 3 个文本短语之一并且该目录尚不存在,那么您似乎正在尝试创建目录,如果他们选择了 3 个文本短语以外的其他内容,则向您的用户抱怨。我会分别对待每一种情况:
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If ($text -in $AcceptableText)
{
    If (!(Test-Path $Path\$text)) 
    {
        new-item -ItemType directory -path $Path\$text
    }
}
Else
{
    write-host "invalid input"
}
或者您可以先像这样测试目录是否存在:
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If (!(Test-Path $Path\$text))
{
    If (($text -in $AcceptableText)) 
    {
        new-item -ItemType directory -path $Path\$text
    }
    Else
    {
        write-host "invalid input"
    }
}
编辑:或者,如果您想变得棘手并避免使用测试路径(如@tommymaynard 所建议的那样),您可以使用以下代码(如果您不想进行错误检查,甚至可以消除 Try|Catch 包装器)
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If (($text -in $AcceptableText)) 
{
    try { mkdir -path $Path\$text -ErrorAction Stop } #Change to -ErrorAction Ignore if you remove Try|Catch
    catch [System.IO.IOException] { } #Do nothing if directory exists
    catch { Write-Error $_ }        
}
Else
{
    write-host "invalid input"
}
编辑:另外值得注意的是,有很多方法可以使用 PowerShell 创建文件夹
一些漂亮、干净的例子。希望这可以帮助。
是的,您可以 :) 请注意,在 switch 语句中,当所有其他情况不匹配时,将执行 default。请测试并告诉我。
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
if (!(Test-Path $Path\$text))
{
    switch ($text)
    {
        "Specific Text1"
        {
            new-item -ItemType directory -path $Path\$text
        }
        "Specific Text2"
        {
            new-item -ItemType directory -path $Path\$text
        }
        "Specific Text3"
        {
            new-item -ItemType directory -path $Path\$text
        }
        default
        {
            write-host "invalid input"
        }
    }
}