Automation

 View Only
  • 1.  Conditional HTML tag for my PowerCLI report

    Posted Jan 04, 2019 08:12 PM

    I'm trying to create a conditional tag in my PowerShell/PowerCLI report.  I feel as though I'm missing something very simple.  I have reports with CSS and they come out correct, so I have an IF statement with an ID of 'Red' and I have the CSS color the background Red.  I'm certain that my CSS is not the problem, because I've used it for years, but the way I try and add the tag is causing problems (I think).  Here is what I have:

    $css = (gc C:\MyCSS.css)

    #$allVMs = Get-VM

    $AllReport = @()

    $allVMs | where { $_.PowerState -eq "PoweredOn" -and ($_ | Get-HardDisk).StorageFormat -ne "Thin" } | Sort Name | foreach {

        $vm = $_

        $disktype = ($vm | Get-HardDisk | select -ExpandProperty StorageFormat)

        $report = '' | Select Name, Disktype

        $report.Name = $vm.Name

        $report.Disktype = If ($disktype -ne 'Thin') { ("<a id='Red'>" + $disktype + "</a>") } #I"m sure this is the problem

        $allreport += $report

    }

    $allreport | ConvertTo-Html -Head "<style>$css</style>" -Body @"

    <p><a id='Red'>This</a> is a test </p>

    "@ | Out-File C:\MyOutput\Report.html

    To make sure I had the CSS correct, I gave the word "This" the id tag of Red, and in my report its background color is red.  The problem is that the line that starts with $report.Disktype.  The tag is added, sort of.  When I look at the html, it shows this <tr><td>VMname</td><td>&lt;a id=&#39;Red&#39;&gt;EagerZeroedThick&lt;/a&gt;</td></tr>

    The HTML for the word "This" with the id of Red shows this for its HTML:

    <p><a id='Red'>This</a> is a test </p>

    I think it has something to do with the quotes, so I've tried making the double quotes single, and the singles double, I also tried using a subexpression, but that didn't work.  I also attached the HTML reports appearance, it shows the same in Firefox or Chrome.

    Any help is appreciated.



  • 2.  RE: Conditional HTML tag for my PowerCLI report
    Best Answer

    Posted Jan 04, 2019 08:39 PM

    You might want to have a look at Re: Generate HTML Report with cell colors

    I used the style attribute on the <p> tag.



  • 3.  RE: Conditional HTML tag for my PowerCLI report

    Posted Jan 04, 2019 11:06 PM

    Thank you, it was piping the data to Out-String, then the foreach to replace the "&lt;" and other characters.  I thought I could somehow make the change as part of the way I input the HTML tags in quotes, but after I made the change, it works now.  Thank you so much.  You're a one man PowerEverything tutorial.