Convert-To-HTML works but ConvertTo-HTML -fragment doesn't

Here's my scenario; I have a script that produces an HTML report and am trying to incorporate it into an larger HTML report using the -fragment switch. Why doesn't it work?
Code that works;

Function OPorts {
      $process = "Open Ports"
      $reportfile = $strfolderpath + "Open Ports.html"
            $strnmap = nmap -sW -sV $strcomputer
            $nmap = @()
                  Foreach ($Line in $strnmap) {
                  $Object = New-Object -TypeName PSObject
                  Add-Member -InputObject $Object -Type NoteProperty -Name Log -Value $Line
                  $nmap += $Object
                  }
            $nmap | ConvertTo-HTML -head $pstyle -body "<H1>Open Ports</H1>" | Out-File $reportfile       
      }

code that doesn't work;

$nmap = @()
      $TotalPorts = @()

            foreach ($computer in $strComputer) {
                  $strnmap = nmap -sW -sV $computer
                        Foreach ($Line in $strnmap) {
                              $Object = New-Object -TypeName PSObject
                              Add-Member -InputObject $Object -Type NoteProperty -Name Log -Value $Line
                  $nmap += $Object
                  }
            $nmap | ConvertTo-HTML -fragment
            $TotalPorts += $nmap
            }
#      $TotalPorts | ConvertTo-HTML -fragment

On the "fragment" code I'll get a printout on the console of the HTML with the correct information, but it won't appear in the report. I have several other items in this HTML report that work fine.

BTW; this is the code that inserts it into the report.

<h3>Open Ports</h3>
      <p>The following is a summary of the Open Ports</p>
      <table class="normal">
      $TotalPorts ****I've also used $nmap here ******
      </table>

I'm on Version 3 of powershell.
Thanks in advance.
RichardPWolfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian ScholerCommented:
In the OPorts function, you pipe the result of ConvertTo-Html to Out-File and that's how it gets written.

In the code where you use -Fragment, you never write it out to a file, which is why you see it on the screen but the file doesn't get updated.
RichardPWolfAuthor Commented:
OK, understand but it gets written to the report using the last bit of code at the bottom of my post. It's part of a larger HTML report and I've used the fragment switch on many other parts of the report and it works fine. Do you want to see some other items that "are" successfully written to the report?
footechCommented:
I see nowhere where $nmap or $TotalPorts is converted to html and stored.  Just piping to ConvertTo-HTML doesn't do anything (besides output to screen) unless you store the results.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

RichardPWolfAuthor Commented:
OK, I'll post the "entire" script.

This started out from a script I found to report on some server information. I've been adding to it.

As you can see there's a lot  going on. the file gets written later in the script after "ALL" content is added.
E--Scripts-Compliance-TP-Month-End-Syste
RichardPWolfAuthor Commented:
I re-read your comment footech and I believe where you're coming from is how does it get to a variable after conversion to html. That is the big question as I've tried many ways to do that so I see where you're going. I've just not been able to successfully get the result to a variable to use in the report.
footechCommented:
There are a number of similiar lines in the code you posted.  Something like
$nmap = $nmap | ConvertTo-HTML -fragment

Open in new window


Remember, every time (almost) you're setting the contents of a variable you'll start with
$variablename =
RichardPWolfAuthor Commented:
My code is pretty sloppy right now. I'll be cleaning it up after I know everything works.

So to get the information in the variable I need to use this "$nmap = $nmap | ConvertTo-HTML -fragment" or something similar? If so I think this is one of the few methods I've tried.
RichardPWolfAuthor Commented:
It's better. At least I'm seeing something. Now is to figure out why it's displaying; (scroll to the bottom to see anything useful).

*

7
27
19
19
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
21
20
19
18
98
63
45
70
79
57
18
111
77
8
 
Starting Nmap 6.47 ( http://nmap.org ) at 2015-08-05 14:34 Central Daylight Time
Nmap scan report for WSUS (192.168.6.58)
Host is up (0.00061s latency).
rDNS record for 192.168.6.58: wsus.hoodview.fcu
All 1000 scanned ports on WSUS (192.168.6.58) are closed
MAC Address: 00:50:56:BC:5F:C8 (VMware)
 
Service detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 2.40 seconds
footechCommented:
The first bit is what you'll see when you pass a string (rather than an object with a property that is a string type) to ConvertTo-HTML.  The number is actually the length of the string.

I think you've got the pipe to ConvertTo-HTML within the loop.
Try something like this.
$nmap = @()
foreach ($computer in $strComputer) {
    $strnmap = nmap -sW -sV $computer
    Foreach ($Line in $strnmap) {
        $Object = New-Object -TypeName PSObject
        Add-Member -InputObject $Object -Type NoteProperty -Name Log -Value $Line
        $nmap += $Object
    }
}
$nmap = $nmap | ConvertTo-HTML -fragment

Open in new window

RichardPWolfAuthor Commented:
That did it. Thanks Now to finish up I've just got to separate the servers on that report for easier reading.
RichardPWolfAuthor Commented:
OK, one last bit of code. Using my code;
$nmap = @()
foreach ($computer in $strComputer) {
    $strnmap = nmap -sW -sV $computer
    Foreach ($Line in $strnmap) {
        $Object = New-Object -TypeName PSObject
        Add-Member -InputObject $Object -Type NoteProperty -Name Log -Value $Line
        $nmap += $Object
    }
}
$nmap = $nmap | ConvertTo-HTML -fragment

I'd like to add the computer name before each loop of the nmap. What I thought would work (but didn't is the following.
$nmap = @()
foreach ($computer in $strComputer) {
$NMAP += $COMPUTER <- Upper case for illustrative purposes only.
foreach ($computer in $strComputer) {
    $strnmap = nmap -sW -sV $computer
    Foreach ($Line in $strnmap) {
        $Object = New-Object -TypeName PSObject
        Add-Member -InputObject $Object -Type NoteProperty -Name Log -Value $Line
        $nmap += $Object
    }
}
$nmap = $nmap | ConvertTo-HTML -fragment
footechCommented:
This is straying beyond the original question.
I'm limited in what I can suggest without really examining the script and its output, and making a determination on what I think looks best (which I don't really have time for).

It seems to me that this output (from nmap) isn't really suited for a table, but we're shoe-horning it in anyway.  When you try to add other things like a header it compounds the issue.  However, continuing with the shoe-horning approach, here's something that should work (also optimized the code a bit).
$nmap = @()
foreach ($computer in $strComputer) {
    $nmap += New-Object -TypeName PSObject -Property @{ Log = $computer }
    $strnmap = nmap -sW -sV $computer
    foreach ($Line in $strnmap) {
        $nmap += New-Object -TypeName PSObject -Property @{ Log = $Line }
    }
}
$nmap = $nmap | ConvertTo-HTML -fragment

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RichardPWolfAuthor Commented:
That my friend worked great. Thank you. At some point I'd like to add a blank line between each scan but that's for a later date. Again Thank you very much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.