Solved

PowerShell

Posted on 2014-09-16
8
152 Views
Last Modified: 2014-09-16
The following script works fine. However, if I add the following, it returns expected result. Any idea ? How can I add a heading in front of the message ? Tks

unexpected result
--------------------------
$Body = "Location:" + $Body + "`n" + $Body1

Location            
@{Name=backup_2014_09_15_230003_8368484.bak; LastWriteTime=09/15/2014 23:00:14}

Original Result
---------------------
Name                                                                                LastWriteTime                                              
----                                                                                     -------------                                              
backup_2014_09_02_230001_6570526.bak              2/9/2014 23:00:10                                          
backup_2014_09_03_230001_4905028.bak              3/9/2014 23:00:10      



Original Script
--------------------
$Body = Get-ChildItem \\nas\Backup\HR2 -Recurse |
   Select Name,LastWriteTime | sort-object -property LastWriteTime


$Body1 = Get-ChildItem \\nas\Backup\HR2E -Recurse |
   Select Name,LastWriteTime | sort-object -property LastWriteTime

$Body = $Body + "`n" + $Body1

$Date = Get-date -format "dd/MM/yy HH:mm:ss"

Send-MailMessage `
      -Body ($Body | Out-String) `
      -Subject "Backup Folder - $Date" `
      -From db01@abc.com  `
      -To it@abc.com `
      -SmtpServer exdag.abc.com
0
Comment
Question by:AXISHK
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40324886
I guess, you are converting the $Body to HTML before sending email. If so, try with..
$Body = @()

$Body += Get-ChildItem \\nas\Backup\HR2 -Recurse | 
    Select Name,LastWriteTime | sort-object -property LastWriteTime


 $Body += Get-ChildItem \\nas\Backup\HR2E -Recurse | 
    Select Name,LastWriteTime | sort-object -property LastWriteTime

Open in new window

0
 

Author Comment

by:AXISHK
ID: 40324896
What's the purpose of  $Body = @() ?

Do you mean the default format for $Body is a HTML text format ?
0
 

Author Comment

by:AXISHK
ID: 40324901
One more check, the heading appear only once instead showing under each file lists. How does it appear like this ?


Name                                                        LastWriteTime                                              
----                                                        -------------
0
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!

 
LVL 40

Expert Comment

by:Subsun
ID: 40324970
$Body = @() - Means setting $Body as a array object..

Do you mean the default format for $Body is a HTML text format ?
If you didn't use the ConvertTo-HTML as I mentioned in your last question, then the email will be sent as plain text and format may not look good.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40324974
To list the folder information separate, you can use following option..
$Body = @()

$Body += Get-ChildItem \\nas\Backup\HR2 -Recurse | 
    Select Name,LastWriteTime | sort-object -property LastWriteTime |
		ConvertTo-HTML -Property Name,FullName,LastWriteTime -head $a -body "<H3>HR2 Folder Information</H3>"


 $Body += Get-ChildItem \\nas\Backup\HR2E -Recurse | 
    Select Name,LastWriteTime | sort-object -property LastWriteTime |
		ConvertTo-HTML -Property Name,FullName,LastWriteTime -head $a -body "<H3>HR2E Folder Information</H3>"

Open in new window


Also replace -Body ($Body | Out-String) ` with -BodyAsHtml ($Body | Out-String) `
0
 

Author Comment

by:AXISHK
ID: 40325007
Can the heading be left justified ?
Display.png
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40325035
You can use the following head..
$a = "<style>"
$a = $a + "BODY{background-color:White;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{text-align:left;border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

Open in new window

0
 

Author Closing Comment

by:AXISHK
ID: 40327188
Tks
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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