Author Posts

September 19, 2016 at 11:11 am

Hi all,

I recently tried to amend my PowerShell disk space script, which previously outputted results to a text file, to instead create HTML reports. I want to make the report more presentable..

Original code

clear
$file = get-Content 'X' # List of Computers
$fileName = "X" + (Get-Date -Format dd.MM.yyyy) + ".TXT"

foreach ( $node in $file) {
 get-WmiObject win32_logicaldisk -ComputerName $node | 
 ft SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize | Out-File $filename -append -noclobber }

My amended code

clear
$file = get-Content 'X' # List of Computers
$fileName = "X" + (Get-Date -Format dd.MM.yyyy) + ".HTML"
foreach ( $node in $file) {
 get-WmiObject win32_logicaldisk -ComputerName $node | 
 ft SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize  | ConvertTo-HTML | Out-File $filename -append -noclobber }

This runs fine, but there is an error in the outputted data.

https://static.spiceworks.com/shared/post/0020/0728/BGD8Lgp.png

Is anybody able to point me in the right direction?

  • This topic was modified 2 years ago by  Dr3w. Reason: Enclosed close block too early
  • This topic was modified 2 years ago by  Dr3w. Reason: Enclosed close block too early

September 19, 2016 at 11:59 am

You'll need to replace ft (Format-Table) with Select-Object. Format-Table ends the object pipeline because it converts the objects received from Get-WmiObject into formatted text for output on a screen.

Please find a working and formatted example of your code below:

September 19, 2016 at 1:38 pm

Also, keep in mind that disk reports are a very common requirement of administrators all over the world. There are a lot of pre-built scripts out there that perform the functionality you're asking for. Just do a search for "powershell disk space email report" and you'll find many examples of providing this functionality. Read through them and understand logically how they creating the reports.