如何将 Json 参数传递给 powershell 脚本

How to pass Json parameters to powershell script

我想将 Json 数据传递给 Powershell 脚本。

PowerShell 脚本:

Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" |输出文件“file.json”

New-AzMySqlFirewallRule -Name “” -ResourceGroupName “dev” -ServerName “dev-core” -EndIPAddress “” -StartIPAddress “”

在上面的 powershell 脚本中,我需要从提到的 Json 文件中获取“”的值 below.So 如何在 运行 时间和所有期间获取 Json 参数值应将 3 个参数传递给上述命令,以便它将为新的数据库服务器创建新的防火墙规则。

此外,当我 运行 powershell 命令(Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Out-File "file.json “) 我在下面 format.Not 中获取我的 Json 文件数据,确定这种格式是否看起来不错,但我需要以下值从 pdbr_home、1.2.3.4 和5.6.7.8 和类似的另外 2 行数据应该传递到我的 powershell 命令这里 New-AzMySqlFirewallRule -Name “” -ResourceGroupName “dev” -ServerName “dev-core” -EndIPAddress “” -StartIPAddress “” . 通过 for 循环。

file.Json:

      [
        {
        "EndIPAddress": "1.3.2.2",
        "Id": "/subscriptions/abcdefg/resourceGroups/dev/providers/Microsoft.DBforMySQL/servers/db-dev-  vm/firewallRules/praveen_Home",
        "Name": "praveen_Home",
        "StartIPAddress": "4.3.1.2",
        "Type": "Microsoft.DBforMySQL/servers/firewallRules"
      },
      {
        "EndIPAddress": "2.4.5.6",
        "Id": "/subscriptions/abcdefg/resourceGroups/dev/providers/Microsoft.DBforMySQL/servers/db-dev-  vm/firewallRules/pdbr_Home",
        "Name": "pdbr_Home",
        "StartIPAddress": "3.2.1.2",
        "Type": "Microsoft.DBforMySQL/servers/firewallRules"
      }
    ]

下面的命令输出结果如下

PS /home/praveen> 获取命令 json

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          ConvertFrom-Json                                   7.0.0.0    Microsoft.PowerShell.Utility
Cmdlet          ConvertTo-Json                                     7.0.0.0    Microsoft.PowerShell.Utility
Cmdlet          Test-Json                                          7.0.0.0    Microsoft.PowerShell.Utility
Application     json_pp                                            0.0.0.0    /usr/bin/json_pp
Application     json_pp                                            0.0.0.0    /bin/json_pp

错误:

错误:

New-AzMySqlFirewallRule: /home/praveen/dbtest.ps1:21
Line |
  21 |  …  -ServerName "praveen-dev" -EndIPAddress $entry.EndIPAddress -StartI …
     |                                              ~~~~~~~~~~~~~~~~~~~
     | Cannot bind argument to parameter 'EndIPAddress' because it is an empty string.

最终解决方案现在对我有用:

##################### 正在将防火墙规则从源数据库服务器更新到目标数据库服务器############# #####

Write-Host -NoNewline "Updating Firewall rules from Soiurce DB server to Target DB server"
Get-AzMySqlFirewallRule -ResourceGroupName $ResourceGroupName -ServerName $SourceDBServerName | Select-Object Name, StartIPaddress, EndIPaddress | Convertto-Json | Out-File "firewallrule.json"
foreach ($frule in (Get-Content firewallrule.json -raw | ConvertFrom-Json)) {
  New-AzMySqlFirewallRule -Name $frule.Name -ResourceGroupName $ResourceGroupName -ServerName $TargetDBServerName -EndIPAddress $frule.EndIPAddress -StartIPAddress $frule.StartIPAddress
}

写入文件前使用Convertto-Json。 文档:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7.2

Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Convertto-Json | Out-File "file.json"

按要求编辑:

Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Select-Object Name,"StartIP address", "EndIP address"| Convertto-Json | Out-File "file.json"

根据要求进行另一次编辑(修正了我的错误 - 谢谢@sage pourpre):

foreach ($entry in (Get-Content file.json -raw | ConvertFrom-Json) {
    New-AzMySqlFirewallRule -name $entry.Name `
    -ResourceGroupName "dev" `
    -ServerName "dev-core" `
    -StartIPAddress $entry.StartIPAddress `
    -EndIPAddress $entry.EndIPAddress
}

试试下面的方法:

Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server" | Convertto-Json | Out-File "file.json"

$data = Get-Content "C:\Users\me\file.json" | Out-String | ConvertFrom-Json #replace path to where you have exported the json file


foreach ($line in $data) {
    New-AzMySqlFirewallRule -name $line.Name `
    -ResourceGroupName "dev" `
    -ServerName "dev-core" `
    -StartIPAddress $line.StartIPAddress `
    -EndIPAddress $line.EndIPAddress
}

一种不同的方法,尽管问题是针对 JSON,只是将规则存储在一个变量中。

        $rules = Get-AzMySqlFirewallRule -ResourceGroupName "dev" -ServerName "dev-DB-Server"
    
    foreach ($rule in $rules){
        New-AzMySqlFirewallRule -name $rule.Name `
            -ResourceGroupName "dev" `
            -ServerName "dev-core" `
            -StartIPAddress $rule.StartIPAddress `
            -EndIPAddress $rule.EndIPAddress
}

几天前,我能够在上述所有输入的帮助下修复此解决方案。感谢 all.Really 感谢您的帮助。

正在将防火墙规则从源数据库服务器更新到目标数据库服务器

Write-Host -NoNewline "Updating Firewall rules from Soiurce DB server to Target DB server"
Get-AzMySqlFirewallRule -ResourceGroupName $ResourceGroupName -ServerName $SourceDBServerName | Select-Object Name, StartIPaddress, EndIPaddress | Convertto-Json | Out-File "firewallrule.json"
foreach ($frule in (Get-Content firewallrule.json -raw | ConvertFrom-Json)) {
  New-AzMySqlFirewallRule -Name $frule.Name -ResourceGroupName $ResourceGroupName -ServerName $TargetDBServerName -EndIPAddress $frule.EndIPAddress -StartIPAddress $frule.StartIPAddress
}