Search code examples
htmlazurepowershellreportreporting

Poweshell Disk space report into a HTML format to makie more cleaner?


Hi I have the following powershell script which pull disk space data from azure (for a selected list of serves) and put it out in a CSV - however its never clean even with teh delimiter "," in place

Instead i would like to have it in a HTML format (More cleaner) Any help would be good..

`Import-Module ActiveDirectory

# Delete reports older than 60 days
$OldReports = (Get-Date).AddDays(-60)

# Location for disk reports`your text`
Get-ChildItem "C:\Temp\DiskSpaceReport\*.*" |
Where-Object { $_.LastWriteTime -le $OldReports } |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

# Create variable for log date
$LogDate = get-date -f yyyyMMdd

############
# Import CSV file
$Servers = Import-Csv C:\Temp\ADComputers.csv

# Loop through each system
$DiskReport = ForEach ($Server in $Servers) {

# Get system
$System = Get-ADComputer $Server.Hostname | Where-Object { $_.Enabled -eq $true } | 
Select-Object Name, DNSHostName | Sort-Object Name
Get-WmiObject win32_logicaldisk `
    -ComputerName $System.DNSHostName -Filter "Drivetype=3" `
    -ErrorAction SilentlyContinue
}

# Create disk report
$DiskReport | Select-Object `
@{Label = "HostName"; Expression = { $_.SystemName } },
@{Label = "DriveLetter"; Expression = { $_.DeviceID } },
#@{Label = "DriveName"; Expression = { $_.VolumeName } },
#@{Label = "Total Capacity (GB)"; Expression = { "{0:N1}" -f ( $_.Size / 1gb) } },
#@{Label = "Free Space (GB)"; Expression = { "{0:N1}" -f ( $_.Freespace / 1gb ) } },
@{Label = 'Free Space (%)'; Expression = { "{0:P0}" -f ($_.Freespace /$_.Size) } } |

# Export report to CSV file
Export-csv -Path "C:\Temp\DiskSpaceReport\DiskReport_$logDate.txt" -NoTypeInformation -Delimiter    
","

##### EMail the report  

#Define Variables#

$fromaddress = “[email protected]”
$toaddress = “[email protected]”
#$ccaddress = "[email protected]"
$Subject = “UK Disk report”
#$body = get-content “C:\users\desktop\content.html”
$attachment = “C:\temp\DiskSpaceReport\DiskReport_$logDate.txt"
$smtpserver = “relay.mail.com”

###

$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
#$message.CC.Add($ccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
#$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($file)
#$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)

###################################################`

This is what i see: enter image description here


Solution

  • For converting your data to HTML format instead of CSV, you can use ConvertTo-Html

    Convert$DiskReport block into an HTML fragment by using | ConvertTo-Html -Fragmentin your script.

    Add .html type instead of .txt

     $HtmlFilePath = "C:\Temp\DiskSpaceReport\DiskReport_$LogDate.html"
    

    Convert DiskReport information to html as below.

     $HtmlReport = $DiskReport | Select-Object 
     @{Label = "HostName"; Expression = { $_.SystemName } }, 
    @{Label = "DriveLetter"; Expression = { $_.DeviceID } }, 
    #@{Label = "DriveName"; Expression = { $_.VolumeName } }, 
    #@{Label = "Total Capacity (GB)"; Expression = { "{0:N1}" -f ( $_.Size / 1gb) } 
    | ConvertTo-Html -Fragment
    

    Save HTML report to file.

     $HtmlFilePath = "C:\Temp\DiskSpaceReport\DiskReport_$LogDate.html" $HtmlReport | Out-File -FilePath $HtmlFilePath
     $HtmlReport | Out-File -FilePath $HtmlFilePath
    

    I have used below PowerShell script to retrieves information about Azure VMs and exports the details to HTML File.

       $resourceGroupName = "Identity-Resources"
        $vmList = Get-AzVM -ResourceGroupName $resourceGroupName
        $vmInfoArray = @()
        foreach ($vm in $vmList) {
            $vmInfo = [PSCustomObject]@{
                Name = $vm.Name
                ResourceGroup = $vm.ResourceGroupName
                Location = $vm.Location
                OSType = $vm.StorageProfile.OsDisk.OsType
                VMSize = $vm.HardwareProfile.VmSize
            }
            $vmInfoArray += $vmInfo | ConvertTo-Html -Fragment 
        }
        $LogDate ="16-09-2023"
        $HtmlFilePath = "/home/vallepu/File_$LogDate.html"
        $vmInfoArray| Out-File -FilePath $HtmlFilePath
        Write-Host "VM information exported to $HtmlFilePath"
    

    enter image description here

    HTML File:

    enter image description here