Variable / String Appends in Powershell

I'm in the process of building a PowerShell script which is making heavy use of ConvertTo-HTML to serve as the basis for a web-based reporting tool.

However, I've hit a brick wall with regard to how variables may be appended.

The script sometimes breaks out into a loop when multiple databases need to be interrogated. For  the first database, the HTML is built for the result set as a variable with the last script action being "ConvertTo-HTML  xxxxxx | Output-String".

For the second and subsequent databases, I simply want to append the output to the existing variable.

Any pointers greatly appreciated!
campbell1972Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
ConvertTo-Html will create a standalone HTML document. Added information to that is tricky once it's created. However, that would be a poor way to go about things.

ConvertTo-Html has a Fragment parameter. This generates a table out of an object.

Typically this means you can build a complex document like this:
$body = '<h1>Title 1</h1>'
$body += $results1 | ConvertTo-Html -Fragment
$body += '<h1>Title 2</h1>
$body += $results2 | ConvertTo-Html -Fragment

$document = ConvertTo-Html -Body $body | Out-String

Open in new window

You would often add a head (style information) at the same time. The point of the last step is only to create the wrapper document.

This will leave an empty table in the document, this can be problematic if you have particular styles in use. The Empty table can be removed like this:
$document = $document -replace '<table>\r?\n</table>'

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:
Addendum, if the intent is to push everything into a single table, you need to merge everything before ConvertTo-Html is called. Appending information to existing HTML tables would be painful.
0
campbell1972Author Commented:
Great tips Chris, thanks very much for that!

I can see exactly how I need to go about this now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.