繁体   English   中英

Powershell中调用Rest方法出错

[英]Error in Invoke Rest Method in Powershell

我正在尝试更新 azure 中的 ip 范围

这是我的代码:

Write-Host 'This is the current SNAP IP Range:' -ForegroundColor Green
Write-Output $SnatIpRange

#Calculates
# Example in adding new excluded IP range
$NewSnatIpRange = python .\Calc.py $SnatIpRange "192.168.0.1/24" 

Write-Host 'This is the new SNAP IP Range:' -ForegroundColor Green   
Write-Output $NewSnatIpRange

# Update a REST API with $NewSnatIpRange as the new IP range.
$body = @"

{
    "location" : "japaneast",
        "properties": {    
                "snat": {
                    "privateRanges": ["$NewSnatIpRange"]
            }
    
        }

}
"@
Invoke-RestMethod -Method Put -Body $body -Uri $restUri -Headers $authHeader
Write-Host 'SNAT is updated Successully' -ForegroundColor Green  

遇到错误:


Line |
  45 |  Invoke-RestMethod -Method Put -Body $body -Uri $restUri -Headers $aut …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | {   "status": "Failed",   "error": {     "code": "FirewallPolicySNATHasInvalidIps",     "message": "One of IPAdresses of SNAT PrivateRanges is invalid. Only IPv4 addresses are permitted."   } }

我试图替换这个:
$NewSnatIpRange 到 "10.0.0.0/8" "10.0.0.0/16"
PUT 方法成功地反映在站点中。

$body = @"

{
    "location" : "japaneast",
        "properties": {    
                "snat": {
                    "privateRanges": ["$NewSnatIpRange"]
            }
    
        }

}

我的问题是,我需要将计算的值放入 $NewSnatIpRange 中。
任何人都知道如何解决此错误?

SNAT PrivateRanges 的 IP 地址之一无效。 仅允许使用 IPv4 地址。

问题的原因与 PowerShell 脚本的主体格式有关。

当你在body中引用PowerShell参数时,可以使用如下格式:

$body = "

{
    `"location`" : `"japaneast`",
        `"properties`": {    
                `"snat`": {
                    `"privateRanges`": [`"$NewSnatIpRange`"]
            }
    
        }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM