Piping and scoping issues – HTML report script

I’m working on HTML report that will include various bits of information required for prieodic processes - the report should contain all servers in one file.
When running the script with a -ComputerName parameter of multiple servers, I am able to display it as intended, as one file with each entry as a seperate table.
However, when piping get-content .\servers.txt to the script I only get the last server in the list.
I tried scoping the main HTML variable as Global, but I can’t figure out how clear it when I’m done, since doing so in the script itself clears it in between objects passed in the pipe.
If I don’t clear the $global:body then if I run the script a second time the information is doubled.

General gist of it:
<pre class="lang:ps decode:true ">[CmdletBinding()]
[string[]]$ComputerName = $env:COMPUTERNAME,
[ValidateScript({Test-Path $_ -PathType ‘Container’})]
[string]$Destination = (Get-Location)

Begin {
$date = get-date -Format G
$report = “temp.html”
Remove-Item $report
New-Item $report -type file | out-null

$header = @"
	TABLE{width: 100%;border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
	TH{border-width: 1px;padding: 10px;border-style: solid;border-color: black;}
	TD{border-width: 1px;padding: 15px;border-style: solid;border-color: black;}
	&lt;title&gt;Op Guide report $($date)&lt;/title&gt;

$footer = @"
$header | out-file $report -append


Process {
foreach ($computer in $ComputerName) {
Try {
$OS = Get-WmiObject -Class “Win32_OperatingSystem” -ComputerName $Computer -ErrorAction Stop | Select CSName,Caption
Catch {
Write-Warning “Unable to connect to $computer”
Write-Warning $_.Exception.Message
$unreachable = $True

	$global:body += "&lt;table&gt;"
	If($unreachable) {
		$global:body += "&lt;th colspan='2'&gt;" + $Computer + "&lt;/th&gt;"
		$global:body += "&lt;tr&gt;&lt;td colspan='2'&gt;&lt;p align='center'&gt;&lt;font color='red',align='center'&gt;Unreachable&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;"
		$unreachable = $null

#more code goes here to format information and add to table

End {
$global:body | out-file $report -append
$footer | out-file $report -append
invoke-item $report

Clear-Variable -Name Body -Scope Global

Edit, crayon massacre the html bits and opening tag for the code snip…

I tried the script as you have it above and I am not able to reproduce what you describe. The html file is created as you describe (one server per table, all servers in one file). When I run again, the original file is deleted before creating a new file. One thing I have noticed is that you do not seem to terminate the <table> tag, but maybe that is in the commented section where you indicate more code goes.

Pipelining the list of servers works for me too. Your code appears to be correct how it handles that scenario. You have a declared string array as a parameter and you enumerate over each one, and you also have it tagged to accept pipeline input. I’m not sure why you are getting different results.

Perhaps you could close out your Powershell console/session, reload the script and try again with this code.

Thanks for the reply Jonathan, had a bit of a noob day yesterday, was piping my list as | foreach {myscript.ps1 $_}
Instead of just | myscript.ps1

Also found out Here-Strings "@ termniator can’t have spaces before it.