以下は、レジストリ キーの存在をチェックし、"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"
}