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