仅向用户输出TRUE结果

时间:2013-01-14 15:30:22

标签: powershell powershell-v2.0

克里斯蒂安在下面帮助了我,我已经改变了一点来检查钥匙的价值。无论如何,我现在遇到的问题仍然是基于我提供给用户的输出。我不想显示密钥名称,而是显示密钥的值。

密钥的值取决于已找到的密钥为TRUE(在下面的代码中)。

因此,如果下面的结果对键1的值为TRUE(键的值为1),2(键的值为TWO)和3(键的值为THREE)则如何将WRITE-HOST置于用户只根据找到的特定密钥?

即我希望在特定情况下的所有参数都为TRUE时,提供的结果具有以下之一的输出。

Write-Host "The value of the keys are $keyvalue1"

Write-Host "The value of the keys are $keyvalue1 and $keyvalue2"

Write-Host "The value of the keys are $keyvalue1 and $keyvalue2 and $keyvalue3"

Write-Host "The value of the keys are $keyvalue1 and $keyvalue2 and $keyvalue3 and $keyvalue4"

以下是代码:

$keyvalue1 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 1).'1'
$keyvalue2 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 2).'2'
$keyvalue3 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 3).'3'
$keyvalue4 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 4).'4'

$testpath1 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 1).'1' -ne $null 2>$null
$testpath2 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 2).'2' -ne $null 2>$null
$testpath3 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 3).'3' -ne $null 2>$null
$testpath4 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 4).'4' -ne $null 2>$null

 $a = $testpath1, $testpath2 , $testpath3 , $testpath4 # convert your true/false results in an array
$s = "" # empty string
$i = 1 # a simple variable as index
foreach ($b in $a)
 {
   if ($b -ne $true ) 
   { 
     break
   } 

   $s += "$i " # if true add index in string
   $i++
}

"$($s)is/are true" #output the result

2 个答案:

答案 0 :(得分:2)

为什么要复制Get-ItemProperty调用?您已经将值(或缺少值)存储在$ keyvalue中。您应该能够将其简化为以下内容:

$keyVals = @()
$keyVals += (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 1).1
$keyVals += (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 2).2
$keyVals += (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 3).3
$keyVals += (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 4).4

Write-Host "The value of keys are " -NoNewLine
$first = $true
foreach ($keyVal in $keyVals) {
    if ($keyVal -ne $null) {
        if (!$first) { 
            Write-Host " and " -NoNewLine 
        }
        $first = $false
        Write-Host $keyVal -NoNewLine
    }
}
Write-Host 

答案 1 :(得分:0)

试试这个(未经测试):

$keyvalue1 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 1).'1'
$keyvalue2 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 2).'2'
$keyvalue3 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 3).'3'
$keyvalue4 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 4).'4'
$testpath1 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 1).'1' -ne $null 2>$null
$testpath2 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 2).'2' -ne $null 2>$null
$testpath3 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 3).'3' -ne $null 2>$null
$testpath4 = (Get-ItemProperty hklm:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\$CommunityName -Name 4).'4' -ne $null 2>$null

 $a = $testpath1, $testpath2 , $testpath3 , $testpath4 # convert your true/false results in an array

$v = $keyvalue1,$keyvalue2,$keyvalue2,$keyvalue4 
$s = "The value of the keys are " # empty string
$i = 1 # a simple variable as index
foreach ($b in $a)
 {
   if ($b -ne $true ) 
   { 
     break
   } 

   $s += "$($v[$i]) " # if true add value of $v[index] in string
   $i++
}

"$s" #output the result