Azure powershell-如何拉

Azure powershell-如何拉,azure,powershell,azure-powershell,Azure,Powershell,Azure Powershell,我有一个Azure powershell脚本,它拉取一个XML文件,然后通过“foreach”循环运行这些值 我在Azure存储中有一个blob文件,格式如下(XML)—— 这应该打印出来: 计数器位于1,子网为192.168.1.0/24 计数器位于2,子网为192.168.50.0/24 我该怎么做呢?但是不是XML,而是Json,您需要做的是下载Json文件内容作为字符串,然后使用convertfromJSON将其转换为Powershell对象 请按照以下代码操作: $url="https

我有一个Azure powershell脚本,它拉取一个XML文件,然后通过“foreach”循环运行这些值

我在Azure存储中有一个blob文件,格式如下(XML)——

这应该打印出来: 计数器位于1,子网为192.168.1.0/24 计数器位于2,子网为192.168.50.0/24


我该怎么做呢?但是不是XML,而是Json,您需要做的是下载Json文件内容作为字符串,然后使用
convertfromJSON
将其转换为Powershell对象

请按照以下代码操作:

$url="https://xxx.blob.core.windows.net/t1s/test.json"
$subnets=@()
$web_client = New-Object System.Net.WebClient
$download_json = $web_client.DownloadString($url)

#convert the download string to a PowerShell object
$json = $download_json | ConvertFrom-Json

$counter=0
foreach($subnet in $json.Root.PublicSubnets.Subnet)
{
$counter++
Write-Output "Counter is at $counter and subnet is $subnet"
}
test.json文件:

{
  "Root": {
    "PublicSubnets": {
      "Subnet": [
        "192.168.1.0/24",
        "192.168.50.0/24",
        "10.82.19.5/24",
        "10.1.1.0/16",
        "172.16.15.0/16"
      ]
    },
    "Descrip": { "Description": "\"This is the description\"" }
  }
}
测试结果:

更新

或者您可以使用
调用RestMethod-Uri$url
,它将自动解析对PSObject的json响应:


工作得很好。非常感谢。
$url="https://xxx.blob.core.windows.net/t1s/test.json"
$subnets=@()
$web_client = New-Object System.Net.WebClient
$download_json = $web_client.DownloadString($url)

#convert the download string to a PowerShell object
$json = $download_json | ConvertFrom-Json

$counter=0
foreach($subnet in $json.Root.PublicSubnets.Subnet)
{
$counter++
Write-Output "Counter is at $counter and subnet is $subnet"
}
{
  "Root": {
    "PublicSubnets": {
      "Subnet": [
        "192.168.1.0/24",
        "192.168.50.0/24",
        "10.82.19.5/24",
        "10.1.1.0/16",
        "172.16.15.0/16"
      ]
    },
    "Descrip": { "Description": "\"This is the description\"" }
  }
}