2011-10-11 109 views
1

我希望能够监控我的打印机状态网页,并在墨水量低于25%时通过脚本给我发送电子邮件。林相当肯定这可以在Powershell中完成,但我在如何做到这一点上的损失。Web浏览器在PowerShell监控页面

这是有问题的网页HTML:

<h2>Supply Status</h2> 

    <table class="matrix"> 
       <thead> 
    <tr> 
     <th>Supply Information</th> 
     <th>Status</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td>Black Toner</td> 
     <td>End of life</td> 
    </tr> 
    <tr> 
     <td>Cyan Toner</td> 

     <td>Under 25%</td> 
    </tr> 
    <tr> 
     <td>Magenta Toner</td> 
     <td>Under 25%</td> 
    </tr> 
    <tr> 

     <td>Yellow Toner</td> 
     <td>Under 25%</td> 
    </tr> 
    </tbody> 

    </table> 
    <p> 

感谢。

亚当

回答

1

最简单的方法很可能是HTML Agility Pack,你可以在PowerShell中导入。 Lee Holmes有一个简短的文章演示一个简单的例子。基本上你使用类似XML的API来访问HTML DOM。

+0

你能帮助我一点,我对这一切都很陌生。 – user989384

+2

我也是。通常我使用示例和文档来实现我所需要的。但现在已经有一段时间了,我使用这个库,所以除非我花费你应该花费的所有努力,否则我无法提供帮助。 – Joey

3

建立在@乔伊的答案,给这个旋转的HTML敏捷包。

$html = new-object HtmlAgilityPack.HtmlDocument 
$result = $html.Load("http://full/path/to/file.htm") 
$colors = $html.DocumentNode.SelectNodes("//table[@class='matrix']//tbody/tr") 
$result = $colors | % { 
    $color = $_.SelectSingleNode("td[1]").InnerText 
    $level = $_.SelectSingleNode("td[2]").InnerText 
    new-object PsObject -Property @{ Color = $color; Level = $level; } | 
     Select Color,Level 
} 
$result | Sort Level | ft -a 

这假定您已经将HTML Agility Pack加载到PowerShell中。矿在我的个人资料加载为:

[System.Reflection.Assembly]::LoadFrom( 
     (join-path $profileDirectory HtmlAgilityPack) 
     + "\HtmlAgilityPack.dll") | Out-Null 

使用HTML提供的例子,你的输出是这样的:

output from PowerShell script

在这一点上,你有输出,并且可以通过电子邮件发送出去。