PowerShell If语句:If等于多个特定文本



早上好!我正在编写一个脚本,并试图包含一个基于文本变量的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语句中,当所有其他情况都不匹配时,将执行默认值。请测试一下并告诉我。

$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"
}
}
}

首先要担心输入验证Test-Path调用的区别

$validValues = 'Specific text 1','Specific text 2','Specific text 3'
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
if($validValues -notcontains $text) {
Write-Host "Invalid output"
}
elseif(!(Test-Path -LiteralPath $path$text)) {
Write-Host "Item exists"
}
else {
New-Item -ItemType Directory -LiteralPath $Path$text
}

选择字符串版本:

if ('text' | select-string text,text2,text3) { 'yes' } 
yes

最新更新