Solved

PowerShell

Posted on 2014-09-16
8
145 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
Comment Utility
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
Comment Utility
What's the purpose of  $Body = @() ?

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

Author Comment

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


Name                                                        LastWriteTime                                              
----                                                        -------------
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
$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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
Comment Utility
Can the heading be left justified ?
Display.png
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
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
Comment Utility
Tks
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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This article will help you understand what HashTables are and how to use them in PowerShell.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

6 Experts available now in Live!

Get 1:1 Help Now