Check if a command has run successfully

前端 未结 4 1532
轮回少年
轮回少年 2020-12-05 04:46

I\'ve tried enclosing the following in an if statement so I can execute another command if this succeeds:

Get-WmiObject -Class Win32_Share -ComputerName $Ser         


        
4条回答
  •  半阙折子戏
    2020-12-05 04:54

    Or if a failure returns no standard output, that would work for an if statement:

    if (! (Get-CimInstance Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'")) { 
      'command failed'
    }
    

    Also there's now the or symbol "||" in powershell 7:

    Get-CimInstance Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'" || 'command failed'
    

提交回复
热议问题