在Powershell中处理来自WebRequest的XML数据

时间:2017-07-17 19:17:50

标签: xml rest powershell

<div expr:class='"post hentry grid-item" + (data:post.labels any (l => l.name !="" : " " + l.name)' itemprop='blogPost' itemscope='itemscope' itemtype='http://schema.org/BlogPosting'>

这将返回约30个:

$uri = "https://${server}/api/${api_version}/xml"
$login_request = "<LoginRequest password ='$pwd' user-id = '$user' ></LoginRequest>"
$resp = Invoke-WebRequest -URI $uri -Body $login_request -ContentType 'text/xml' -Method post
[xml]$xmldata = $resp.content
if($xmldata.LoginResponse.success -eq '0'){
    Write-Host 'ERROR: '$xmldata.LoginResponse.Failure.message -ForegroundColor Red
    }
    Else{
    $SCRIPT:session_id = $xmldata.LoginResponse.'session-id'
    Write-Host "Login Successful" -ForegroundColor Green
    }
$disc_request = "<DiscoveryConnectionListingRequest session-id='$SCRIPT:session_id'/>"
$resp_disc = Invoke-WebRequest -URI $uri -Body $disc_request -ContentType 'text/xml' -Method post
[xml]$xmldata = $resp_disc.content
$xmldata.DiscoveryConnectionListingResponse.DiscoveryConnectionSummary

我很难确定如何查看这些数据,并弄清楚是否有任何“连接状态”。已断开连接&#39;。我会使用相同的逻辑来确定引擎ID是0还是1.这里的任何人都能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

Hackerman的评论将过滤掉已断开连接的connection-status

$xmldata.DiscoveryConnectionListingResponse.DiscoveryConnect‌​ionSummary | Where-Object {$_."connection-status" -eq "Disconnected" }

请记住,您可以在PowerShell中轻松遍历xml节点:

foreach($node in $xmldata.DiscoveryConnectionListingResponse.DiscoveryConnectionSummary){

    if($node."connection-status" -eq "disconnected"{
        # do something
        Write-Output "ID $($node.id) with engine ID $($node."engine-id") is disconnected"
    }else{
        # do something else
    }

}