Share via

help on an If statement

ZM 211 Reputation points
2022-04-11T13:51:44.213+00:00

im kind of struggling and need a bit of help

im trying to make a if statement for these 2 questions below, but I keep doing it wrong - could I get some help:

  1. can I get a if statement for az webapp list to check it it exist, and then delete
    $appSvc = az webapp list -g $spokeRG.name | ConvertFrom-Json
  2. can I get a if statement for vnet to check it it exist, and then delete
    $appSvcVNET = az webapp vnet-integration list -n $appSvc.name -g $appSvc.resourcegroup | ConvertFrom-Json
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

1 answer

Sort by: Most helpful
  1. Michael Taylor 61,226 Reputation points
    2022-04-11T14:24:09.727+00:00

    For pure PS then an if statement is pretty straightforward.

    $appSvc = az webapp ...
    if (!$appSvc) {
       # Something went wrong
    }
    

    Unfortunately this doesn't work with az because it generates its own errors anyway. Even PS error handling doesn't work properly here. There is a blog article that discusses how you can work around this partially in PS. In my experience you'll still see the error from PS but you should be able to handle the error programmatically anyway.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.