Pegar el script de PowerShell de varias líneas funciona en PowerShell ISE pero falla en la consola de PowerShell

Pegar el script de PowerShell de varias líneas funciona en PowerShell ISE pero falla en la consola de PowerShell

Al pegar un script de varias líneas en PowerShell ISE, parece ejecutar todo el bloque de script a la vez.

Salida de 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:\> 

Al pegar un script de varias líneas en la consola PowerShell, ¿parece intentar ejecutarse línea por línea?

Salida de la consola 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:\>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

El comportamiento que espero de PowerShell Console es el mismo que el comportamiento en PowerShell ISE; debe saber que elsees parte de la ifdeclaración.

No recuerdo que alguna vez haya actuado de esa manera. ¿Qué está causando que este comportamiento sea diferente?

Respuesta1

Su script falla porque else {está en una línea diferente a la }anterior. agregue una comilla invertida para indicar que se está extendiendo a la siguiente línea o elimine el retorno de carro antes else:

if ($true) {
  Write-Output "yes"
} else {
  Write-Output "no"
}

información relacionada