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
Solved

Formatting Powershell ConvertTo-HTML output with linebreaks?

Posted on 2014-07-22
7
2,494 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
  • 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

840 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