Michał Herman
Michał Herman

Reputation: 3587

Add firewall rule to Azure Database for MySQL server from powershell

I have deployed Azure Database for MySQL server and I want to add a firewall rule to it. I need to do it from PowerShell because this step is part of the greater solution. I tried the following code:

$resource = Get-AzureRmResource -ResourceGroupName $ResourceGroup.Variables.ResourceGroup `
                                -ResourceType "Microsoft.DBforMySQL/servers" -ResourceName $MySQLServer.ResourceName
$props = $resource.Properties
$props | Add-Member @{ipV4FirewallSettings = [ordered] @{ "firewallRules" = @() } }
$props.ipV4FirewallSettings.firewallRules = $MySQLServer.FirewallRules
$props | Add-Member @{administratorLoginPassword = "Qwerty123!" }
Set-AzureRmResource -PropertyObject $props -ResourceGroupName $ResourceGroup.Variables.ResourceGroup `
                    -ResourceType "Microsoft.DBforMySQL/servers" -ResourceName $MySQLServer.ResourceName  -Force

Where $MySQLServer.FirewallRules are from json file in the following format:

"FirewallRules" : [
  { "firewallRuleName" : "test", "rangeStart": "0.0.0.0", "rangeEnd": "0.0.0.0" },
  { "firewallRuleName" : "test2", "rangeStart": "0.0.0.1", "rangeEnd": "255.255.255.255" }
],

This code does not throw any error, but it's not adding rules to the resource.
I need a pointer to where I made a mistake or some documentation, how to handle such task properly.

Upvotes: 0

Views: 484

Answers (1)

Jason Ye
Jason Ye

Reputation: 13954

You can use New-AzureRmResource command to add Mysql firewall rules:

PS C:\Users\jason> $b = New-Object Psobject -Property @{startIpAddress="172.0.0.1" ; endIpAddress="172.0.0.8"}
PS C:\Users\jason> $b

startIpAddress endIpAddress
-------------- ------------
172.0.0.1      172.0.0.8


PS C:\Users\jason> New-AzureRmResource -ResourceId "/subscriptions/b83c1ed3-xxxx-xxxx-xxxx-2b83a074c23f/resourceGroups/jasonmysql/providers/Microsoft.DBforMySQL/servers/jasonmysql/firewallRules/rule2" -Properties $b -ApiVer
sion 2017-04-30-preview -Force

Name              : rule2
ResourceId        : /subscriptions/b83c1ed3-xxxx-xxxx-xxxx-2b83a074c23f/resourceGroups/jasonmysql/providers/Microsoft.DBforMySQL/servers/jasonmysql/firewallRules/rule2
ResourceName      : jasonmysql/rule2
ResourceType      : Microsoft.DBforMySQL/servers/firewallRules
ResourceGroupName : jasonmysql
SubscriptionId    : b83c1ed3-xxxx-xxxx-xxxx-2b83a074c23f
Properties        : @{startIpAddress=172.0.0.1; endIpAddress=172.0.0.8}

Upvotes: 2

Related Questions