Author Posts

October 19, 2016 at 4:11 pm

hi, newbie but excited (first time posting and asking) ..

I have a script i found that works great for pooling disk space on servers. What I thought would just be awesome is to also pool server "up times" added to this same script. The scripts writes to an html file inserting columns ect. I though, being new can't figure out where to insert this addition of server uptime.

I did find a stand alone, which i suppose I could use, but would be nice to added to the server space script. Can one of you great experts suggest how to insert a script and it also insert a column for each servers up time, or is this just now going to work with in this script?

# First lets create a text file, where we will later save the freedisk space info
$freeSpaceFileName = "FreeSpace.htm"
$serverlist = "sl.txt"
$warning = 30
$critical = 10
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString('yyyy/MM/dd')
Add-Content $fileName ""
Add-Content $fileName ""
Add-Content $fileName ""
Add-Content $fileName 'xxxx.COM DiskSpace Report'
add-content $fileName "
add-content $fileName ""
add-content $fileName ""
Add-Content $fileName ""
Add-Content $fileName ""

add-content $fileName ""
add-content $fileName ""
add-content $fileName ""
add-content $fileName "xxxx.COM DiskSpace Report – $date"
add-content $fileName ""
add-content $fileName ""
add-content $fileName ""

}

# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)

Add-Content $fileName ""
Add-Content $fileName "Drive"
Add-Content $fileName "Drive Label"
Add-Content $fileName "Total Capacity(GB)"
Add-Content $fileName "Used Capacity(GB)"
Add-Content $fileName "Free Space(GB)"
Add-Content $fileName "Freespace %"
Add-Content $fileName ""
}

Function writeHtmlFooter
{
param($fileName)

Add-Content $fileName ""
Add-Content $fileName ""
}

Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName ""
Add-Content $fileName "$devid"
Add-Content $fileName "$volName"

Add-Content $fileName "$totSpace"
Add-Content $fileName "$usedSpace"
Add-Content $fileName "$frSpace"
Add-Content $fileName "$freePercent"
Add-Content $fileName ""
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName ""
Add-Content $fileName "$devid"
Add-Content $fileName "$volName"
Add-Content $fileName "$totSpace"
Add-Content $fileName "$usedSpace"
Add-Content $fileName "$frSpace"
Add-Content $fileName "$freePercent"
#
Add-Content $fileName ""
}
else
{
Add-Content $fileName ""
Add-Content $fileName "$devid"
Add-Content $fileName "$volName"
Add-Content $fileName "$totSpace"
Add-Content $fileName "$usedSpace"
Add-Content $fileName "$frSpace"
Add-Content $fileName "$freePercent"
# #FBB917
Add-Content $fileName ""
}
}
Function sendEmail
{ param($from,$to,$subject,$smtphost,$htmlFileName)
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)

}

writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $serverlist)
{
Add-Content $freeSpaceFileName ""
Add-Content $freeSpaceFileName ""
Add-Content $freeSpaceFileName " $server "
Add-Content $freeSpaceFileName ""

writeTableHeader $freeSpaceFileName

$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3}
foreach ($item in $dp)
{
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size

}
}
writeHtmlFooter $freeSpaceFileName
$date = ( get-date ).ToString('yyyy/MM/dd')
sendEmail XXXXXXX "Disk Space Report – $Date" $freeSpaceFileName

  • This topic was modified 1 year, 11 months ago by  rmueller.

October 19, 2016 at 4:59 pm

Richard Mueller jr? start over, make one custom object with properties. Convert it to html and then send.

$diskspace = your code to get diskspace
$uptime = your code to get uptime

[pscustomobject]@{

disk = $diskspace
uptime = $uptime

}

October 19, 2016 at 5:48 pm

This will generate an html report with uptime. $list variable contains a list of computernames.

$report = foreach ($comp in $list){
$drive = Get-WmiObject win32_logicaldisk -Filter 'DriveType = "3"' -ComputerName $comp | 
Select-Object DeviceID,VolumeName,status,Size,FreeSpace
$lastboot = Get-WmiObject Win32_OperatingSystem -ComputerName $comp
$time = (Get-Date) - $lastboot.ConvertToDateTime($lastboot.LastBootUpTime)

[PSCustomObject]@{
    ComputerName = $comp
    DeviceID = $drive.DeviceID
    VolumeName = $drive.VolumeName
    Status = $drive.status
    Size = "{0:N2} GB" -f ($drive.Size /1GB)
    Freespace = "{0:N2} GB" -f ($drive.FreeSpace /1GB)
    Uptime = "{0} Days {1} Hours {2} Minutes" -f $time.Days,
    $time.Hours,$time.Minutes}
} #End Foreach

$report | ConvertTo-Html -Body "Diskspace Report" |
 Out-File .\report.html 

October 19, 2016 at 6:38 pm

Because of the way it's written, there's no easy place to add up time. The author is formatting and writing the HTML directly from the called functions for each of the disks it finds in the Get-WmiObject call. That removes any chance you have of manipulating the data, adding to it, or formatting it. If he worked with objects and then added information, etc. to those objects before formatting them as HTML and dumping it out to a file, he could make it a lot more versatile.

Dan Potter had it right when he told you to create an array of custom objects with the information you want displayed in the report and then convert that array to HTML. Look at using the ConvertTo-Html cmdlet...

October 19, 2016 at 7:15 pm

@random ..

wow. you just broke that whole dang script down to this? Ok, i would had server list location inside the ($comp in $list)? =c:\list.txt ?

October 19, 2016 at 7:23 pm

$list = Get-Content \\path\to\list.txt

October 19, 2016 at 8:44 pm

thanks ... that worked fine, except for a few squawked about access.. possible being 2012 type, might not like the uptime part being called that way. The script is being run as an enterprise/domain admin.

Again, thanks for the help and lesson. I look forward to being able to help others.