Solved

PowerShell

Posted on 2014-09-16
8
153 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
[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
  • 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
Problems using Powershell and Active Directory?

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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