Can I get “&&” or “-and” to work in PowerShell?

后端 未结 12 908
南笙
南笙 2020-11-29 17:33

&& is notoriously hard to search for on Google Search, but the best I\'ve found is this article which says to use -and.

Unfortunatel

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 18:19

    I think a simple if statement can accomplish this. Once I saw mkelement0's response that the last exit status is stored in $?, I put the following together:

    # Set the first command to a variable
    $a=somecommand
    
    # Temporary variable to store exit status of the last command (since we can't write to "$?")
    $test=$?
    
    # Run the test
    if ($test=$true) { 2nd-command }
    

    So for the OP's example, it would be:

    a=(csc /t:exe /out:a.exe SomeFile.cs); $test = $?; if ($test=$true) { a.exe }
    

提交回复
热议问题