无法使用PowerShell打印内部XML值

时间:2018-09-04 15:46:44

标签: powershell web-services innerxml

我正在尝试使用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

0 个答案:

没有答案