Author Posts

May 22, 2013 at 6:29 am

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:

[CmdletBinding()]
Param
(
	[Parameter(ValueFromPipeline=$True)]
		[ValidateNotNullorEmpty()]
		[string[]]$ComputerName = $env:COMPUTERNAME,
	[Parameter(Mandatory=$false)]
		[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 = @"
		
		
		Op Guide report $($date)
		
		
	"@

	$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 += ""
		
		If($unreachable) {
			$global:body += ""
			$global:body += ""
			$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
}
" + $Computer + "

Unreachable

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

May 22, 2013 at 9:21 am

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

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.

May 23, 2013 at 1:03 am

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
Doh..

Also found out Here-Strings "@ termniator can't have spaces before it.
http://damiankarlson.com/2011/08/04/quick-tip-powershell-here-strings-dont-like-whitespace/