Solved

Formatting Powershell ConvertTo-HTML output with linebreaks?

Posted on 2014-07-22
7
2,573 Views
Last Modified: 2014-07-23
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.
0
Comment
Question by:garryshape
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 40213120
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?
0
 

Author Comment

by:garryshape
ID: 40213127
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
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40213173
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.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 67

Expert Comment

by:sirbounty
ID: 40213178
You could also try just joining with a space between each:

Permissions = (Get-MailboxPermission -identity $User | select -ExpandProperty User) -join "&nbsp;"
}
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40213196
It's possible to join with "<br>", but unfortunately the ConvertTo-HTML cmdlets converts the greater than and less than signs to &lt; and &gt;, respectively.  This can also be a problem when you are trying to include an html link inside of a table when using that cmdlet.  One way around this is to just search and replace the HTML code responsible for this.  I had another question here where this was the case - http:Q_28043326.html

Here's the code, with a little formatting thrown in to make it pretty.
Function Convert-HTMLEscape {
 <#
 Convert &lt; and &gt; to < and >
 It is assumed that these will be in pairs
 Also convert $quot; to "
 #>
[cmdletbinding()]
Param (
 [Parameter(Position=0,ValueFromPipeline=$True)]
 [string[]]$Text
 )
  Process
  {
    foreach ($item in $Text)
    {
      if ($item -match "&lt;")
      {
        (($item.Replace("&lt;","<")).Replace("&gt;",">")).Replace("&quot;",'"')
      }
      else
      {
        #otherwise just write the line to the pipeline
        $item
      }
    }
  } #close process
} #close function

$head = @"
<style>
TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 2px;padding: 4px;border-style: solid;border-color: black;background-color:lightblue;text-align:left;font-size:14px}
TD{border-width: 1px;padding: 4px;border-style: solid;border-color: black;font-size:12px}
</style>
"@

$path = "C:\list.txt"
@(Get-Content $path) | ForEach `
{
    $name = $_
    Get-Mailbox -identity $name | select Alias,ForwardingAddress,
                                         @{n="Permissions";e={(Get-MailboxPermission -identity $name | Select -expand User -Unique | Sort) -join "<br>" }}
} | ConvertTo-HTML -Head $head | Convert-HTMLEscape | Out-File Mailboxes.htm

Open in new window

0
 

Author Comment

by:garryshape
ID: 40214569
@footech
Holy crap how did you do that
0
 
LVL 40

Expert Comment

by:footech
ID: 40214922
:)
A lot of what I've learned about PowerShell has been from researching questions that others have posed here.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In this video we show how to create an Accepted Domain in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Ac…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question