foreach object convert to html

Good afternoon;

In the following script is there a way to have the pre and post content appear only once as opposed for each object?

Thank you for your input

$names=Import-Csv C:\Tmp\WinRM_Services.csv | select -ExpandProperty MachineName
clear-content -Path ‘D:\PowerShell_temp\psversion_errors.txt’
clear-content -Path ‘D:\Powershell_temp\psversion.txt’
clear-content -Path ‘D:\Powershell_temp\psversion.html’
clear-content -Path ‘D:\Powershell_temp\psversion_message.txt’
$Title = “PSversion Analysis”
$footer = “report run $(Get-Date)”
$computername = $env:computername

$output1 = foreach ($name in $names) {
try {
#Invoke-Command -ComputerName $name -ScriptBlock {$PSVersionTable.psversion} -ErrorAction Stop | Out-File -FilePath ‘D:\Powershell_temp\psversion.txt’ -Append
Invoke-Command -ComputerName $name -ScriptBlock {$PSVersionTable.psversion} -ErrorAction Stop | `
ConvertTo-Html -Title $Title -PreContent “$Computername” -PostContent $footer | `
Out-File d:\powershell_temp\psversion.html -Append
#The is not needful for the script to run, helpful script documentation to indicate what errors were lookin note below how the value in the brackets was obtained.
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {

#Piping the exception message to the outfile worked.
$.Exception.Message | out-file -FilePath ‘D:\PowerShell_temp\psversion_errors.txt’ -Append
Write-Host "Message: [$($
.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
#The following two lines produces the same output as in psversion_errors.txt
$exception = $_.Exception.Message
Out-File -FilePath ‘D:\Powershell_temp\psversion_message.txt’ -Append -InputObject $exception

}
}

Just to don’t leave you without any answer at all … just an idea: You could “collect” all the needed data “inside” your foreach loop like you actually already do and output it and convert it to HTML after the loop has finished in one big chunk. This way you wouldn’t have multiple HTML documents in one file.
Or you use the parameter -Fragment for your Convertto-HTML inside your loop and create the header and footer and everything else you need seperately and assemble it later.

Olaf,
Thank you for your response, a little over my head. My powershell scripting skill is at a novice level, trying to do things that are at times over my head.

Norm

Something along these lines:

$output1 = foreach ($ComputerName in $names) {
    try {
        Invoke-Command -ComputerName $ComputerName -ScriptBlock {$PSVersionTable.psversion} -ErrorAction Stop
    } 
    catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
        $_.Exception.Message | out-file -FilePath 'D:\PowerShell_temp\psversion_errors.txt' -Append
        Write-Host "Message: [$($_.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
    }
}

$output1 | 
    ConvertTo-Html -Title $Title -PreContent "$Computername" -PostContent $footer |
    Add-Content -Path 'd:\powershell_temp\psversion.html'

Basically, allow the $output1 variable to store the output instead of outputting to a file, then convert the whole lot to HTML at once, and then output everything to a file at the end.

Alternate version which pipes the whole thing along at once without bothering to store it in a variable:

$names | ForEach-Object {
    try {
        Invoke-Command -ComputerName $_ -ScriptBlock {$PSVersionTable.psversion} -ErrorAction Stop
    } 
    catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
        $_.Exception.Message | out-file -FilePath 'D:\PowerShell_temp\psversion_errors.txt' -Append
        Write-Host "Message: [$($_.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
    }
} | ConvertTo-Html -Title $Title -PreContent "$Computername" -PostContent $footer |
    Add-Content 'd:\powershell_temp\psversion.html'

So you might start with learning the basics of Powershell from scratch. This thread lists some good starting points for your studies: Beginner sites and tutorials. (scroll down to the bottom)

And here I have a simple example of what I meant: With

$ServicesStartigWithA = Get-Service -Name a*
You store the result of this simple query in a variable. Then you can iterate over all single elements of this array and convert each single element to HTML and store the result in a file:
foreach ($Service in $ServicesStartigWithA) {
$Service |
Select-Object -Property Status,Name,DisplayName |
ConvertTo-Html |
Out-File C:\sample\sample_Uncool.html -Append
}
If you open this HTML file in a browser it looks bad.
If you “collect” the needed properties you want in a variable $output you can pipe this “outside your loop” in a big chunk to the Convertto-HTML and create a valid HTML file wich looks just fine in the browser.
$output = foreach ($Service in $ServicesStartigWithA) {
$Service |
Select-Object Status,Name,DisplayName
}
$output |
ConvertTo-Html |
Out-File C:\sample\sample_cool.html -Append

Another tip: please do not use backticks as line continuation charachters. They are actually unnecessary and are usually considered as really bad style. :wink:

Thank you Joel your script works perfectly!!

Norm