Link to home
Start Free TrialLog in
Avatar of Garry Shape
Garry ShapeFlag for United States of America

asked on

Formatting Powershell ConvertTo-HTML output with linebreaks?

Someone helped make a script for me here but I kind of have a twist on it I'm trying to accomplish

What this code here does is import a list of usernames to run Exchange commands against (Get their Alias,ForwardingAddress, then another nested command that gets mailbox permissions (people who have permissions against that mailbox).

$path = "H:\mailboxexports.txt"
@(Get-Content $path) | ForEach `
{
$name = $_
Get-Mailbox -identity $name | select Alias,ForwardingAddress, @{n="Permissions";e={(Get-MailboxPermission -identity $name | Select -expand User)}}
} | ConvertTo-HTML | Out-File H:\Mailboxes.htm

Open in new window


Problem is now I'm trying to convert it out to HTML.
When I do that, the "Permissions" column just kind of puts everything clumped together in its row's table data.

So attempting to add back into the code, replacing
@{n="Permissions";e={(Get-MailboxPermission -identity $name | Select -expand User)}}

Open in new window

with
@{n="Permissions";e={(Get-MailboxPermission -identity $name | Select -expand User -join "`r`n)}}

Open in new window

The html file output still shows clumped together, however, if I view the source of the webpage, I can indeed see the carriage returns in the source code in the Permissions column.

But not in the rendered HTML viewing the actual webpage

I tried doing -join "<BR>" but that just literally displays the <BR> tag in the rendered HTML, it doesn't force a line break.
Avatar of sirbounty
sirbounty
Flag of United States of America image

The way I see it, you're trying to display one alias and multiple permissions...
So the issue is layout.  
You can have repeated Aliases with each permission:

Alias1   Perm1
Alias1   Perm2
Alias1   Perm3

etc.  Or, you can generate a table with some rowspanning:


Alias1   Perm1
             Perm2
             Perm3

Which should produce better output...  But I'm not clear on what you're wanting for output.  Can you elaborate?
Avatar of Garry Shape

ASKER

Here's what I've got

$UserList = "H:\mailboxexports.txt"
$MasterList = (Get-Content $UserList)
foreach ($User in $MasterList) {
$MyObject = New-Object PSObject -Property @{
EmailAddress = (Get-Mailbox $User).Alias
ForwardingAddress = (Get-Mailbox $User | select ForwardingAddress)
Permissions = (Get-MailboxPermission -identity $User | select User)
}
$MasterList += $MyObject
}
$MasterList

Open in new window


So far working with console output then wil try to HTML

But in the console output it adds "@{" before each thing and encloses with a }
And it also lists every item in the list from the text file before proceeding to run the script. not sure how to eliminate those
That's because it's a multi-valued result (the @{...)
How did you want the output displayed?  I can modifiy it for how you want the html to render.
You could also try just joining with a space between each:

Permissions = (Get-MailboxPermission -identity $User | select -ExpandProperty User) -join "&nbsp;"
}
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@footech
Holy crap how did you do that
:)
A lot of what I've learned about PowerShell has been from researching questions that others have posed here.