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()]
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 = @"
<head>
<style>
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;}
</style>
<title>Op Guide report $($date)</title>
</head>
<body>
"@
$footer = @"
</body>
</html>
"@
$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 += "<table>"
If($unreachable) {
$global:body += "<th colspan='2'>" + $Computer + "</th>"
$global:body += "<tr><td colspan='2'><p align='center'><font color='red',align='center'>Unreachable</font></p></td></tr>"
$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…