파워쉘의 조건문인 IF 를 통해 값을 비교하고 그 비교 결과에 따른 명령어를 수행할 수 있도록 지정할 수 있습니다.
하기의 내용을 실습하기 위해서 'PowerShell ISE' 를 실행시켜 작업해야겠습니다.
먼저 아래와 같이 입력해 보면 'IF' 문의 조건에 맞을때 뒤의 'Write-Host' 명령이 실행되는것을 확인할 수 있습니다.
if(100 -eq 100) { Write-Host "조건에 맞습니다." }
if(100 -eq 10) { Write-Host "조건에 맞습니다." }
IF문의 조건에 맞지 않을때 실행할 명령어는 'ELSE'문에 넣을 수 있습니다.
if(100 -eq 10) { Write-Host "조건에 맞습니다." } else { Write-Host "조건에 맞지 않습니다" }
조건을 연속으로 지정하여 특정 조건에 맞을때 특정 명령을 실행 할 수도 있습니다.
if(100 -eq 10) { Write-Host "첫번째 조건에 맞음" } elseif (100 -gt 10) { Write-Host "두번째 조건에 맞음" }
변수를 사용하면 아래와 같이 사용할 수 있습니다.
아래와 같이 길게 작성해야 하는 것은 'PowerShell' 보다 'PowerShell ISE' 를 사용하면 편리합니다.
[CmdletBinding()]
param
(
[parameter(Mandatory=$true)] [int]$this,
[Parameter(Mandatory=$true)] [int]$that
)
param
(
[parameter(Mandatory=$true)] [int]$this,
[Parameter(Mandatory=$true)] [int]$that
)
If ($this –eq $that) {
Write-Host "this 와 that 는 같습니다." -ForegroundColor Yellow
} ElseIf ($this –gt $that) {
Write-Host "this 는 that 보다 큽니다." -ForegroundColor Yellow
} Else{
Write-Host "this 가 that 보다 작습니다.." -ForegroundColor Yellow
}
'PowerShell' 카테고리의 다른 글
PowerShell 호스트명 확인 (0) | 2016.04.20 |
---|---|
PowerShell을 이용한 정책 확인 및 수정 (0) | 2016.03.30 |
PowerShell 조건문 Switch (0) | 2016.03.02 |
원격 Powershell 실행 (0) | 2016.03.02 |
PowerShell 변수 사용 (0) | 2016.02.29 |
PowerShell 리디렉션 출력 (0) | 2016.02.26 |
PowerShell 필터링 기능 (0) | 2016.02.26 |
PowerShell 비교 연산자 (0) | 2016.02.25 |