We help IT Professionals succeed at work.

Format the content in powershell result

Simon Leung
Simon Leung used Ask the Experts™
on
Any idea how to align the space properly to see the the email content sent from powershell ? Thx

$emailFrom = "xxxx@gmail.com"
$emailto = "xxx@abc.com"
$pass = "xxxx"
$smtpServer = "smtp.gmail.com"
 
 
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.EnableSsl = $true

$msg.From = "$emailFrom"  
$msg.To.Add("$emailto")
$msg.BodyEncoding = [system.Text.Encoding]::Unicode
$msg.SubjectEncoding = [system.Text.Encoding]::Unicode
$msg.IsBodyHTML = $true  
$msg.Subject = "Test mail from PS"
$output =  repadmin /replsummary *
$msg.Body = $output
$SMTP.Credentials = New-Object System.Net.NetworkCredential("$emailFrom", "$pass");
$smtp.Send($msg)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shabarinath RamadasanInfrastructure Architect

Commented:
Hello,

Either you need to have the entire result on HTML by  adding the required HTML code lines into the result through your own logic in the code or you need to relay on converting the result into HTML.

You can convert the result using the commandlet - ConvertTo-Html

Cheers !

Author

Commented:
Already tried but it doesn't align the result nicely....
Shabarinath RamadasanInfrastructure Architect

Commented:
Here is one dashboard I made for Hyper-V servers.
You can get an insight on how the result is made into HTML format.

https://gallery.technet.microsoft.com/scriptcenter/HyperV-Dash-Board-VM-Disk-299bac7d

Though its lenghty, worth trying but you may need to a different commandlet to fetch the replication health ratherthan using repadmin.

Check this

https://techcommunity.microsoft.com/t5/itops-talk-blog/powershell-basics-how-to-check-active-directory-replication/ba-p/326364

Cheers!
Messaging and Directory Services
Commented:
Try this code:
if not working, remove below
 $msg.BodyEncoding = [system.Text.Encoding]::Unicode
 $msg.SubjectEncoding = [system.Text.Encoding]::Unicode
and try

$output = repadmin /replsummary *

$output1 = $output | ? { $_ -ne "" }


$SourceDSARowStart = [array]::IndexOf($output1,$($output1 | ? { $_ -like "source DSA*" }))
$DestinationDSARowStart =  [array]::IndexOf($output1,$($output1 | ? { $_ -like "Destination DSA*" }))


$SourceDSARowEnd =  $DestinationDSARowStart -1


$ErrorsIndexRowStart = [array]::IndexOf($output1,$($output1 | ? { $_ -like "Experienced*" }))

$DestinationDSARowEnd = $ErrorsIndexRowStart -1

if($ErrorsIndexRowStart -ne -1)
{
    $ErrorRowStart = $ErrorsIndexRowStart +1
    $ErrorRowEnd = $output1.Count -1
}


$SourceDSADataRows = $output1[$($SourceDSARowStart+1)..$($SourceDSARowEnd)]

$SourceDSAResults = @()

$SourceDSADataRows | % { 

    $data = $_.Split(" ") | ? { $_ -ne ""}

    $SourceDSAData = '' | select SourceDSA, LargestDelta, Fails, Total, PercentageError
    $SourceDSAData.SourceDSA = $data[0]
    $SourceDSAData.LargestDelta = $data[1]
    $SourceDSAData.Fails = $data[2]
    $SourceDSAData.Total = $data[4]
    $SourceDSAData.PercentageError = $data[5]
    $SourceDSAResults += $SourceDSAData
}


$DestinationDSADataRows = $output1[$($DestinationDSARowStart+1)..$($DestinationDSARowEnd)]

$DestinationDSAResults = @()

$DestinationDSADataRows | % { 

    $data = $_.Split(" ") | ? { $_ -ne ""}

    $DestinationDSAData = '' | select DestinationDSA, LargestDelta, Fails, Total, PercentageError

    $DestinationDSAData.DestinationDSA = $data[0]
    $DestinationDSAData.LargestDelta = $data[1]
    $DestinationDSAData.Fails = $data[2]
    $DestinationDSAData.Total = $data[4]
    $DestinationDSAData.PercentageError = $data[5]
    $DestinationDSAResults += $DestinationDSAData
}

$OperationalErrorDataRows = $output1[$($ErrorsIndexRowStart +1)..$($output1.Count -1)]


$OperationalResults = @()

$OperationalErrorDataRows | % { 


    $data = $_.Split(" ") | ? { $_ -ne ""}

    $OperationalErrorData = '' | select ErrorCode, ComputerFQDN

    $OperationalErrorData.ErrorCode = $data[0]
    $OperationalErrorData.ComputerFQDN = $data[2]
    
    $OperationalResults += $OperationalErrorData
}


$Style = "
<style>
    BODY{background-color:#b0c4de;}
    TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
    TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#778899}
    TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
    tr:nth-child(odd) { background-color:#d3d3d3;} 
    tr:nth-child(even) { background-color:white;}    
</style>
"

$html = $SourceDSAResults | ConvertTo-HTML -AS Table -Fragment | out-string
$body = ConvertTo-HTML -head $Style -PreContent $html 


$html = $DestinationDSAResults | ConvertTo-HTML -AS Table -Fragment | out-string
$body += ConvertTo-HTML -head $Style -PreContent $html 


$html = $OperationalResults | ConvertTo-HTML -AS Table -Fragment | out-string
$body += ConvertTo-HTML -head $Style -PreContent $html 



####### gmail

$emailFrom = "xxxx@gmail.com" 
 $emailto = "xxx@abc.com"
 $pass = "xxxx"
 $smtpServer = "smtp.gmail.com" 
  
  
 $msg = new-object Net.Mail.MailMessage 
 $smtp = new-object Net.Mail.SmtpClient($smtpServer) 
 $smtp.EnableSsl = $true 

 $msg.From = "$emailFrom"  
 $msg.To.Add("$emailto") 
 $msg.BodyEncoding = [system.Text.Encoding]::Unicode 
 $msg.SubjectEncoding = [system.Text.Encoding]::Unicode 
 $msg.IsBodyHTML = $true  
 $msg.Subject = "Test mail from PS" 
 
 $msg.Body = $body
 $SMTP.Credentials = New-Object System.Net.NetworkCredential("$emailFrom", "$pass"); 
 $smtp.Send($msg) 

Open in new window

Top Expert 2014

Commented:
The issue really just has to do with the differences between plain-text and HTML, and fixed-width vs. variable-width fonts.  Typically HTML will specify which font (or font family) to use for displaying text, and usually defaults to a variable-width font, e.g. Times New Roman, Arial, Calibri.  Programs displaying plain-text files (the file itself has no info about what font or other text styles should be used) usually default to using a fixed-width font, e.g. Lucida Console, Courier New, Consolas.

The output from repadmin /replsummary is just plain-text.  There are a few approaches to sending an email with that output while maintaining the same appearance that you see running the command in a console (which by default will use a fixed-width font).
1.  Send the email as plain-text, with the output within the email body.
2.  Send the email as either plain-text or HTML, but have the repadmin output attached as a plain-text file.
3.  Send the email as HTML, with the output within the email body.
I'll expand on each option below.

1.  Send the email as plain-text, with the output within the email body.
This is the simplest, but has the downside that the display of plain-text email is completely dependent on the email client.  You can configure one email client to use a fixed-width font to display plain-text emails, but that won't affect viewing it with any other client.  It's a simple change to make in Outlook.

2.  Send the email as either plain-text or HTML, but have the repadmin output attached as a plain-text file.
Really about the same complexity as option #1, but now the display of the text file is up to whatever program you use for text files (like Notepad) rather than the email client.

3.  Send the email as HTML, with the output within the email body.
This is much more complicated, and generally requires parsing that text output in order to create PS objects which can then be piped to ConvertTo-Html to create HTML tables. Text alignment then comes with the alignment of the tables/cells.  Coding the parsing of the text can be error-prone.  Haven't tested, but it appears to be the approach Raheman is trying above.
A simpler take on this is to just convert specific characters of the plain-text into their HTML equivalents (like newline into "<br>", spaces into "&nbsp"), essentially manually creating your HTML email body.
If you can use PS cmdlets to provide the information (like some in the links provided by Shabarinath), then you can save yourself from the text parsing and instead use more common PowerShell techniques to reference the desired properties of the returned objects, perhaps combing with output from other cmdlets, and then passing on to ConvertTo-Html.