mysql - 从 powershell 将防火墙规则添加到 Azure Database for MySQL 服务器

标签 mysql powershell azure firewall

我已经部署了 Azure Database 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" }
],

此代码不会引发任何错误,但它不会向资源添加规则。
我需要一个指向我犯错误的地方或一些文档,以及如何正确处理此类任务。

最佳答案

您可以使用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}

关于mysql - 从 powershell 将防火墙规则添加到 Azure Database for MySQL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49151368/

相关文章:

powershell - 如何在 PowerShell 中创建自定义类型以供我的脚本使用?

powershell - 输出的复制项 'preview'

c# - ASP.NET Core 2.2是否支持使用Microsoft帐户外部登录?

azure - 如何在同一个 Azure webrole 上配置 2 个 HTTPS 端点?

php - 如何在数据库搜索中获取单行数据

sql - 如何防止内部 SELECT 返回 NULL?

php - 在非对象上调用成员函数 rollback()

mysql - 如何结合左连接和减法?

powershell - 如何将这个不同的属性名称保存到单个公共(public)属性中?

azure - 为什么此 Azure 资源管理器模板验证失败?