# Help required with script using Convertto-EnhancedHTML advanced function

**URL:** <https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122>\
**Category:** PowerShell Help\
**Created:** [October 28, 2015, 10:32am UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122 "2015-10-28T10:32:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![munster99](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@munster99](https://forums.powershell.org/u/munster99)\
**Post date:** [October 28, 2015, 10:32am UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/1 "2015-10-28T10:32:26Z")

</div>

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’

[CmdletBinding()]  
PARAM(  
[Parameter(Mandatory=$true,  
Position=0,  
HelpMessage = “Please enter a Servername”)]  
[ValidateNotNullOrEmpty()]  
[String]$computerName  
)  
BEGIN{  
$style = (Get-content ‘C:\css\style.css’)  
Get-Module OSTools | Out-Null

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

```
    $frag12 = Get-OsinfoVMInfo -VCName $computer | where { $_.name -ne (((($_.vmpathname).Split(']')[1]).Split('/'))[0].TrimStart(' '))} | Select Name,vmpathname |
              ConvertTo-EnhancedHTMLFragment -TableCssID IFolders_TCID &#8175;
                                             -DivCssID IFolders_DCID &#8175;
                                             -DivCssClass IFolders_DCC &#8175;
                                             -TableCssClass IFolders_TCC &#8175;
                                             -As Table &#8175;
                                             -Properties Name,vmpathname &#8175;
                                             -MakeHiddenSection &#8175;
                                             -MakeTableDynamic &#8175;
                                             -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 &#8175;
                                            -DivCssID Consolidation_Needed_DCID &#8175;
                                            -DivCssClass Consolidation_Needed_DCC &#8175;
                                            -TableCssClass Consolidation_Needed_TCC &#8175;
                                            -As Table &#8175;
                                            -Properties Name,'Consolidation Needed' &#8175;
                                            -MakeHiddenSection &#8175;
                                            -MakeTableDynamic &#8175;
                                            -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 &#8175;
                                            -DivCssID NIC_Connected_DCID &#8175;
                                            -DivCssClass NIC_Connected_DCC &#8175;
                                            -TableCssClass NIC_Connected_TCC &#8175;
                                            -As Table &#8175;
                                            -Properties Name,'NIC Connected','NIC Start' &#8175;
                                            -MakeHiddenSection &#8175;
                                            -MakeTableDynamic &#8175;
                                            -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 &#8175;
                                            -DivCssID InvalidVMs_DCID &#8175;
                                            -DivCssClass InvalidVMs_DCC &#8175;
                                            -TableCssClass InvalidVMs_TCC &#8175;
                                            -As Table &#8175;
                                            -Properties Name,'Inaccessible/Invalid VM' &#8175;
                                            -MakeHiddenSection &#8175;
                                            -MakeTableDynamic &#8175;
                                            -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" &#8175;
                           -PreContent "Morning Report: $(($VC).ToUpper())" &#8175;
                           -HTMLFragments $frag1,$frag2,$frag3,$frag4,$frag5,$frag6,$frag7,$frag8,$frag9,$frag10,$frag11,$frag12,$frag13,$frag14,$frag15 &#8175;
                           -PostContent "Time Retrieved $(Get-Date)" &#8175;
                           -CssStyleSheet $style |
    Out-File $path -Encoding ascii
} #foreach

```

} # Process  
END{} # End

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 28, 2015, 10:43am UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/2 "2015-10-28T10:43:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![munster99](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@munster99](https://forums.powershell.org/u/munster99)\
**Post date:** [October 28, 2015, 10:38pm UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/3 "2015-10-28T22:38:33Z")

</div>

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){  
Out-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  
munster99

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 29, 2015, 12:03am UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/4 "2015-10-29T00:03:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![munster99](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@munster99](https://forums.powershell.org/u/munster99)\
**Post date:** [October 29, 2015, 1:03am UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/5 "2015-10-29T01:03:34Z")

</div>

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 &#8175;
                                            -DivCssID NIC_Connected_DCID &#8175;
                                            -DivCssClass NIC_Connected_DCC &#8175;
                                            -TableCssClass NIC_Connected_TCC &#8175;
                                            -As Table &#8175;
                                            -Properties Name,'NIC Connected','NIC Start' &#8175;
                                            -MakeHiddenSection &#8175;
                                            -MakeTableDynamic &#8175;
                                            -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: (🙂 [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.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 29, 2015, 1:05am UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/6 "2015-10-29T01:05:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![munster99](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@munster99](https://forums.powershell.org/u/munster99)\
**Post date:** [October 29, 2015, 1:28am UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/7 "2015-10-29T01:28:01Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 29, 2015, 1:33am UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/8 "2015-10-29T01:33:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![munster99](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@munster99](https://forums.powershell.org/u/munster99)\
**Post date:** [October 29, 2015, 7:21pm UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/9 "2015-10-29T19:21:55Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/help-required-with-script-using-convertto-enhancedhtml-advanced-function/5122/10 "2024-05-16T20:44:32Z")

</div>


