Convertto-HTML fails with Error

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Ahmet Cardak Ahmet Cardak 10 months ago.

  • Author
    Posts
  • #34973
    Profile photo of Ahmet Cardak
    Ahmet Cardak
    Participant

    Hi, I am pretty new to PowerShell, was following Don Jones book to make HTML Report using PowerShell, I have the following error when i run the script below;

    Error:
    ConvertTo-Html : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
    At C:\Scripts\makereport.ps1:26 char:26
    + ConvertTo-Html -Body "$Computer",$frag1,$frag2,$frag3 -Title "Repor ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [ConvertTo-Html], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ConvertToHtmlCommand

    Script;

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)]
    [string[]]$ComputerName
    )

    foreach ($Computer in $ComputerName) {

    $OsInfo = Get-OSInfo -ComputerName $Computer
    $procs = Get-Process -ComputerName $Computer
    $logs = Get-EventLog -LogName Security -ComputerName $Computer -Newest 10

    $frag1 = $OsInfo |
    ConvertTo-Html -as List -Fragment -PreContent "OS INfo" |
    Out-string

    $frag2 = $procs
    ConvertTo-Html -as List -Fragment -PreContent "Processes" |
    Out-string

    $frag3 = $logs
    ConvertTo-Html -as List -Fragment -PreContent "Logs" |
    Out-string

    ConvertTo-Html -Body "$Computer",$frag1,$frag2,$frag3 -Title "Report for $Computer" out-file "C:\scripts\report.htm"

    }

  • #34974
    Profile photo of Don Jones
    Don Jones
    Keymaster

    If you look at the examples in the book, you'll notice that I used Out-String a lot. That takes the array of strings (Object[]) and makes them into a single String, which is what -Body requires.

    You're doing that also in what you pasted here, but you need to check and ensure each variable actually contains a string and not an array. I'm not able to actually run your script to tell you where it's gone wrong.

  • #34975
    Profile photo of Ahmet Cardak
    Ahmet Cardak
    Participant

    Ok, thank you. I have actually find the EnhancedHTML2.ps1 and this generates beautiful report but is there an easy way to put the information from all the servers on the same page instead of creating attachments for each server ?

    #requires -module EnhancedHTML2

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True,
    ValueFromPipeline=$True,
    ValueFromPipelineByPropertyName=$True)]
    [string[]]$ComputerName,

    [Parameter(Mandatory=$True)]
    [string]$Path
    )
    BEGIN {
    Remove-Module EnhancedHTML2
    Import-Module EnhancedHTML2
    }
    PROCESS {

    $CSS = @"

    body {
    color:#333333;
    font-family:Calibri,Tahoma;
    font-size: 10pt;
    }
    h1 {
    text-align:center;
    }
    h2 {
    border-top:1px solid #666666;
    }

    th {
    font-weight:bold;
    color:#eeeeee;
    background-color:#333333;
    cursor:pointer;
    }
    .odd { background-color:#ffffff; }
    .even { background-color:#dddddd; }
    .paginate_enabled_next, .paginate_enabled_previous {
    cursor:pointer;
    border:1px solid #222222;
    background-color:#dddddd;
    padding:2px;
    margin:4px;
    border-radius:2px;
    }
    .paginate_disabled_previous, .paginate_disabled_next {
    color:#666666;
    cursor:pointer;
    background-color:#dddddd;
    padding:2px;
    margin:4px;
    border-radius:2px;
    }
    .dataTables_info { margin-bottom:4px; }
    .sectionheader { cursor:pointer; }
    .sectionheader:hover { color:red; }
    .grid { width:100% }
    .red {
    color:red;
    font-weight:bold;
    }

    "@

    function Get-InfoOS {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)][string]$ComputerName
    )
    $os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName
    $props = @{'OSVersion'=$os.version;
    'SPVersion'=$os.servicepackmajorversion;
    'OSBuild'=$os.buildnumber}
    New-Object -TypeName PSObject -Property $props
    }

    function Get-InfoCompSystem {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)][string]$ComputerName
    )
    $cs = Get-WmiObject -class Win32_ComputerSystem -ComputerName $ComputerName
    $props = @{'Model'=$cs.model;
    'Manufacturer'=$cs.manufacturer;
    'RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
    'Sockets'=$cs.numberofprocessors;
    'Cores'=$cs.numberoflogicalprocessors}
    New-Object -TypeName PSObject -Property $props
    }

    function Get-InfoBadService {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)][string]$ComputerName
    )
    $svcs = Get-WmiObject -class Win32_Service -ComputerName $ComputerName `
    -Filter "StartMode='Auto' AND State'Running'"
    foreach ($svc in $svcs) {
    $props = @{'ServiceName'=$svc.name;
    'LogonAccount'=$svc.startname;
    'DisplayName'=$svc.displayname}
    New-Object -TypeName PSObject -Property $props
    }
    }

    function Get-InfoProc {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)][string]$ComputerName
    )
    $procs = Get-WmiObject -class Win32_Process -ComputerName $ComputerName
    foreach ($proc in $procs) {
    $props = @{'ProcName'=$proc.name;
    'Executable'=$proc.ExecutablePath}
    New-Object -TypeName PSObject -Property $props
    }
    }

    function Get-InfoNIC {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)][string]$ComputerName
    )
    $nics = Get-WmiObject -class Win32_NetworkAdapter -ComputerName $ComputerName `
    -Filter "PhysicalAdapter=True"
    foreach ($nic in $nics) {
    $props = @{'NICName'=$nic.servicename;
    'Speed'=$nic.speed / 1MB -as [int];
    'Manufacturer'=$nic.manufacturer;
    'MACAddress'=$nic.macaddress}
    New-Object -TypeName PSObject -Property $props
    }
    }

    function Get-InfoDisk {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)][string]$ComputerName
    )
    $drives = Get-WmiObject -class Win32_LogicalDisk -ComputerName $ComputerName `
    -Filter "DriveType=3"
    foreach ($drive in $drives) {
    $props = @{'Drive'=$drive.DeviceID;
    'Size'=$drive.size / 1GB -as [int];
    'Free'="{0:N2}" -f ($drive.freespace / 1GB);
    'FreePct'=$drive.freespace / $drive.size * 100 -as [int]}
    New-Object -TypeName PSObject -Property $props
    }
    }

    foreach ($computer in $computername) {
    try {
    $everything_ok = $true
    Write-Verbose "Checking connectivity to $computer"
    Get-WmiObject -class Win32_BIOS -ComputerName $Computer -EA Stop | Out-Null
    } catch {
    Write-Warning "$computer failed"
    $everything_ok = $false
    }

    if ($everything_ok) {
    $filepath = Join-Path -Path $Path -ChildPath "$computer.html"

    $params = @{'As'='List';
    'PreContent'='OS'}
    $html_os = Get-InfoOS -ComputerName $computer |
    ConvertTo-EnhancedHTMLFragment @params

    $params = @{'As'='List';
    'PreContent'='Computer System'}
    $html_cs = Get-InfoCompSystem -ComputerName $computer |
    ConvertTo-EnhancedHTMLFragment @params

    $params = @{'As'='Table';
    'PreContent'='♦ Local Disks';
    'EvenRowCssClass'='even';
    'OddRowCssClass'='odd';
    'MakeTableDynamic'=$true;
    'TableCssClass'='grid';
    'Properties'='Drive',
    @{n='Size(GB)';e={$_.Size}},
    @{n='Free(GB)';e={$_.Free};css={if ($_.FreePct -lt 80) { 'red' }}},
    @{n='Free(%)';e={$_.FreePct};css={if ($_.FreeePct -lt 80) { 'red' }}}}
    $html_dr = Get-InfoDisk -ComputerName $computer |
    ConvertTo-EnhancedHTMLFragment @params

    $params = @{'As'='Table';
    'PreContent'='♦ Processes';
    'MakeTableDynamic'=$true;
    'TableCssClass'='grid'}
    $html_pr = Get-InfoProc -ComputerName $computer |
    ConvertTo-EnhancedHTMLFragment @params

    $params = @{'As'='Table';
    'PreContent'='♦ Services to Check';
    'EvenRowCssClass'='even';
    'OddRowCssClass'='odd';
    'MakeHiddenSection'=$false;
    'TableCssClass'='grid'}
    $html_sv = Get-InfoBadService -ComputerName $computer |
    ConvertTo-EnhancedHTMLFragment @params

    $params = @{'As'='Table';
    'PreContent'='♦ NICs';
    'EvenRowCssClass'='even';
    'OddRowCssClass'='odd';
    'MakeHiddenSection'=$false;
    'TableCssClass'='grid'}
    $html_na = Get-InfoNIC -ComputerName $Computer |
    ConvertTo-EnhancedHTMLFragment @params

    $params = @{'CssStyleSheet'=$style;
    'Title'="System Report for $computer";
    'PreContent'="System Report for $computer";
    'HTMLFragments'=@($html_os,$html_cs,$html_dr,$html_pr,$html_sv,$html_na);
    'jQueryDataTableUri'='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.3/jquery.dataTables.min.js';
    'jQueryUri'='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js'}
    ConvertTo-EnhancedHTML @params |
    Out-File -FilePath $filepath

    }
    }

    }

You must be logged in to reply to this topic.