通过powershell脚本为REST POST命令设置XML请求数据格式

通过powershell脚本为REST POST命令设置XML请求数据格式,xml,rest,post,powershell,Xml,Rest,Post,Powershell,我成功地重用了come脚本代码,但我遇到的问题超出了我的能力范围,请原谅我,我不是一个脚本或xml专家。。。。非常感谢您的帮助 我认为POST命令需要与powershell结构相关的XML数据格式,特别是当XML标记缩进/具有多级时: sourceIpRange 目的地 波特兰 在xml标记数据处于同一级别的情况下,我还有其他函数在工作,即没有缩进,一切正常 function create_acl ($networkname,$newaclname,$newaclposition,$acla

我成功地重用了come脚本代码,但我遇到的问题超出了我的能力范围,请原谅我,我不是一个脚本或xml专家。。。。非常感谢您的帮助

我认为POST命令需要与powershell结构相关的XML数据格式,特别是当XML标记缩进/具有多级时:

  • sourceIpRange
  • 目的地
  • 波特兰
在xml标记数据处于同一级别的情况下,我还有其他函数在工作,即没有缩进,一切正常

function create_acl ($networkname,$newaclname,$newaclposition,$aclaction,$protoltype,$SourceIP,$SourceSubnetMask,$DestIP,$DestSubnetMask,$portRange,$port1,$port2,$direction){
Write-host "Creating new ACL rule [" -nonewline 
Write-host $newaclname -nonewline -ForegroundColor Yellow
Write-Host "]... " -nonewline 
$createacl = "<AclRule xmlns='http://cloud.net/schemas/network'>
    <name>" + $newaclname +"</name>
    <position>" + $newaclposition + "</position>
    <action>" + $aclaction + "</action>
    <protocol>" + $protoltype + "</protocol>
    <sourceIpRange>
        <ipAddress>" + $SourceIP + "</ipAddress>
        <netmask>" + $SourceSubnetMask + "</netmask>
   </sourceIpRange>
   <destinationIpRange>
        <ipAddress>" + $DestIP + "</ipAddress>
        <netmask>" + $DestSubnetMask + "</netmask>
   </destinationIpRange>
   <portRange>
        <type>" + $portRange + "</type>
        <port1>" + $port1 + "</port1>
        <port2>" + $port2 + "</port2>
   </portRange>
   <type>" + $direction + "</type>
</ACLRule>"
try{
    $out = post_xml ("/oec/0.9/" + $account.account.orgId + "/network/" + $networkbyname[$networkname] + "/aclrule") $createacl
    if($out.status.result -eq "SUCCESS"){
        Write-host "Done" -ForegroundColor Green
    }else{
        Write-host "Failed" -ForegroundColor Red
        Write-host $out.status.resultDetail "-" $out.status.resultCode -ForegroundColor Red
    }
}
catch [Net.WebException] {
    Write-host "Failed" -ForegroundColor Red
    Write-host "-" $error[0] 
    write-host "- 400 Bad request could mean the acl already exists"
    write-host "- Continuing anyway..."
}
get_networkinfo
}
结果是

PS C:\Users\mike\Cloud\Scripts> create_acl "Network-1" "Test" "150" "DENY" "TCP" "10.214.32.0" "255.255.255.0" "10.214.33.0" "255.255.255.0" "Range" "8080" "8081" "INSIDE_ACL"
Creating new ACL rule [Test]... Failed
Exception calling "UploadData" with "3" argument(s): "The remote server returned an error: (400) Bad Request."
如果我使用用于Chrome的Postman Rest客户端,XML数据如下所示

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AclRule xmlns="http://cloud.net/schemas/network">
<name>Test</name>
<position>150</position>
<action>DENY</action>
<protocol>TCP</protocol>
<sourceIpRange>
    <ipAddress>10.214.32.0</ipAddress>
    <netmask>255.255.255.0</netmask>
</sourceIpRange>
<destinationIpRange>
    <ipAddress>10.212.33.0</ipAddress>
    <netmask>255.255.255.0</netmask>
</destinationIpRange>
<portRange>
    <type>RANGE</type>
    <port1>8080</port1>
    <port2>8081</port2>
</portRange>
<type>INSIDE_ACL</type>
</AclRule>
谢谢
Mike

我认为您没有正确地通过字符串构造XML。您希望使用“此处字符串”,例如:


创建的XML看起来像什么?添加更多信息,谢谢你的提问,但是。。。您的代码声明“400错误请求可能意味着acl已经存在”我认为acl不存在,并且使用Postman Rest Client for Chrome可以正常工作?没那么傻,在删除变量时,我输入了错误的环境GUID,因此没有检查该环境。。。。。在更改这两项的大小写后,它将处理硬编码的XML数据和“范围”。正在引入变量,这里仍然存在问题。。。。。任何人都知道它的进展如何。谢谢
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AclRule xmlns="http://cloud.net/schemas/network">
<name>Test</name>
<position>150</position>
<action>DENY</action>
<protocol>TCP</protocol>
<sourceIpRange>
    <ipAddress>10.214.32.0</ipAddress>
    <netmask>255.255.255.0</netmask>
</sourceIpRange>
<destinationIpRange>
    <ipAddress>10.212.33.0</ipAddress>
    <netmask>255.255.255.0</netmask>
</destinationIpRange>
<portRange>
    <type>RANGE</type>
    <port1>8080</port1>
    <port2>8081</port2>
</portRange>
<type>INSIDE_ACL</type>
</AclRule>
function create_acl {
Write-host "Creating new ACL rule [" -nonewline 
Write-host $newaclname -nonewline -ForegroundColor Yellow
Write-Host "]... " -nonewline 
$createacl = "<AclRule xmlns='http://cloud.net/schemas/network'>
  <name>Test</name>
  <position>150</position>
  <action>DENY</action>
  <protocol>TCP</protocol>
  <sourceIpRange>
      <ipAddress>10.214.32.0</ipAddress>
      <netmask>255.255.255.0</netmask>
  </sourceIpRange>
  <destinationIpRange>
      <ipAddress>10.212.33.0</ipAddress>
      <netmask>255.255.255.0</netmask>
  </destinationIpRange>
  <portRange>
      <type>RANGE</type>
      <port1>8080</port1>
      <port2>8081</port2>
  </portRange>
  <type>INSIDE_ACL</type>
</AclRule>" 
}
try{
    $out = post_xml ("/oec/0.9/" + $account.account.orgId + "/network/89e8ecc4-6c86-11e2-9153-001b21cfdbe0/aclrule") $createacl
    if($out.status.result -eq "SUCCESS"){
        Write-host "Done" -ForegroundColor Green
    }else{
        Write-host "Failed" -ForegroundColor Red
        Write-host $out.status.resultDetail "-" $out.status.resultCode -ForegroundColor Red
    }
}
catch [Net.WebException] {
    Write-host $_.Exception.ToString()
    }

get_networkinfo
PS C:\Users\mike\Cloud\Scripts> create_acl
Creating new ACL rule []... System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
at CallSite.Target(Closure , CallSite , Object , Object , String , Byte[] )
Getting Network Information... Done
$createacl = @"
<AclRule xmlns='http://cloud.net/schemas/network'>
   <name>$newaclname</name>
   <position>$newaclposition</position>
   <action>$aclaction</action>
   <protocol>$protoltype</protocol>
   <sourceIpRange>
        <ipAddress>$SourceIP</ipAddress>
        <netmask>$SourceSubnetMask</netmask>
   </sourceIpRange>
   <destinationIpRange>
        <ipAddress>$DestIP</ipAddress>
        <netmask>$DestSubnetMask</netmask>
   </destinationIpRange>
   <portRange>
        <type>$portRange</type>
        <port1>$port1</port1>
        <port2>$port2</port2>
   </portRange>
   <type>$direction</type>
</AclRule>
"@
$xml = [xml]$createacl