locked
ConvertTo-HTML Problem RRS feed

  • Question

  • Hi,

    I'm writing my first script and everything is working ok.. except when i try to convert it to html. i have tried a few different ways but cant seem to get it right. the closest i have got is:

    **********************************************************

    Function BSValidate-PPServer {

        Param (
            [String[]]$ComputerName,
            [String]$OutPath
              )
        ForEach ($comp in $ComputerName) {
            Write-Host "Gathering Details for $comp, Please Hold"
            Write-Host "Grabbing Computer Type"
            $comptype = if (Get-WmiObject -ComputerName $comp -

    class Win32_ComputerSystem |
                            Where-Object -Property "Manufacturer"

    -EQ 'VMware, Inc.')
                        {
                           "Virtual"
                        }
                        else
                        {
                            "Physical"
                        } ConvertTo-Html -PreContent -Fragment

    "<h2>System Type</h2>" |
                          Out-String
            Write-Host "Grabbing Disk Information"
            $DriveSPC = Get-WmiObject -Class Win32_logicalDisk -

    ComputerName $comp -Filter "DeviceID='C:'" |
                        Select DeviceID,VolumeName,@{Name=”Size

    (GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},
                                                   @

    {Name=”Freespace(GB)”;Expression={“{0:N1}” -f

    ($_.freespace/1gb)}} |

    ConvertTo-Html -as Table -Fragment -PreContent "<H2>Operating

    System Drive Details</H2>" |

    Out-String
            Write-Host "Grabbing Todays Hotfixes"
            $Hotfix = Get-HotFix -ComputerName $comp | where-

    object -value "InstalledOn" -GE (get-date).Adddays(-1)
            Write-Host "Counting Hotfixes"
            $HotfixCount = $Hotfix.count |
                                          ConvertTo-Html -Fragment

    -PreContent "<H2>Todays Installed Hotfix Count</H2>" |
                                          Out-Default
            Write-Host "Checking for Services not running that are

    meant to be"
            $SVCFail = IF (Get-WmiObject -Computername $Comp -

    class win32_service |
                        Where-Object {$_.StartMode -eq "Auto" -and

    $_.State -ne "running"} -eq $null)
                            {
                               out-string -stream "No Services

    have failed"
                            }
                            else
                            {
                                Select-Object Name, Desciption
                                } ConvertTo-Html -as Table -

    Fragment -PreContent "<H2>Failed Services</H2>" |
                                                       Out-String
            Write-Host "Checking for Application Log Errors"
            $AppLog = Get-EventLog -Computername $Comp -LogName

    "Application" -EntryType Error,warning -After (Get-

    Date).AddDays(-1) |
                        Select-Object EntryType, Source, Message 

    |
                                                       ConvertTo-

    Html -as table -Fragment -PreContent "<H2>Application Log

    Errors</H2>" |
                                                       Out-String
            Write-Host "Checking for System Log Errors"
            $SysLog =  Get-EventLog -computername $Comp -LogName

    "System" -EntryType Error,warning -After (Get-Date).AddDays(-

    1) |
                        Select-Object EntryType, Source, Message |
                                                       ConvertTo-

    Html -as Table -Fragment -PreContent "<H2>System Log

    Errors</H2>" |
                                                       Out-String

            Write-Host "Saving Your Report for $comp"
                ConvertTo-Html -PostContent "$comptype $DriveSPC

    $HotfixCount $SVCFail $AppLog $SysLog" |

                                             Out-File test.htm

            Write-Host 'Ok done, Onto the next one (if any)'


                 }
            }

    ***************************************************************************************************

    This is working ok but the areas where i have the "if" statements dont seem to be using the heading? any ideas on what im doing wrong?

    thanks

    Tuesday, August 12, 2014 3:02 AM

Answers

  • You can modify the script as required. The below is a Querying general System Information. Worked in Win 7 and Win 2008 R2.

    Add-Content  "$Filepath\style.CSS"  -Value " body { 
    font-family:Calibri; 
     font-size:10pt; 
    background-image:url('C:\Images\CookieAuth.jpg');  
    } 
    th {  
    background-color:black; 
     
    color:white; 
    } 
    td { 
     background-color:#19fff0; 
    color:black; 
    }" 
     
    Write-Host "CSS File Created Successfully... Executing Inventory Report!!! Please Wait !!!" -ForegroundColor Yellow  
    #ReportDate 
    $ReportDate = Get-Date | Select -Property DateTime |ConvertTo-Html -Fragment 
     
    #General Information 
    $ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem |  
    Select -Property Model , Manufacturer , Description , PrimaryOwnerName , SystemType |ConvertTo-Html -Fragment 
     
    #Boot Configuration 
    $BootConfiguration = Get-WmiObject -Class Win32_BootConfiguration | 
    Select -Property Name , ConfigurationPath | ConvertTo-Html -Fragment  
     
    #BIOS Information 
    $BIOS = Get-WmiObject -Class Win32_BIOS | Select -Property PSComputerName , Manufacturer , Version | ConvertTo-Html -Fragment 
     
    #Operating System Information 
    $OS = Get-WmiObject -Class Win32_OperatingSystem | Select -Property Caption , CSDVersion , OSArchitecture , OSLanguage | ConvertTo-Html -Fragment 
     
    #Time Zone Information 
    $TimeZone = Get-WmiObject -Class Win32_TimeZone | Select Caption , StandardName | 
    ConvertTo-Html -Fragment 
     
    #Logical Disk Information 
    $Disk = Get-WmiObject -Class Win32_LogicalDisk -Filter DriveType=3 |  
    Select SystemName , DeviceID , @{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}}, @{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | 
    ConvertTo-Html -Fragment 
     
    #CPU Information 
    $SystemProcessor = Get-WmiObject -Class Win32_Processor  |  
    Select SystemName , Name , MaxClockSpeed , Manufacturer , status |ConvertTo-Html -Fragment 
     
    #Memory Information 
    $PhysicalMemory = Get-WmiObject -Class Win32_PhysicalMemory | 
    Select -Property Tag , SerialNumber , PartNumber , Manufacturer , DeviceLocator , @{Name="Capacity(GB)";Expression={"{0:N1}" -f ($_.Capacity/1GB)}} | ConvertTo-Html -Fragment 
     
    #Software Inventory 
    $Software = Get-WmiObject -Class Win32_Product | 
    Select Name , Vendor , Version , Caption | ConvertTo-Html -Fragment  
     
    ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
    <font color = blue><H4><B>General Information</B></H4></font>$ComputerSystem 
    <font color = blue><H4><B>Boot Configuration</B></H4></font>$BootConfiguration 
    <font color = blue><H4><B>BIOS Information</B></H4></font>$BIOS 
    <font color = blue><H4><B>Operating System Information</B></H4></font>$OS 
    <font color = blue><H4><B>Time Zone Information</B></H4></font>$TimeZone 
    <font color = blue><H4><B>Disk Information</B></H4></font>$Disk 
    <font color = blue><H4><B>Processor Information</B></H4></font>$SystemProcessor 
    <font color = blue><H4><B>Memory Information</B></H4></font>$PhysicalMemory 
    <font color = blue><H4><B>Software Inventory</B></H4></font>$Software" -CssUri  "$filepath\style.CSS" -Title "Server Inventory" | Out-File "$FilePath\$ComputerName.html" 
     
    Write-Host "Script Execution Completed" -ForegroundColor Yellow 
    Invoke-Item -Path "$FilePath\$ComputerName.html"

    CSS file is for look and feel

    File looks like below

    Download Code


    Regards Chen V [MCTS SharePoint 2010]

    • Marked as answer by m.waters76 Tuesday, August 12, 2014 12:06 PM
    Tuesday, August 12, 2014 8:15 AM

All replies

  • What does "dont seem to be using the heading" mean?

    Can you reduce this to a simple demonstration of what you are asking?


    ¯\_(ツ)_/¯

    Tuesday, August 12, 2014 3:40 AM
  • A quick look shows that your script cannot possibly work due to a dozen syntax errors.

    ¯\_(ツ)_/¯

    Tuesday, August 12, 2014 3:42 AM
  • The following is just nonsense string together.

             $comptype=if(
                            Get-WmiObject -ComputerName $comp -class Win32_ComputerSystem | 
                             Where-Object -Property "Manufacturer" -EQ 'VMware, Inc.')
                         {
                            "Virtual"
                         }else{
                             "Physical"
                         } ConvertTo-Html -PreContent -Fragment 
            "<h2>System Type</h2>" |
                           Out-String 
    

    This is what happens when you just copy and paste without understanding what you are doing.  I see no way to fix this because is is too difficult to understand what is supposed to be happening.

    Start small - a few lines at  time and add a small amount until you understand what is happening.


    ¯\_(ツ)_/¯

    Tuesday, August 12, 2014 3:47 AM
  • If you spend some time reformatting the script and posting it as a code block using the code formatting tools on the toolbar here you might get someone to spend time on this.  I tried but is such a total mess that I gave up.  Too aggravating to look at and try to understand.

    Here is one piece I partially fixed:

    This shows how to post code:

    $cs=Get-WmiObject -ComputerName $comp -class Win32_ComputerSystem
    $comptype=if($cs.Manufacturer -eq 'VMware, Inc.'){'Virtual'}else{'Physical'} 


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, August 12, 2014 3:56 AM
    Tuesday, August 12, 2014 3:56 AM
  • You can modify the script as required. The below is a Querying general System Information. Worked in Win 7 and Win 2008 R2.

    Add-Content  "$Filepath\style.CSS"  -Value " body { 
    font-family:Calibri; 
     font-size:10pt; 
    background-image:url('C:\Images\CookieAuth.jpg');  
    } 
    th {  
    background-color:black; 
     
    color:white; 
    } 
    td { 
     background-color:#19fff0; 
    color:black; 
    }" 
     
    Write-Host "CSS File Created Successfully... Executing Inventory Report!!! Please Wait !!!" -ForegroundColor Yellow  
    #ReportDate 
    $ReportDate = Get-Date | Select -Property DateTime |ConvertTo-Html -Fragment 
     
    #General Information 
    $ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem |  
    Select -Property Model , Manufacturer , Description , PrimaryOwnerName , SystemType |ConvertTo-Html -Fragment 
     
    #Boot Configuration 
    $BootConfiguration = Get-WmiObject -Class Win32_BootConfiguration | 
    Select -Property Name , ConfigurationPath | ConvertTo-Html -Fragment  
     
    #BIOS Information 
    $BIOS = Get-WmiObject -Class Win32_BIOS | Select -Property PSComputerName , Manufacturer , Version | ConvertTo-Html -Fragment 
     
    #Operating System Information 
    $OS = Get-WmiObject -Class Win32_OperatingSystem | Select -Property Caption , CSDVersion , OSArchitecture , OSLanguage | ConvertTo-Html -Fragment 
     
    #Time Zone Information 
    $TimeZone = Get-WmiObject -Class Win32_TimeZone | Select Caption , StandardName | 
    ConvertTo-Html -Fragment 
     
    #Logical Disk Information 
    $Disk = Get-WmiObject -Class Win32_LogicalDisk -Filter DriveType=3 |  
    Select SystemName , DeviceID , @{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}}, @{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | 
    ConvertTo-Html -Fragment 
     
    #CPU Information 
    $SystemProcessor = Get-WmiObject -Class Win32_Processor  |  
    Select SystemName , Name , MaxClockSpeed , Manufacturer , status |ConvertTo-Html -Fragment 
     
    #Memory Information 
    $PhysicalMemory = Get-WmiObject -Class Win32_PhysicalMemory | 
    Select -Property Tag , SerialNumber , PartNumber , Manufacturer , DeviceLocator , @{Name="Capacity(GB)";Expression={"{0:N1}" -f ($_.Capacity/1GB)}} | ConvertTo-Html -Fragment 
     
    #Software Inventory 
    $Software = Get-WmiObject -Class Win32_Product | 
    Select Name , Vendor , Version , Caption | ConvertTo-Html -Fragment  
     
    ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
    <font color = blue><H4><B>General Information</B></H4></font>$ComputerSystem 
    <font color = blue><H4><B>Boot Configuration</B></H4></font>$BootConfiguration 
    <font color = blue><H4><B>BIOS Information</B></H4></font>$BIOS 
    <font color = blue><H4><B>Operating System Information</B></H4></font>$OS 
    <font color = blue><H4><B>Time Zone Information</B></H4></font>$TimeZone 
    <font color = blue><H4><B>Disk Information</B></H4></font>$Disk 
    <font color = blue><H4><B>Processor Information</B></H4></font>$SystemProcessor 
    <font color = blue><H4><B>Memory Information</B></H4></font>$PhysicalMemory 
    <font color = blue><H4><B>Software Inventory</B></H4></font>$Software" -CssUri  "$filepath\style.CSS" -Title "Server Inventory" | Out-File "$FilePath\$ComputerName.html" 
     
    Write-Host "Script Execution Completed" -ForegroundColor Yellow 
    Invoke-Item -Path "$FilePath\$ComputerName.html"

    CSS file is for look and feel

    File looks like below

    Download Code


    Regards Chen V [MCTS SharePoint 2010]

    • Marked as answer by m.waters76 Tuesday, August 12, 2014 12:06 PM
    Tuesday, August 12, 2014 8:15 AM
  • Thanks so much Chen! thats a push in the right direction :)

    And apologies for my post format. i missed the code snippet. its my first post.

    thanks again

    Tuesday, August 12, 2014 12:11 PM