Microsoft Virtualization Discussions
Microsoft Virtualization Discussions
I was asked to produce a script to monitor our filers recently and, in doing so, discovered how handy the ConvertTo-HTML cmdlet can be when paired with some nice CSS code.
The following script will produce a nicely formatted HTML page containing important info on the filer's physical disks
Save it as something like Get-NADisks and run it with the following syntax
Get-NADisks -filer <filername/filerIP> -Username <userID> -password <password> -HTMLFile <output file>
[CmdletBinding()]
Param
(
[parameter(Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string]$Filer,
[parameter(Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string]$Username,
[parameter(Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string]$Password,
[parameter(Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string]$HTMLFile
)
Function Set-AlternatingRows {
<#
.SYNOPSIS
Simple function to alternate the row colors in an HTML table
.DESCRIPTION
This function accepts pipeline input from ConvertTo-HTML or any
string with HTML in it. It will then search for <tr> and replace
it with <tr class=(something)>. With the combination of CSS it
can set alternating colors on table rows.
CSS requirements:
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
Classnames can be anything and are configurable when executing the
function. Colors can, of course, be set to your preference.
This function does not add CSS to your report, so you must provide
the style sheet, typically part of the ConvertTo-HTML cmdlet using
the -Head parameter.
.PARAMETER Line
String containing the HTML line, typically piped in through the
pipeline.
.PARAMETER CSSEvenClass
Define which CSS class is your "even" row and color.
.PARAMETER CSSOddClass
Define which CSS class is your "odd" row and color.
.EXAMPLE $Report | ConvertTo-HTML -Head $Header | Set-AlternateRows -CSSEvenClass even -CSSOddClass odd | Out-File HTMLReport.html
$Header can be defined with a here-string as:
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
"@
This will produce a table with alternating white and grey rows. Custom CSS
is defined in the $Header string and included with the table thanks to the -Head
parameter in ConvertTo-HTML.
.NOTES
Author: Martin Pugh
Twitter: @thesurlyadm1n
Spiceworks: Martin9700
Blog: www.thesurlyadmin.com
Changelog:
1.1 Modified replace to include the <td> tag, as it was changing the class
for the TH row as well.
1.0 Initial function release
.LINK
.LINK
http://thesurlyadmin.com/2013/01/21/how-to-create-html-reports/
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory,ValueFromPipeline)]
[string]$Line,
[Parameter(Mandatory)]
[string]$CSSEvenClass,
[Parameter(Mandatory)]
[string]$CSSOddClass
)
Begin {
$ClassName = $CSSEvenClass
}
Process {
If ($Line.Contains("<tr><td>"))
{ $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
If ($ClassName -eq $CSSEvenClass)
{ $ClassName = $CSSOddClass
}
Else
{ $ClassName = $CSSEvenClass
}
}
Return $Line
}
}
Function Convert-Bytes
{
#Convert bytes to KB/MB/GB/TB (Rounded up and no decimal places)
Param(
[Parameter(Mandatory,ValueFromPipeline)]
[int64]$sizeInBytes
)
Begin {}
Process {
Switch ($sizeInBytes)
{
{$sizeInBytes -ge 1TB} {"{0:n$sigDigits}" -f "{0:N0}" -f [System.Math]::Round(($sizeInBytes/1TB)) + " TB" ; break}
{$sizeInBytes -ge 1GB} {"{0:n$sigDigits}" -f "{0:N0}" -f [System.Math]::Round(($sizeInBytes/1GB)) + " GB" ; break}
{$sizeInBytes -ge 1MB} {"{0:n$sigDigits}" -f "{0:N0}" -f [System.Math]::Round(($sizeInBytes/1MB)) + " MB" ; break}
{$sizeInBytes -ge 1KB} {"{0:n$sigDigits}" -f "{0:N0}" -f [System.Math]::Round(($sizeInBytes/1KB)) + " KB" ; break}
Default { "{0:n$sigDigits}" -f $sizeInBytes + " Bytes" }
}
}
}
Function Check-Disks
{
Begin {
$DiskDetails = @()
}
Process {
#Loops through the disks and puts import information in a PSObject
ForEach ($Disk in Get-NaDisk)
{
[string]$Diskname = $Disk.Name
[string]$DiskStatus = $Disk.Status
[string]$DiskErrorDetails = $Disk.BrokenDetails
$DiskObj=New-Object psobject -Property @{
Name = $DiskName
Shelf = $Disk.Shelf
Bay = $Disk.Bay
Pool = $Disk.Pool
Plex = $Disk.Plex
Model = $Disk.Model
Serial = $Disk.SerialNumber
'Disk Type' = $Disk.EffectiveDiskType
'Disk Status' = $Disk.Status
Size = $Disk.PhysicalSpace|Convert-Bytes
}
$DiskDetails += $DiskObj
}
[string]$DisksInfoHTML = $DiskDetails|select name,'Disk status',Size,shelf,bay,Pool,Plex,model,serial,'Disk type'|`
ConvertTo-html -head $HTMLHeader -As TABLE -Body "<h2 align=""center"">Disks Status</h2>" -PostContent "<br><b>$ErrorList</b>" |`
foreach {$_ -replace '&','&'}|Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd|Out-File $HTMLFile
}
}
#Define a CSS for the HTML
[string]$HTMLHeader = @'
<style>
body { font-family:Verdana;
font-size:8pt;
background-color:#000050;}
h2 {color:white;
text-align:center;
text-decoration:underline;
font-size:18pt;}
th { color:yellow;
background-Color:black; }
table, tr, td {padding: 2px; margin: 0px;text-align: center;font-family:Verdana;font-size:10pt;color:white}
table {margin-left: auto;margin-right: auto;}
.odd { background-color:#191970; }
.even { background-color:#000050; }
</style>
'@
$UserName=""
$Password=""
[Security.SecureString]$securepassword = ConvertTo-SecureString $Password -AsPlainText –Force
[PSCredential]$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$securepassword
#Connect to the filer
If (Test-Connection $filer -count 1 -Quiet)
{
Connect-NaController $filer -Credential $cred |Out-Null
Check-Disks
}
I am new to ONTAP powershell toolkit. How can I run this script?
Simply install the DataOntap Powershell toolkit and save the script to a .ps1 file then run it!