g.pickardou
g.pickardou

Reputation: 35973

How to check whether AzureRmNetworkSecurityRuleConfig exists?

I would like to automate configuring and reconfiguring Azure resource manager Network Security Group's inbound and outbound security rules.

So I have to check if a rule is exist then using appropriate

Add-AzureRmNetworkSecurityRuleConfig...

or

Set-AzureRmNetworkSecurityRuleConfig...

My guess was:

if (Get-AzureRmNetworkSecurityRuleConfig... )

but this unfortunately throws error in case the named rule does not exist yet.

Upvotes: 0

Views: 1212

Answers (1)

Michael B
Michael B

Reputation: 12228

You could use something like this -

$rg = Get-AzureRmNetworkSecurityGroup
$rules = Get-AzureRmNetworkSecurityRuleConfig -NetworkSecurityGroup $rg 

foreach ($rule in $rules)
{

    if ($rule.name  -like "RDP1")
    {
        "rule exist" 
    } else {
        "rule not exist" 
    }

}

Obviously you would swap out the "RDP" for whatever rule name you need. But that will give you a true / false for whether that rule exists in that Security Group.

You could filter the Get-AzureRmNetworkSecurityGroup to specify the exact Security Group you want to check.

And instead of $rule.Name you could use any other parameter (or multiple) to narrow down the selection (destinationPortRange for instance)

Upvotes: 1

Related Questions