私はスクリプト コードを再利用することができましたが、私が遭遇した問題は私を超えています。私はスクリプトや xml の専門家ではないことを許してください....どんな助けも大歓迎です!
特にXMLタグがインデントされている/マルチレベルである場合、powershell構造に関連してPOSTコマンドが必要とするのはXMLデータの形式だと思います。
- ソースIpRange
- 宛先IPRange
- ポート範囲
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
}
コマンドは次の方法で実行されます
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"
結果は
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."
Postman Rest Client for Chrome を使用すると、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
ありがとうマイク