Author Posts

January 1, 2012 at 12:00 am

by adoshell at 2013-04-25 13:31:35

Hi all,

I'm an absolute beginner when it comes to PowerShell. I'm working on a script which searches a list of machines for a specific file extension and then writes it to an HTML file (Report).

The problem I'm having is the HTML report keeps repeating the "Titles" such as "Name" and "Creation Time". What would be the best way to only have the "header" column listed once?

Here is the code;

$a = "


$list = Get-Content -Path "c:\Scripts\PCnames.txt"

foreach ($computer in $list)
$check_exe = Get-ChildItem -Path "\\$computer\c\logfiles" | ?{$_.extension -eq ".exe"}| Select-Object "$computer", name, CreationTime | ConvertTo-HTML -head $a `
| Out-File -filepath "c:\scripts\exe_report.html" -append

Appreciate your feedback!

by poshoholic at 2013-04-25 13:44:20

You need to push your ConvertTo-HTML block outside of your foreach loop. Here's one way you could do that:
$a = "


$list = Get-Content -Path "c:\Scripts\PCnames.txt"

$check_exe = $(foreach ($computer in $list) {
Get-ChildItem -Path "\\$computer\c\logfiles" | ?{$_.extension -eq ".exe"}| Select-Object "$computer", name, CreationTime
}) | ConvertTo-HTML -head $a | Out-File -filepath "c:\scripts\exe_report.html" -append

That will generate one report for the entire loop instead of one report for each time the loop is invoked.

by adoshell at 2013-04-25 13:58:16

Hi Kirk,

Thanks on your reply and it makes sense to me. I tried running your code but here is an issue I ran into;

Select-Object : Empty string is not allowed.
At line:11 char:95


by poshoholic at 2013-04-25 18:55:21

Two things:

1. I see another issue. You want a columns showing the computer name, but you're going about it the wrong way. Instead of:
...| Select-Object "$computer", name, CreationTime
do this:
... | Select-Object @{Name='ComputerName';Expression={$computer}},Name,CreationTime
That will put the name of the computer in a ComputerName property. The other way your script was trying to use the name of the computer as a property name in your report.

2. I think you have a blank line in your PCnames.txt file (maybe a single blank line at the end of the file). That is the most logical explanation for the error you are reporting. Pipe the results of your call to Get-Content to Where-Object {$_} so that you strip out any blank lines you get from the PCnames.txt file.