有没有办法在Powershell中的if语句中查找哪个条件

时间:2019-03-14 14:30:21

标签: powershell if-statement

我在if条件中进行了多次检查,如果其中任何一项失败,则状态应该失败。为此,我现在拥有多个if语句,将每个值与某些值进行比较,以便脚本可以通知确切失败的内容。有没有一种方法可以将这些条件放在if语句中的and条件中,并确定哪个匹配失败

if ($a -eq "apple")
{
    Write-Host "My fruit is apple"
}
else
{
    Write-Host "My fruit is not apple"
}
if ($b -eq "banana")
{
    Write-Host "My fruit is banana"
}
else
{
    Write-Host "My fruit is not banana"
}

如果我想最小化if语句我可以写

if (($b -eq "banana") -and ($a -eq "apple"))
{
    Write-Host "My fruit is banana and apple"
}
else
{
    Write-Host "Match failed for $a or $b"
}

在这种情况下,如果$ b是香蕉但$ a不是苹果,它将进入其他部分,但是我怎么告诉它$ a不匹配。我只需要知道不满足的条件

3 个答案:

答案 0 :(得分:3)

水果选项的位掩码应该可以很好地工作。这样的例子,

# Fruit values are such that each will toggle its own bits.
[flags()] Enum Fruits {
  Apple  = 0x01
  Banana = 0x02
  Pear   = 0x04
  Orange = 0x08
  Pomelo = 0x10
  Melon  = 0x20
}

# Add some fruits into a basket    
$basket = [fruits]'apple'+[fruits]'banana'

# Check contents    
[fruits].GetEnumNames() | % {
    "$_ in basket? " $basket.HasFlag([fruits]::$_)
}
Apple in basket?  True
Banana in basket?  False
Pear in basket?  True
Orange in basket?  False
Pomelo in basket?  False
Melon in basket?  False

# Fill basket with sum of fruits:    
$basket = [fruits]17

[fruits].GetEnumNames() | % {
    "$_ in basket? " $basket.HasFlag([fruits]::$_)
}
Apple in basket?  True
Banana in basket?  False
Pear in basket?  False
Orange in basket?  False
Pomelo in basket?  True
Melon in basket?  False

# Remove an apple
$basket -= [fruits]'apple'
$basket
Pomelo

$basket.value__如果有任何结果,将大于零。

two articles中提供了更多讨论。

答案 1 :(得分:1)

鉴于您的值用不同的变量$a$b(概念上是值的数组)表示,switch statement是您最好的选择:

# Sample values that are valid
$a = 'banana'
$b = 'apple'

# Collect the values to check in an array.
# (You could pass `$a, $b` directly to `switch` as well.)
$valuesToCheck = $a, $b

switch ($valuesToCheck) {
  'apple'  { 'My fruit is an apple.'; continue }
  'banana' { 'My fruit is a banana.'; continue }
  default  { Write-Warning "'$_' is not a known fruit." } 
}

以上结果:

My fruit is a banana.
My fruit is an apple.

例如,如果您将$b更改为无效值,例如$b = 'spinach',则会得到:

My fruit is a banana.
WARNING: 'spinach' is not a known fruit.

答案 2 :(得分:0)

您可以使用哈希表吗?

看这篇文章: https://social.technet.microsoft.com/Forums/ie/en-US/7064dc22-0611-4f4c-a846-dc24ed344389/comparing-hashtables?forum=winserverpowershell

创建两个哈希表并进行比较。 您可以添加更多键和值,而无需更改代码。

Hash1将是您具有所有正确值的模板,而Hash2将使用您要检查的所有值。

$Results = @()
$hash1 = @{"test1"="123";"test2"="abcd";"test3"="xyz"}
$hash2 = @{"test1"="123";"test2"="cdey";"test3"="xyz";"test4"="pdqy"}
$hash1.keys | %{
    if ($hash2.keys -match $_){
        if (!($hash2.$_ -match $hash1.$_)) {
            $Results += new-object psobject -property @{Hash="Hash1";Key=$_;Value=$hash1.$_}
        }
    } Else {$Results += new-object psobject -property @{Hash="Hash1";Key=$_;Value=$hash1.$_}}
}
$hash2.keys | %{
    if ($hash1.keys -match $_){
        if (!($hash1.$_ -match $hash2.$_)) {
            $Results += new-object psobject -property @{Hash="Hash2";Key=$_;Value=$hash2.$_}
        }
    } Else {$Results += new-object psobject -property @{Hash="Hash2";Key=$_;Value=$hash2.$_}}
}
$Results | select Hash,Key,Value | sort -property Key,Hash | FT

关于哈希表如何在Powershell中工作的简单说明 https://www.tutorialspoint.com/powershell/powershell_hashtables.htm