여러 줄의 스크립트를 PowerShell ISE에 붙여넣으면 전체 스크립트 블록이 한 번에 실행되는 것 같습니다.
PowerShell ISE의 출력
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\> if ($true) {
Write-Output "yes"
}
else {
Write-Output "no"
}
yes
PS C:\>
여러 줄 스크립트를 PowerShell 콘솔에 붙여넣으면 Line-By-Line을 실행하려는 것 같나요?
PowerShell 콘솔의 출력
C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
C:\> if ($true) {
>> Write-Output "yes"
>> } yes
C:\> else {
>> Write-Output "no"
>> } else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ else {
+ ~~~~
+ CategoryInfo : ObjectNotFound: (else:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
C:\>
PowerShell 콘솔에서 기대하는 동작은 PowerShell ISE의 동작과 동일합니다. 이는 명령문 else
의 일부라는 것을 알아야 합니다 if
.
나는 이것이 그런 식으로 행동한 기억이 없습니다. 이 동작이 달라지는 원인은 무엇입니까?
답변1
else {
위의 줄과 다른 줄에 있기 때문에 스크립트가 실패합니다 }
. 백틱을 추가하여 다음 줄로 확장됨을 나타내거나 다음 줄 앞에 캐리지 리턴을 제거합니다 else
.
if ($true) {
Write-Output "yes"
} else {
Write-Output "no"
}