Author Posts

August 17, 2014 at 8:35 am

Hi Guys,

I am trying to generate an HTML report and have come across an issue where if my hashtable has a single key-value pair the output displays an * on the html but if I have 2 or more key-value pairs it's not an issue. Something to do with what the function returns??

# This script will create an html file in your current location named $computername.html
$Path = Get-Location
$ComputerName = $env:COMPUTERNAME

function Get-InfoOS {
    $os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName
    $props = @{'Edition'=$os.caption}
    New-Object -TypeName PSObject -Property $props
}

function Get-InfoOS2 {
    $os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName
    $props = @{'OSVersion'=$os.version;
               'SPVersion'=$os.servicepackmajorversion;
               'Edition'=$os.caption}
    New-Object -TypeName PSObject -Property $props
}

$filepath = Join-Path -Path $Path -ChildPath "$computerName.html"
$html_os = Get-InfoOS -ComputerName $computerName |
           ConvertTo-HTML -As List -Fragment -PreContent "OS" |
           Out-String
           
$html_os2 = Get-InfoOS2 -ComputerName $computerName |
           ConvertTo-HTML -As List -Fragment -PreContent "OS 2" |
           Out-String
           
$params = @{'Title'="Report for $computer";
                    'PreContent'="System Report for $computerName";
                    'PostContent'=$html_os,$html_os2}
        ConvertTo-HTML @params | Out-File -FilePath $filepath

The output looks like this:

OS

*:	Microsoft Windows 7 Enterprise

OS 2

OSVersion:	6.1.7601
Edition:	Microsoft Windows 7 Enterprise
SPVersion:	1

Any ideas?

Thanks

August 17, 2014 at 11:31 am

Hi Terry,

I cannot explain this odd behavior of ConvertTo-HTML, but in my experience, explicitly specifying which properties that should be converted to HTML using the parameter -Property will solve the problem.

Example 1, showing the same problem as in your post:

$object1 = [PSCustomObject]@{
    One = '1'
}

$object2 = [PSCustomObject]@{
    One = '1'
    Two = '2'
}

$object1 | ConvertTo-HTML -Fragment -As List | Out-String
$object2 | ConvertTo-HTML -Fragment -As List | Out-String

The output is:


*:1



One:1
Two:2

Example 2:
Now I'm using Get-Member -MemberType *Property | Select-Object -ExpandProperty Name to get a list of all properties that I want to convert to HTML and specify these to the cmdlet ConvertTo-HTML:

$object1 = [PSCustomObject]@{
    One = '1'
}

$object2 = [PSCustomObject]@{
    One = '1'
    Two = '2'
}


$Properties1 = $object1 | 
    Get-Member -MemberType *Property | 
        Select-Object -ExpandProperty Name
$object1 | ConvertTo-HTML -Fragment -As List -Property $Properties1 | Out-String

$Properties2 = $object2 | 
    Get-Member -MemberType *Property | 
        Select-Object -ExpandProperty Name
$object2 | ConvertTo-HTML -Fragment -As List -Property $Properties2 | Out-String

Now I get the output:


One:1



One:1
Two:2

And ofcourse you dont need to use Get-Member, if you know which properties you want, just specify them.

August 18, 2014 at 1:26 pm

Thank you Simon, that resolved my issue.