Christian helped me on the below and I have changed it a bit to check the value of the key as well. Anyway, the problem I now have is still based on the output that I provide to the user. I do not want to display the key name but rather the value of the key.
The value of the key is based on what key(s) have been found to be TRUE (in the code below).
So, if the below result has a TRUE against keys 1 (value of key is ONE), 2 (value of key is TWO) and 3 (value of key is THREE) then how do I put a WRITE-HOST to the user based on the specific keys found only?
ie I want the result that is provided to have an output of one of the below when it is all the parameters in a specific case are 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"
Here is the code:
$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
Why are you duplicating the Get-ItemProperty calls? You already have the value (or lack thereof) stored in $keyvalue. You should be able to simplify this to something like this: