Convertto-html Question

by StrickPS at 2012-10-11 22:26:05

Hi,

I’m a noob into the Powershell world, but enjoying all that I am learning. I work for a company that has been down sizing, and I’m on the go from the time I get to work well into the late hours of the night, with wanting to keep my job. So, I would greatly appreciate some help to make my working world a little easier.

I have taken a lot of scripts and converted them into html pages, but I am not needing to combine several of these into 1 html page from several servers to speed up the process of daily monitoring.

I use this script, but I can’t figure out how to have many servers combine into 1 page:

Clear
$Date = Get-Date
$Servers = Get-Content C:\Solutions\Input\servers.txt
$Log = "System"

foreach ($computer in $Servers) {
$Proc = Get-Eventlog $Log -EntryType Error -newest 10 | select MachineName, TimeGenerated, EventID, Source, Message}

$Proc | Convertto-html -Head "Solutions" -Body "Daily Server Application Logs - $Date" | ft MachineName, TimeGenerated, EventID, Source, Message |-cssuri C:\inetput\www\Logs\css\style.css | Out-file -Filepath C:\Solutions\test\2.html

Invoke-Expression C:\Solutions\test\2.html



I would really appreciate any help with this, I would be able to convert a lot of scripts with being able to see one that actually works.

Cheers,

Strick
by Klaas at 2012-10-12 00:58:26
What if you just add a ‘+’:
[quote]$Proc = Get-Eventlog $Log -EntryType Error -newest 10 | select MachineName, TimeGenerated, EventID, Source, Message}[/quote]
becomes:
$Proc += Get-Eventlog $Log -EntryType Error -newest 10 | select MachineName, TimeGenerated, EventID, Source, Message}
by JeffH at 2012-10-12 04:24:50
You have a number of issues going on with this code. Adding data to an array is the right idea. But you line to create the HTML is incorrect. First, you already select the properties so you don’t need to get them again. And using Format-table is wrong here anyway. What you are doing is taking the HTML output and trying to turn that into a table. You also have the parameter for the CSS in the wrong place. See if this works a little better for you.


Clear
$Date = Get-Date
$Servers = Get-Content C:\Solutions\Input\servers.txt
$Log = "System"

$proc = @()

foreach ($computer in $Servers) {
$Proc += Get-Eventlog $Log -EntryType Error -newest 10 |
select MachineName, TimeGenerated, EventID, Source, Message
}

$Proc | Convertto-html -Head "Solutions" -PreContent "Daily Server Application Logs - $Date" -cssuri C:\inetput\www\Logs\css\style.css |
Out-file -Filepath C:\Solutions\test\2.html


The piped input to Convertto-html becomes the body. I think what you wanted was a line to go before the table.
by StrickPS at 2012-10-12 22:23:13
Hi,

I really appreciate the assistance. Unfortunately, I’m still only able to pull information in for one server, not a list of servers. I tried making the changes as suggested, with the $Proc = @() and $proc +=, all it did was append to the 2.html document and nullified the -newest 10.
by JeffH at 2012-10-14 12:58:44
I think you need to post the code you are using now. I suspect you have some things out of order still.
by StrickPS at 2012-10-20 17:00:19
Hi, sorry for the delay, had to rebuild my testing environment with Windows Server 2012.

I tried the code that was submitted, but it will only convert 1 server into the html file and it does not display only the newest 10, it will have 20 to 30. I have 3 system setup with Powershell 3.0 for use in my testing environment. As requested, this is the code:

Clear
$Date = Get-Date
$Servers = Get-Content C:\Sirius-Solutions\Input\servers.txt
$Log = "System"

$Proc = @()

foreach ($Computer in $Servers) {
$Proc += Get-Eventlog $Log -EntryType Error -newest 10 | select MachineName, TimeGenerated, EventID, Source, Message }

$Proc | Convertto-html -Head "Solution" -Precontent "Daily Server Application Logs - $Date" -cssuri C:\inetpub\wwwroot\Sirius-Solutions\CSS\Style.CSS | Out-File -FilePath C:\inetpub\wwwroot\Sirius-Solutions\4.html



I am able to get more than 1 server if I use the following code, but it’s all together, and I need it to be broken into separate tables for each server:

Clear

Remove-Item C:\inetpub\wwwroot\Sirius-Solutions\2.html

$Date = Get-Date
$Servers = Get-Content C:\Sirius-Solutions\Input\servers.txt
$Log = "System"

get-Eventlog -LogName $Log -Newest 10 -Computername $Servers | select Machinename, TimeGenerated, EventID, Source, Message | convertto-html -head "Sirius-Solutions" -Body " - Daily $Log Event Log - Last 10 Run on $Date" -Precontent "<h1>Sirius Solutions - Small Business Monitoring Solution</h1>" -cssuri C:\inetpub\wwwroot\Sirius-Solutions\CSS\Style.CSS | Out-File -Filepath C:\inetpub\wwwroot\Sirius-Solutions\2.html

Invoke-Expression C:\inetpub\wwwroot\Sirius-Solutions\2.html
by megamorf at 2012-10-21 09:25:26
How about this?

# create array of servers (3 times my local pc)
$servers = "$env:computername","$env:computername","$env:computername"

ConvertTo-Html -Title "Daily Report $(get-date -format g)" | out-file C:\temp\htmlreport.html

foreach($server in $servers)
{
Get-Eventlog System -Newest 10 -ComputerName $server | select Machinename, TimeGenerated, EventID, Source, Message | ConvertTo-Html | out-file C:\temp\htmlreport.html -Append
}

invoke-item C]
by StrickPS at 2012-10-21 10:55:06
Thanks for your assistance, appreciate it.

That is getting closer, but I need to read from a text file as i will be creating 40 reports to run and need to be able to add/remove servers/workstations quickly, don’t want to spend a lot of time editing the scripts with putting in the system names.

I ran a windows update on my systems last night, and now I’m getting errors for scripts that would read from a text file, but since the Windows update on 1 Server 2012 and 2 Windows 7 workstations (test platform). Now, on all 3 systems when trying to run any script that is using the Get-Content C:\File\array.txt, it’s displaying "The argument is null or empty, cannont validate". All 3 systems were built in the last 2 days, have had the execution policy and enable-psremoting setup. Is there anything I’m missing or a patch to resolve this?
by megamorf at 2012-10-21 11:32:00
I just needed a collection of computernames, whether they are read from a text file or a variable doesn’t matter for the rest ot the code.
The line could be $servers = Get-Content C:\temp\serverlist.txt.

I haven’t run into ps problems with Windows 8 yet. Have you tried specifying a path parameter for Get-Content? Is the path to the file correct? Does it contain special characters? Show us the console output and try invoking Get-Content with the -verbose switch.
by StrickPS at 2012-10-21 11:48:56
The path is correct: $Servers = Get-Content C:\Solutions\Input\servers.txt This worked on all 3 systems until the windows update. With the windows update, on the 2012 Server it will not connect to any other system and it is a domain controller also as the DNS system and the other 2 systems are joined. Totally clueless as everything else works on this test platform, email, iis, wds, sql, and every other feature I could setup, but Powershell 3.0 stopped working correctly. I was able to try your script on the Windows 7 System, but it would only get information from 2 systems. I’m trying to resolve this now, seeing this on google quite a bit, but no resolution that I see, yet.
by megamorf at 2012-10-21 12:00:28
So the following returns and error instead of your computerlist?

$servers = Get-Content C:\Solutions\Input\servers.txt
$servers

What does this say?
Get-Content C:\Solutions\Input\servers.txt -verbose

How is your file formatted? One computer per line I assume?
by StrickPS at 2012-10-21 12:17:27
This is the actual script, with the -verbose, and the results with the errors. The servers.txt file is 3 computers, 1 on each line:

Training
Coolmist
Oceanbreeze

Script:
Clear

Remove-Item C:\inetpub\wwwroot\Sirius-Solutions\PSTest.html

# create array of servers
$Servers = Get-Content C:\Sirius-Solutions\Input\servers.txt -verbose

Convertto-HTML -Title "Daily Report $(get-date -format g)" | out-file C:\inetpub\wwwroot\Sirius-Solutions\PSTest.html

foreach($Server in $Servers) {
Get-Eventlog System -Newest 10 -Computername $Server | select Machinename, TimeGenerated, EventID, Source, Message | Convertto-Html -cssuri C:\inetpub\wwwroot\Sirius-Solutions\CSS\Style.CSS | out-file C:\inetpub\wwwroot\Sirius-Solutions\PSTest.html -Append }

Invoke-Expression C:\inetpub\wwwroot\Sirius-Solutions\PSTest.html


Results

et-Eventlog : The network path was not found.
At C:\Sirius-Solutions\Scripts\GettingThere.ps1:11 char:1
+ Get-Eventlog System -Newest 10 -Computername $Server | select Machinename, TimeG …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:slight_smile: [Get-EventLog], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.GetEventLogCommand

Get-EventLog : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\Sirius-Solutions\Scripts\GettingThere.ps1:11 char:46
+ Get-Eventlog System -Newest 10 -Computername $Server | select Machinename, TimeG …
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:slight_smile: [Get-EventLog], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetEventLogCommand

Get-EventLog : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\Sirius-Solutions\Scripts\GettingThere.ps1:11 char:46
+ Get-Eventlog System -Newest 10 -Computername $Server | select Machinename, TimeG …
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:slight_smile: [Get-EventLog], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetEventLogCommand
by DonJ at 2012-10-21 12:32:10
Your $Server has blank lines. Look at your text file.

Also be aware that you’re munging multiple HTML documents into a single file. While IE will deal, it’s technically illegal HTML. Look into the -Fragment option of ConvertTo-HTML - that lets you produce just a piece of the overall HTML page. I did an example at http://www.windowsitpro.com/blog/powers … 3-3-141151 which does that. Requires a bit of re-thinking, but you’ll get a more stable and valid result. You also pick up more flexibility.

The basic trick is to concantenate a bunch of HTML fragements, rather than a bunch of complete HTML pages. You then insert the fragments into the body of a complete HTML page, giving you just one set of <HTML> tags at the end of the day.
by StrickPS at 2012-10-21 13:18:25
DonJ, you called it, space in the text file with server names. Works great with the scripting idea that Megamorf provided. Thank you to all that provided help to me, much appreciated.
by StrickPS at 2012-10-21 17:54:31
Just as a follow up, I was right about the Windows update causing an issue. If scripts stop returning information on individual systems, check the services for Remote Registry, it has to be set to Automatic and started. I have now confirmed this on 4 systems.

Cheers
by DonJ at 2012-10-21 19:41:48
An excellent reason to use Remoting instead ;). Lets you run the commands "locally" without relying on that service… which MS desperately wants to go away (and I’m sure will remove at some point).
by DonJ at 2012-10-24 11:59:49
As a note… free book on creating HTML reports now available. https://powershell.org/books.
by cookie.monster at 2012-10-27 08:05:27
On a side note, if you are interested in generating ‘prettier’, filterable output akin to out-gridview, check out this blog post from Douglas Finke.

*edit - just noticed your post Don, thanks for the heads up!