Convert Hard Drive Results to a HTML report and send it in email

 

 

$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color:black;}"
$a = $a + "Table{background-color:#DFFFFF;border-collapse: collapse;}"
$a = $a + "TH{border-width:1px;padding:0px;border-style:solid;border-color:black;}"
$a = $a + "TD{border-width:1px;padding-left:5px;border-style:solid;border-color:black;}"
$a = $a + "</style>"

Get-WmiObject Win32_logicaldisk -Filter "DriveType=3" -ComputerName localhost | Select-Object SystemName, Deviceid, @{ Name = "Size (GB)"; Expression = {"{0:N0} GB" -f ($_.size / 1GB) } },@{ Name = "Free Space (GB)"; Expression = { "{0:N0} GB" -f ($_.freespace / 1GB) } },@{ Name = "Free Space (%)"; Expression = { "{0:P2} GB" -f (($_.freespace / 1GB) / ($_.size / 1GB)) } } | ft -AutoSize | ConvertTo-Html -head $a | Out-File DriveSpace.htm

  • First, learn about the virtues of a here-string instead of endless concatenations.

     001 
     002 
     003 
     004 
     005 
     006 
     007 
     008 
    $a = @"
    <style>
    TABLE{border-width: 1px;border-style: solid;border-color:black;}
    Table{background-color:#DFFFFF;border-collapse: collapse;}
    TH{border-width:1px;padding:0px;border-style:solid;border-color:black;}
    TD{border-width:1px;padding-left:5px;border-style:solid;border-color:black;}
    </style>
    "@

    Secondly, write a script instead of trying to do everything in one pipeline.  My eyes hurt just looking at that.

  • this is the garbage it has been producing

    ClassId2e4f51ef21dd47e99d3c952918aff9cdpageHeaderEntrypageFooterEntryautosizeInfoshapeInfogroupingEntry
    033ecb2bc07a4d43b5ef94ed5a35d280 Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo
    9e210fe47d09416682b841769c78b8a3
    27c87ef9bbda4f709f6b4002fa4af63c
    4ec4f0187cb04f4cb6973460dfe252df
    cf522b78d86c486691226b40aa69e95c

     

  • ok I cleaned it up put it in a script,  but the Hard drive output is not coming out right


    $a = @"
    <style>
    TABLE{border-width: 1px;border-style: solid;border-color:black;}
    Table{background-color:#DFFFFF;border-collapse: collapse;}
    TH{border-width:1px;padding:10px;border-style:solid;border-color:black;}
    TD{border-width:1px;padding-left:10px;border-style:solid;border-color:black;}
    TD{border-width:1px;padding-right:10px;border-style:solid;border-color:black;}
    </style>
    "@


    $PCs = Get-Content -Path "P:\scripts\PCs.txt"

     $results = @{}
     $results = foreach ($PC in $PCs)
     {
         if (Test-Connection -ComputerName $PC -Count 1 -Quiet)
         {
             Write-Verbose "Working on $PC"
             $bios = Get-WmiObject Win32_BIOS -ComputerName $PC -Credential $local
             $Proc = Get-WmiObject Win32_processor -ComputerName $PC -Credential $local | Select-Object -First 1
             $memory = Get-WmiObject Win32_physicalmemory -ComputerName $PC -Credential $local
             $system = Get-WmiObject Win32_ComputerSystem -ComputerName $PC -Credential $local
             $os = Get-WmiObject Win32_OperatingSystem -ComputerName $PC -Credential $local
             $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $PC -Credential $local | ? {$_.IPEnabled}
      $Disk = Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -ComputerName $PC | Select SystemName,DeviceID,@{Name="Size(GB)";Expression={"{0:N0} GB" -f ($_.size / 1GB) } },@{Name="FreeSpace(GB)";Expression={ "{0:N0} GB" -f ($_.freespace / 1GB) } }
            [PSCustomObject]@{
                 "PC Name" = $os.csname
                 "OS Version" = $os.Caption
                "SystemType" = $system.SystemType
     "Hard Drives" = $Disk
             }
         }
      }

    $results | ConvertTo-HTML -head $a | Out-File P:\Scripts\Inventory.htm

     

    PC NameOS VersionSystemTypeHard Drives
    CORPMASTER Microsoft Windows Server 2012 R2 Standard x64-based PC @{SystemName=CORPMASTER; DeviceID=C:; Size(GB)=279 GB; FreeSpace(GB)=256 GB}

     

  • You need to breakout the drive information.  Additionally, you may have multiple drives.

     001 
     002 
     003 
     004 
     005 
     006 
     007 
     008 
     009 
     010 
     011 
     012 
     013 
     014 
     015 
     016 
     017 
     018 
     019 
     020 
     021 
     022 
     023 
     024 
     025 
     026 
     027 
     028 
     029 
     030 
     031 
     032 
     033 
     034 
     035 
     036 
     037 
     038 
     039 
     040 
     041 
     042 
    $a = @"
    <style>
    TABLE{border-width: 1px;border-style: solid;border-color:black;}
    Table{background-color:#DFFFFF;border-collapse: collapse;}
    TH{border-width:1px;padding:10px;border-style:solid;border-color:black;}
    TD{border-width:1px;padding-left:10px;border-style:solid;border-color:black;}
    TD{border-width:1px;padding-right:10px;border-style:solid;border-color:black;}
    </style>
    "@
    $PCs = "db1","util1"
     
    #$PCs = Get-Content -Path "P:\scripts\PCs.txt" 
     
    $results = @{} 
    $results = foreach ($PC in $PCs) 
    { 
        if (Test-Connection -ComputerName $PC -Count 1 -Quiet) 
        { 
            Write-Verbose "Working on $PC" 
            $bios = Get-WmiObject Win32_BIOS -ComputerName $PC -Credential $local 
            $Proc = Get-WmiObject Win32_processor -ComputerName $PC -Credential $local | Select-Object -First 1 
            $memory = Get-WmiObject Win32_physicalmemory -ComputerName $PC -Credential $local 
            $system = Get-WmiObject Win32_ComputerSystem -ComputerName $PC -Credential $local 
            $os = Get-WmiObject Win32_OperatingSystem -ComputerName $PC -Credential $local 
            $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $PC -Credential $local | ? {$_.IPEnabled} 
            $drives = Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -ComputerName $PC | 
                Select SystemName,DeviceID,@{Name="Size(GB)";Expression={"{0:N0} GB" -f ($_.size / 1GB) } },@{Name="FreeSpace(GB)";Expression={ "{0:N0} GB" -f ($_.freespace / 1GB) } } 
            foreach ($drive in $drives)
            {
            [PSCustomObject]@{ 
                "PC Name" = $os.csname 
                "OS Version" = $os.Caption 
                "SystemType" = $system.SystemType
                "Device ID" = $drive.DeviceID
                "Size(GB)" = $drive."Size(GB)"
                "FreeSpace(GB)" = $drive."FreeSpace(GB)"
                }
            } 
         } 
      } 
     
    $results | ConvertTo-HTML -head $a | Out-File .\Inventory.htm 
  • Thanks Bob, that's what I was looking for,