2017-07-17 23 views
0

我有很多麻烦提出了一种方法来正确添加我想要的表的数据,我正在寻找一张表。然后,我想将该表格转换为HTML并通过电子邮件发送。为了举例,我正在寻找断开状态的事情,但我现在正在使用“连接”来测试它。添加到表格并以HTML格式发送

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}" 
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}" 
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }" 
$style = $style + "TD{border: 1px solid black; padding: 5px; }" 
$style = $style + "</style>" 

$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 


$table = @{} 


foreach($entry in 
$xmldata.DiscoveryConnectionListingResponse.DiscoveryConnectionSummary){ 
if($entry."connection-status" -eq "Connected") { 
      #add to table here 
} 
} 
$html= New-Object psobject -Property $table | ConvertTo-Html 



send-mailmessage -to "email" -from "email" -subject "Alert!" -BodyAsHtml "$html" -smtpserver server 

的数据,我需要添加到表:

$entry.name 
$entry.'connection-status' 
$entry.'engine-id' 

我只是不能图这一点 - 它的驾驶我疯了!任何帮助是极大的赞赏!我一直尝试不同的方法,它不会接受具有相同键的多个值。

回答

1

现在你传递一个空对象到ConvertTo-Html,所以它什么都不产生。你需要传递你的对象来获得你需要的表。

$HTML = $xmldata.DiscoveryConnectionListingResponse.DiscoveryConnectionSummary | 
    Where{$_.'connection-status' -eq 'connected'} | 
    ConvertTo-Html -As Table -Property Name,'Connection-Status','Engine-Id' -PreContent $Style 

send-mailmessage -to "email" -from "email" -subject "Alert!" -BodyAsHtml -body $html -smtpserver server