Help required with script using Convertto-EnhancedHTML advanced function

Hello All

First time I’m posting as I’ve become quite stuck on a script I’m creating using Dons ‘Convertto-EnhancedHTML’ function.
Any help would be greatly appreciated …

I’ve basically created a script that reads in a load of objects and pipes them out to a webpage using the ‘Convertto-EnhancedHTML’ advanced function.
The script is shown below and consists of a module file (which has all the advanced functions) that is called from the main script file.
The styling of the webpage is done via a css file which is also working fine. There are a lot more fragments in total but i’ve purposely not shown them
due to ease of readability.

PROBLEM: The problem I’m experiencing is when my Get-OsinfoVminfo function returns NOTHING (i.e. no objects) It messes up the entry on the
outputted webpage, so I would prefer it not to be outputted. Hence, I’ve been trying to figure out a way that if NO objects are
outputted then that fragment does not get added to my list of fragments …
Make sense ???

The code (which I’ve shown below) shows the basic configuation script and my functions sit in another PSM1 module file. (not added).
As I have mentioned it works great, just want to figure this ‘quirk’ out.

As usual any help would be GREATLY appreciated. Many thanks in advance

Controller Script - to be used in conjunction with ‘Advanced functions’

HelpMessage = “Please enter a Servername”)]
$style = (Get-content ‘C:\css\style.css’)
Get-Module OSTools | Out-Null

} # Begin
Foreach ($computer in $computername) {
Write-Verbose $VC

    $frag12 = Get-OsinfoVMInfo -VCName $computer | where { $ -ne (((($_.vmpathname).Split(']')[1]).Split('/'))[0].TrimStart(' '))} | Select Name,vmpathname |
              ConvertTo-EnhancedHTMLFragment -TableCssID IFolders_TCID `
                                             -DivCssID IFolders_DCID `
                                             -DivCssClass IFolders_DCC `
                                             -TableCssClass IFolders_TCC `
                                             -As Table `
                                             -Properties Name,vmpathname `
                                             -MakeHiddenSection  `
                                             -MakeTableDynamic `
                                             -Precontent 'Inconsistent Folders' | Out-String

    $frag13 = Get-OsinfoVMInfo -VCName $computer | where {$_.'Consolidation Needed' -eq $true} | Select Name, 'Consolidation Needed' |
             ConvertTo-EnhancedHTMLFragment -TableCssID Consolidation_Needed_TCID `
                                            -DivCssID Consolidation_Needed_DCID `
                                            -DivCssClass Consolidation_Needed_DCC `
                                            -TableCssClass Consolidation_Needed_TCC `
                                            -As Table `
                                            -Properties Name,'Consolidation Needed' `
                                            -MakeHiddenSection  `
                                            -MakeTableDynamic `
                                            -Precontent 'Consolidation Required' | Out-String

    $frag14 = Get-OsinfoVMInfo -VCName $computer | where {($_.'NIC Connected' -ne $true) -or ($_.'NIC Start' -ne $true)} | Select Name, 'NIC Connected','NIC Start' |
             ConvertTo-EnhancedHTMLFragment -TableCssID NIC_Connected_TCID `
                                            -DivCssID NIC_Connected_DCID `
                                            -DivCssClass NIC_Connected_DCC `
                                            -TableCssClass NIC_Connected_TCC `
                                            -As Table `
                                            -Properties Name,'NIC Connected','NIC Start' `
                                            -MakeHiddenSection  `
                                            -MakeTableDynamic `
                                            -Precontent 'NIC State' | Out-String

    $frag15 = Get-OsinfoVMInfo -VCName $computer | where {($_.'Inaccessible/Invalid VM' -eq 'Invalid') -or ($_.'Inaccessible/Invalid VM' -eq 'Inaccessible')} | Select Name, 'Inaccessible/Invalid VM' |
             ConvertTo-EnhancedHTMLFragment -TableCssID InvalidVMs_TCID `
                                            -DivCssID InvalidVMs_DCID `
                                            -DivCssClass InvalidVMs_DCC `
                                            -TableCssClass InvalidVMs_TCC `
                                            -As Table `
                                            -Properties Name,'Inaccessible/Invalid VM' `
                                            -MakeHiddenSection  `
                                            -MakeTableDynamic `
                                            -Precontent 'Inaccessible VMs' | Out-String

    $path = Join-Path -Path 'C:\temp' -ChildPath ("$VC" + "_" + "$(((Get-date).ToShortDateString()) -replace '/','')" + ".html")

    ConvertTo-EnhancedHTML -Title "Morning Check Report for $computer" `
                           -PreContent "Morning Report: $(($VC).ToUpper())" `
                           -HTMLFragments $frag1,$frag2,$frag3,$frag4,$frag5,$frag6,$frag7,$frag8,$frag9,$frag10,$frag11,$frag12,$frag13,$frag14,$frag15 `
                           -PostContent "Time Retrieved $(Get-Date)" `
                           -CssStyleSheet $style |
    Out-File $path -Encoding ascii
} #foreach

} # Process
END{} # End

Heading on vacation, but briefly:

Capture the output of your function into a variable. Then check $var.count to see if it’s zero. If it is, don’t output. If it isn’t, create your HTML fragment.

Alternately, see if $var -Eq $null - meaning, it’s got nothing in it. Sometimes that’s more reliable of a check.

Thanks Don - Much appreciated (have a nice break) ! I thought as much but I’ve run into another problem … shown below.

So as you mentioned I have captured my output into a variable and using $null I’m omitting each of the $frags that are not reporting back anything - see below:

if((Get-OSinfoVMInfo -computerName Server1 | where {($.‘NIC Connected’ -ne $true) -or ($.‘NIC Start’ -ne $true)}) -eq $null){
} else {
$frag14 = Get-OsinfoVMinfo -computerName Server1 | where {($.‘NIC Connected’ -ne $true) -or ($.‘NIC Start’ -ne $true)} | Select Name, ‘NIC Connected’,‘NIC Start’ |
ConvertTo-EnhancedHTMLFragment -TableCssID NIC_Connected_TCID `
-DivCssID NIC_Connected_DCID `
-DivCssClass NIC_Connected_DCC `
-TableCssClass NIC_Connected_TCC `
-As Table `
-Properties Name,‘NIC Connected’,‘NIC Start’ `
-MakeHiddenSection `
-MakeTableDynamic `
-Precontent ‘NIC State’ | Out-String
$HTMLFrags += ($frag14 + ‘,’)

I then am trying to add them into the ‘$HTMLFrags’ variable. (Hence only the successful ones will be added in.)

The issue is this when I get my $HTMLfrags variable back it has all the ‘successful’ ‘frags’ in there as expected but I CANNOT enter that into the ‘-HTMLfragments’ parameter in the ‘Convertto-EnhanceHTML’ advanced function. This is because the parameter requires that each of the ‘$frag’ variables that are to be entered in be separated by commas.

So I need a way to enter in the $HTMLFrags variable in a manner that the separate $frags can be separated by a’,’ (comma). I’ve tried splitting the entries by using the -split operator (using ‘span’ to split on) but it removes it completely. After the split Ive tried the -join operator but that hasnt worked.

I feel like Im close to a solution but could do with the extra help.

Many thanks in advance

You’re meant to capture each fragment into a different variable. If you’re concatenating them all together, then the command won’t work the way it’s designed.

Thanks Don - but how would I omit a fragment if it reports back with nothing ???!? ?

All of my fragments come back in their own $frag[1-15] using the below syntax …

    $frag14 = Get-OSinfoVMVMInfo -computerName servername | where {($_.'NIC Connected' -ne $true) -or ($_.'NIC Start' -ne $true)} | Select Name, 'NIC Connected','NIC Start' |
              ConvertTo-EnhancedHTMLFragment -TableCssID NIC_Connected_TCID `
                                            -DivCssID NIC_Connected_DCID `
                                            -DivCssClass NIC_Connected_DCC `
                                            -TableCssClass NIC_Connected_TCC `
                                            -As Table `
                                            -Properties Name,'NIC Connected','NIC Start' `
                                            -MakeHiddenSection  `
                                            -MakeTableDynamic `
                                            -Precontent 'NIC State' | Out-String

When I enter into the -HTMLfragments parameter I am entering the following string:

-HTMLFragments $frag1,$frag2,$frag3,$frag4,$frag5,$frag6,$frag7,$frag8,$frag9,$frag10,$frag11,$frag12,$frag13,$frag14,$frag15 `

The error i’m getting is as follows:

Write-Verbose : Cannot bind argument to parameter ‘Message’ because it is null.
At D:\scripts\MorningChecks.ps1:202 char:23

  •     Write-Verbose $HTMLFrags
  •                   ~~~~~~~~~~
    • CategoryInfo : InvalidData: (:slight_smile: [Write-Verbose], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.WriteVerboseCommand

So i am assuming I am getting this error back due to one of the $frag variables is null. Ideally would like to know how I can create a foreach loop for all of my frags - the issue is once the ‘out-string’ cmdlet is used ALL of the frags are populated hence the $null does not work.

What the parameter accepts is an array, not a comma separated list per se.

$array = @()

Creates a fresh array.

$array += $frag

Adds a fragment to it. So you simply don’t add a blank fragment. Pass $array to the parameter.

Ok so I understand that the parameter requires an array. But how do I add each fragment as shown above to the array. i tried that with the if statement but it didnt work ;-(

Im getting all the frags but its adding them to the array thats puzzling me.

thanks for your help again DON :wink:

That was the command I gave you.

$array = @()

$frag = do-something | convertto-enhancedhtmlfragment
$array += $frag

$frag = do-somethingelse | convertto-enhancedhtmlfragment
$array += $frag

$result = do-somethingagain
If ($result -ne $null) {
$frag = $result | convertto-enhancedhtmlfragment
$array += $frag

$array now has 2-3 fragments in it.

OMG! that worked and it rocks !!!

Thank you soooo much Don - much appreciated. Will remember this ‘technique’ for the future as its really handy.

(For anyone who wants to know - instead of labelling my fragments $frag1, $frag2,$frag3 etc I ended up labelling them as $frag and appended to the $array variable (as shown by Don). The if statement then allowed me to output data if I received any objects and vice versa.This $array variable was then inputted into the -HTMLfragments parameter, and ‘voila’ only the frags that had data would output to my webpage.)