0

Christian 在下面帮助了我,我也对其进行了一些更改以检查密钥的值。无论如何,我现在遇到的问题仍然是基于我提供给用户的输出。我不想显示键名,而是显示键的值。

键的值基于已发现为 TRUE 的键(在下面的代码中)。

因此,如果下面的结果对键 1(键的值为 ONE)、2(键的值为 2)和 3(键的值为 3)为 TRUE,那么我如何将 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
4

2 回答 2

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 
于 2013-01-14T16:46:46.717 回答
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
于 2013-01-14T15:41:00.027 回答