Solved

PowerShell

Posted on 2014-09-16
8
148 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
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 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

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

14 Experts available now in Live!

Get 1:1 Help Now