Azure - Managing errors PS Script : Set-AzNetworkSecurityGroup

Badr HOUSSNI 46 Reputation points
2020-10-05T14:26:48.493+00:00

Hi,

I am trying to handle efficiently errors within my PS Script.
I am experiencing some issues doing this with this command : Set-AzNetworkSecurityGroup
Try catching errors doesn't worked for me.
the best solution so far i've founded was to do this :

$output = Set-AzNetworkSecurityGroup -NetworkSecurityGroup $nsg
if($output)
   { # All Ok }
else { # Something went wrong }

My solution is working so far but doesn't raise errors with some Error Status code, for example :
StatusCode: 400
ReasonPhrase: Bad Request

Does anyone have a solution to make sure to handle all errors of this command ?

thank you.

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,024 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. suvasara-MSFT 9,981 Reputation points
    2020-10-05T18:03:07.287+00:00

    @Badr HOUSSNI , 400 is the generic client-side error status, used when no other 4xx error code is appropriate. In case of 400 error, you need to go through the response content manually else use PowerShell to retrieve the response content message. Here is the logic to retrieve response content using PowerShell when server returns status code 400.

    ----------

    Please do not forget to "Accept the answer" wherever the information provided helps you to help others in the community.