Wednesday, May 7, 2014

Generate Disk Space HTML report using Powershell


 

$Serverslist = get-content(".\clientlist.txt")

$html =""

$SizeInGB=@{Name="Size(GB)"; Expression={"{0:N2}" -f ($_.Capacity/1GB)}}
$FreespaceInGB=@{Name="Freespace(GB)"; Expression={"{0:N2}" -f ($_.Freespace/1GB)}}
$PercentFree=@{Name="PercentFree(%)";Expression={[int](($_.Freespace/$_.Capacity)*100)}}
$ServerName = @{Expression={$_.__Server};Label="Server Name"}

$outputvalues =@()
$serverlist = @()
$notaccessible= @() 

Write-output "Gathering Disk Usage Information........"
foreach ($server in $Serverslist) {
try
{
$ErrorActionPreference = 'Stop'

$outputvalues = gwmi -query "Select  __SERVER,  Name, Capacity, FreeSpace FROM win32_volume where not Name like '\\%Volume%' and drivetype <>5" -Computer $server -Credential $credential |
             Select-Object $ServerName,  Name, $SizeInGB, $FreespaceInGB, $PercentFree             
$serverlist = $serverlist + $outputvalues 
}
catch
{
 $FailedList = write-warning('failed to access "{0}":{1} in "{2}"' -f $server, $_.Exception.Message, $_.InvocationInfo.ScriptName) 
 $FailedList
 $notaccessible += $server + " "  + $_.Exception.Message + "
" } } $serverlist | ConvertTo-HTML -as table -head $html -body "<|h2> Offline Machines

$notaccessible

<|h2> Disk Space Report" | out-file "C:\diskspace_report.html--$((get-date).Tostring('yyyymmdd')).html"

No comments: