if-문에 여러 조건 결합
한 세트 또는 두 조건의 다른 세트가 참이 되기를 원할 때 네 조건을 어떻게 연결합니까?
좀 더 정확히 말하자면, 저는 다음을 하고 싶습니다.
사용자가 로그인하고 운영 체제 버전이 Windows 10인 경우
OR
사용자가 로그인되어 있고 로그온UI 프로세스가 실행되고 있지 않습니다.
명령을 사용할 필요는 없습니다. 격리된 상태에서 모두 올바르게 작동합니다. 제 문제는 명령을 함께 연결하는 것입니다.
예를 들어 다음과 같습니다.
if (
(Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
-and`
(Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"
)
{ echo do X }
그것은 잘 작동하고 있습니다.그 안에 추가하고 싶습니다.if조건의 다른 부분.시도해봤는데 작동이 안 돼요.
if (
(Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
-and`
(Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"`
-or`
(Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
-and -not`
(Get-Process -ErrorAction SilentlyContinue -ComputerName $poste -name logonui)
)
{ echo do X }
이렇게 여러 "블록"을 연결하는 방법은 무엇입니까?나는 두 가지 다른 IF를 할 수 있다는 것을 알고 있습니다. 나는 그것을 작동시켰지만 그것을 모두 하나의 IF로 묶을 수 있는 방법이 없을까요? (IF에는 많은 코드가 포함되어 있고 나는 그것을 두 개의 IF로 복제하고 싶지 않습니다.)
각 조건 집합을 괄호 안에 넣습니다.
if ( (A -and B) -or (C -and D) ) {
echo do X
}
첫 번째 또는 두 번째 조건 집합 중 하나가 참이어야 하는 경우(둘 다 해당되지는 않음),-xor대신에-or:
if ( (A -and B) -xor (C -and D) ) {
echo do X
}
A, B, C 및 D를 각 식으로 바꿉니다.
만약 당신이 당신 자신의 대답에 있는 코드를 더 쉽게 이해하고 싶다면, 당신은 중복된 코드를 제거하여 다음을 만들 수 있습니다.if문 청소기.
결과를 변수에 할당하고 대신 사용:
$UserName = Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem | select -ExpandProperty UserName
$WindowsVersion = Get-WmiObject -Computer $poste -Class Win32_OperatingSystem | select -ExpandProperty Version
$LogonuiProcess = Get-Process -name logonui -ComputerName $poste -ErrorAction SilentlyContinue
그러면 다음 중 하나:
if (($UserName -and $WindowsVersion -like "*10*") -or ($UserName -and -not $LogonuiProcess)) {Write-Output "do X"}
또는
if ($UserName -and $WindowsVersion -like "*10*") {Write-Output "do X"}
elseif ($UserName -and -not $LogonuiProcess) {Write-Output "do Y"}
그래서 몇 가지를 시도해 본 결과, 두 가지 방법이 있는 것 같습니다.
if (
(Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
-and`
(Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"`
) { echo do X }
elseif (
(Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
-and -not`
(Get-Process -ErrorAction SilentlyContinue -ComputerName $poste -name logonui)
) { echo do X }
또는 Ansgar Wiechers의 훌륭한 답변을 사용하여 모든 것을 하나로 묶는 경우:
if (
(
(Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
-and`
(Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"`
) -or`
(
(Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
-and -not`
(Get-Process -ErrorAction SilentlyContinue -ComputerName $poste -name logonui)`
)
) { echo do X }
언급URL : https://stackoverflow.com/questions/50134318/combine-multiple-conditions-in-if-statement
'programing' 카테고리의 다른 글
| C에서 "-1>>5;"가 지정되지 않은 동작입니까? (0) | 2023.08.27 |
|---|---|
| Angular 5 및 재료 - 스낵바 구성 요소에서 배경색 변경 방법 (0) | 2023.08.27 |
| UIBarButtonItem: target-action이 작동하지 않습니까? (0) | 2023.08.27 |
| Oracle XE 인스턴스의 SID를 변경하는 방법 (0) | 2023.08.27 |
| CSS를 사용하여 글꼴을 굵게 만드는 방법은 무엇입니까? (0) | 2023.08.27 |