question

SakeriyeMohamud-4520 avatar image
0 Votes"
SakeriyeMohamud-4520 asked cooldadtx commented

help on an If statement

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

:@cooldadtx - so it claify the if statement is going to be like this - if it exist, delete it

$appSvc = az webapp list -g $spokeRG.name | ConvertFrom-Json
if (!$appSvc) {
az webapp delete --ids $appSvc.id
}

0 Votes 0 ·
cooldadtx avatar image cooldadtx SakeriyeMohamud-4520 ·

Swap the if logic to be positive. If you get an object back then the condition is true.

if ($appSvc) {
   az webapp delete ...
}
0 Votes 0 ·