Here's the whole script:
# get the web page. Yes, PowerShell has a 'curl' command/alias $resp = curl -UseDefaultCredentials http://myhostname/mypagename # Get all of the rows of the table with an ID of "serverTable" $rows = $resp.ParsedHtml.getElementById("serverTable").getElementsByTagName("TR") # Loop through the rows, skipping the header row: for ($i=1; $i -lt $rows.Length(); $i++) { # get the hostname of this row $thehost = $rows[$i].getElementsByTagName("TD")[2] # get the date this host was last rebooted $rebootDateString=$rows[$i].getElementsByTagName("TD")[6] # if the host was rebooted over 20 days ago, print that date if ([datetime]::Now.AddDays(-20) -gt [datetime]::parseexact($rebootDateString.innerText,"G", $null)) {
$rebootDateString.innerText } }
That's it, with no external references and nothing extra to install. It's got date parsing, date arithmetic, HTML parsing and HTTP request capabilities all built in. I realize that this then isn't portable... or is it? There's actually a PowerShell port for Linux available, with instructions here from Microsoft:
I know that Python is a hot language these days, but I don't like it as much as PowerShell. I tried to do the above with Python, and it took quite a bit longer, even though I've used Python more than PowerShell. You have to import some classes and then use XPath to find elements in the HTML. PowerShell was just straightforward and easy, at least for me, with my background and expectations. YMMV, but I like PowerShell.
No comments:
Post a Comment