0

以下は、レジストリ キーの存在をチェックし、"TRUE" の結果に基づいて、スクリプトにそれをキャプチャして処理させるにはどうすればよいですか?

例 -$testpath1常に存在します。ただし$testpath2, $testpath3$testpath4常に存在するとは限りません。したがって、スクリプトは、true を返す場合にのみ testpath1 を処理する必要があります。

ただし、$testpath2および$testpath3が存在する場合、スクリプトは$testpath3およびまで処理する必要があります。$testpath4

私は以下を持っていますが、問題は、最初のステートメントが真であると判断すると、そのステートメントのみを処理し、他のすべてのステートメントをバイパスすることです。スクリプトがすべてのステートメントを通過し、false を含むステートメントを通過してから、そのステートメントを無視して、その前のステートメントを処理することを本当に望んでいます。このようなものには IF ELSE は適用できないと思います-では、これには何を使用すればよいですか?

これがコードで、以下の結果は次のとおりです。

結果: 1 is true

ただし、$testpath1存在$testpath2する...

$CommunityName = Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration | ForEach-Object {Get-ItemProperty $_.pspath} | where-object {$_.PSChildName } | Foreach-Object {$_.PSChildName}

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

if ($testpath1 -eq 'TRUE')
{
Write-Host "1 is true"
}
elseif ($testpath1 -eq 'TRUE' -and $testpath2 -eq 'TRUE')
{
Write-Host "1 and 2 is true"
}
elseif ($testpath1 -eq 'TRUE' -and $testpath2 -eq 'TRUE' -and $testpath3 -eq 'TRUE')
{
Write-Host "1 and 2 and 3 are true"
}
elseif ($testpath1 -eq 'TRUE' -and $testpath2 -eq 'TRUE' -and $testpath3 -eq 'TRUE' -and $testpath4 -eq 'TRUE')
{
Write-Host "1 and 2 and 3 and 4 are true"
}
4

1 に答える 1

1

このようなものは、ニーズに応じて機能します。

 $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
于 2013-01-14T11:30:40.207 に答える