2011-10-11 116 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

在這一點上,你有輸出,並且可以通過電子郵件發送出去。