我正在尝试使用 PowerShell 从 SOAP Web 服务中检索数据。我在从内部 XML 检索值时遇到问题。代码如下所示。
在[XML]$body2
变量中,如果我传递$queryid
变量,则函数else
条件中的值Response
不会打印。我调试了代码并$queryid
获得了正确的值。如果我删除$queryid
并用数字替换它,那么这些值打印得很好。
编辑:应该$queryid
设置为一个数字,然后我想在$body2
.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$id1 = "1"
$id2 = "2"
$global:queryid = ""
$array = @()
function Main() {
$uri = "https://mywebsite/CatalogTasks.asmx?WSDL"
[XML]$body = @"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope">
<soap:Body>
<Request xmlns="http://mywebsite.com">
<user>user_id</user>
<pwd>password</pwd>
<fields>[Number],Tech name],[Description],[State]</fields>
<condition>[State]='Open'</condition>
<orderBy>[Number]</orderBy>
</Request>
</soap:Body>
</soap:Envelope>
"@
Response $uri $body $id1
Write-Host "Id: $global:queryid"
$queryid = $global:queryid
[XML]$body2 = @"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope">
<soap:Body>
<RetrieveXML xmlns="http://mywebsite.com">
<user>user_id</user>
<pwd>password</pwd>
<queryId>$queryid</queryId>
</RetrieveXML>
</soap:Body>
</soap:Envelope>
"@
Response $uri $body2 $id2
}
function Response ($url, $bodyy, $id) {
$resp = (Invoke-WebRequest -Uri $url -Body $bodyy -ContentType "text/xml" -Proxy "http://proxy" -ProxyUseDefaultCredentials -Method POST)
if ($id -eq 1) {
$soap = $resp.Content
$xpathfilter = "//*[local-name()='QueryId']"
$Element = Select-Xml -Content $soap -XPath $xpathfilter
$global:queryid = $Element.Node.'#text'
} else {
$soap2 = $resp.Content
$filter = "//*[local-name()='Number']"
$Element2 = Select-Xml -Content $soap2 -XPath $filter
$number = $Element2.Node.'#text'
[array]$array = ($number)
foreach ($i in $array){
Write-Host $i
}
}
}
Main