Author Posts

May 4, 2017 at 6:06 am

Hi Guys,

I have written up a small disk space report script to show me disk space status on my servers. Nothing ground-breaking but it does what I need it to do. It works perfectly in the console when I run it however, now I want it to output to an HTML file and eventually send it to me on a weekly basis.
Somehow I just cant get it to output into a readable format, it seems to show up some machine code characters. Once I get the output working, I can write up the code to send an email to me which would be quite simple, but I need to fix this output issue first!
Any ideas?
I will continue to search the internet for similar issues but thought I'd ask here as well..

Here is my script

Get-WmiObject win32_logicaldisk -ComputerName $servers -Credential $cred -Filter "drivetype=3" | select PSComputerName,Name,FileSystem,VolumeName,
@{n="Free Space in GB";e={[math]::truncate($_.freespace / 1GB)}},@{n="Size of Disk";e={[math]::truncate($_.size/ 1GB)}} | Format-table -GroupBy PSComputerName | ConvertTo-Html -CssUri 'C:\powershell\Scripts\Disk Size\style.css' -Title "Disk Report" | 
Out-File C:\Atkins\disk.html 

-A

May 4, 2017 at 9:58 am

You'll need to remove the Format-Table statement in your pipeline because that it does not output objects but text. ConvertTo-Html needs objects as input to create HTML documents or fragments. Format-Table is usually the last command in a pipeline used to output text to an interactive PowerShell console session.

Try the below refactored example:

$getWmiObjectParams = @{
    Class = 'Win32_LogicalDisk'
    ComputerName = $servers
    Credential = $cred
    Filter = 'drivetype=3'
}

$selectObjectProperties = @(
    'PSComputerName'
    'Name'
    'FileSystem'
    'VolumeName'
    @{
        Name = "Free Space in GB"
        Expression = { [math]::truncate($_.freespace / 1GB) }
    }
    @{
        Name = "Size of Disk"
        Expression = { [math]::truncate($_.size/ 1GB) }
    }
)

Get-WmiObject @getWmiObjectParams | 
    Select-Object -Property $selectObjectProperties |
        ConvertTo-Html -CssUri 'C:\powershell\Scripts\Disk Size\style.css' -Title "Disk Report" |
            Out-File C:\Atkins\disk.html