Solved

Formatting Powershell ConvertTo-HTML output with linebreaks?

Posted on 2014-07-22
7
2,879 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
To show how to generate a certificate request 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 Servers >> Certificates…
how to add IIS SMTP to handle application/Scanner relays into office 365.
Suggested Courses

628 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