从powershell向Azure数据库for MySQL服务器添加防火墙规则

时间:2018-03-07 11:57:27

标签: mysql powershell azure firewall

我已部署Azure数据库for MySQL服务器,我想为其添加防火墙规则。我需要从PowerShell执行此操作,因为此步骤是更大解决方案的一部分。我尝试了以下代码:

$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

$MySQLServer.FirewallRules来自json文件的格式如下:

"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" }
],

此代码不会抛出任何错误,但它不会向资源添加规则 我需要一个指向错误或某些文档的指针,如何正确处理这样的任务。

1 个答案:

答案 0 :(得分:2)

您可以使用New-AzureRmResource命令添加Mysql防火墙规则:

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}