Convertto-EnhancedHTMLFragment

Hello,

I need small help , in converting normal string to html format using convertto-enhancedhtml

Below is a sample code I am using. I want to output couple of variables in html page for multiple servers but I am not able to do that. What change do I need to make in the code.

I am getting the output in desired format but instead of the variables I get length value of the string.


$css = @"

body {

font-family:tahoma;
font-size:10pt;
color:#333;
padding:10px;

}

h1 {
text-allign:center;
border-top:1px solid #666;
border-bottom:1px solid #666;
}

h2 {

background-color:#666;

}
h3{
background-color:white;

}

"@

$server = ‘wdcqvap21’
foreach ($computer in $server){

   [string]$a1 =  Write-Output "Parameter file has been renamed for $computer !!"
    $frag1 = $a1 |ConvertTo-EnhancedHTMLFragment -as list | out-string


  [String]$a2 = Write-Output "Parameter file has been ammended for $computer !!"
    $frag2 = $A2 | ConvertTo-EnhancedHTMLFragment -as list | Out-String

      ConvertTo-EnhancedHTML -CssStyleSheet $css -HTMLFragments "$computer","$frag1","$FRAG2" | Out-File "c:\temp\syz.html"
}

I am able to get the desired output using convertto-html ,but I want to use convertto-enhancedHTMLFragments for the same piece of code, so that I can highlight cells that do not meet the condition and this can happen only if I use EnhancedHTMLFragments.


$css = @"

body {

font-family:tahoma;
font-size:8pt;
color:#333;
padding:10px;

}

h1 {
text-allign:center;
border-top:1px solid #666;
border-bottom:1px solid #666;
}

h2 {

background-color:#999;

}
h3{
background-color:white;

}

"@

$server = ‘wdcqvap21’
foreach ($computer in $server){

    $frag1 = ConvertTo-Html -as list -PreContent "Parameter file has been renamed for $server" | out-string


    $frag2 = ConvertTo-html -as List -PreContent "Parameter file has been amended for $server" | Out-String

      ConvertTo-HTML -head $css -body "$computer",$frag1,$frag2 | Out-File "c:\temp\syz.html"
}

PLEASE use the code formatting tags, as listed in the bullets above the posting text box. It makes your code a lot easier to follow. Thank you.

In this line:

ConvertTo-EnhancedHTML -CssStyleSheet $css -HTMLFragments "$computer","$frag1","$FRAG2" | Out-File "c:\temp\syz.html

There’s no need to include the variables in quotation marks.

But, more specifically:

[String]$a2 = Write-Output "Parameter file has been ammended for $computer !!"
$frag2 = $A2 | ConvertTo-EnhancedHTMLFragment -as list | Out-String

The EnhancedHTML commands are expecting objects as input. They’re designed to enumerate the object’s properties to construct a list or table. You’re passing in a string, which has a Length property, and so that’s what you’re getting. The commands weren’t designed to just display simple messages like this. You’re also going through a real unnecessary set of steps.

$a2 = "Parameter file has been ammended for $computer !!"

Is sufficient, and you can use $a2 in the fragment list for ConvertTo-EnhancedHTML.

may is say that long hard road would be this, but as Don said, will simply increase you script complexity:

foreach ($computer in $server){

[string]$a1 = Write-Output "Parameter file has been renamed for $computer !!"
[String]$a2 = Write-Output "Parameter file has been ammended for $computer !!" 

$props = @{ 'Renamed'= $a1 ;
            'Ammended'= $a2
          }
$Processed = New-Object -TypeName PSObject -Property $props

$params = @{'As'='list';
            'Properties'= 'Computer',
             @{n='Renamed';e={$_.renamed}},
             @{n='Ammended';e={$_.ammended}}
            }
           
$frags = ConvertTo-EnhancedHTMLFragment  -InputObject $Processed $params

ConvertTo-EnhancedHTML -CssStyleSheet $css -HTMLFragments $computer,$frags | Out-File "c:\temp\syz.html"
}

Thanks a lot for the clarity !!! :slight_smile: