Solved

Formatting Powershell ConvertTo-HTML output with linebreaks?

Posted on 2014-07-22
7
2,193 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
You could also try just joining with a space between each:

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

Accepted Solution

by:
footech earned 500 total points
Comment Utility
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
Comment Utility
@footech
Holy crap how did you do that
0
 
LVL 39

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
In this video we show how to create a Shared Mailbox 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 Recipients >> Sha…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now