Link to home
Start Free TrialLog in
Avatar of RichardPWolf
RichardPWolfFlag for United States of America

asked on

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.
Avatar of Brian Scholer
Brian Scholer

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.
Avatar of RichardPWolf

ASKER

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?
Avatar of footech
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.
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
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.
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 =
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.
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
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

That did it. Thanks Now to finish up I've just got to separate the servers on that report for easier reading.
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
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.