貼上多行 PowerShell 腳本在 PowerShell ISE 中有效,但在 PowerShell 控制台中失敗

貼上多行 PowerShell 腳本在 PowerShell ISE 中有效,但在 PowerShell 控制台中失敗

將多行腳本貼到 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 控制台時,它似乎嘗試逐行執行?

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"
}

相關內容