Christian基於以下方式幫助我,並且我已對其進行了一些更改以檢查密鑰的值。無論如何,我現在遇到的問題仍然是基於我提供給用戶的輸出。我不想顯示密鑰名稱,而是顯示密鑰的值。僅向用戶輸出TRUE結果
密鑰的值基於哪些密鑰被發現爲TRUE(在下面的代碼中)。因此,如果以下結果對於關鍵字1(關鍵字的值爲1),關鍵字的值爲2和關鍵字的值爲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
爲什麼我複製它......因爲我不如你:-)非常感謝 - 你的代碼工作,我得到預期的結果。一個問題 - @()是做什麼的? – lara400
只需確保$ keyVals是一個數組 - 開始時爲空。 PowerShell非常簡單,不需要聲明。但是,如果返回的第一個值是一個字符串,那麼$ keyVals是一個字符串,後續使用'+ ='只是將這些值連接到字符串。 –