Solved

PowerShell to dump a particular folders

Posted on 2014-09-15
5
78 Views
Last Modified: 2014-09-16
How to list all the folders and files with last modified date and send out an email using powershell ?

Tks
0
Comment
Question by:AXISHK
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40322643
Try this code, this will report the details in an HTML mail.. You can also attach the details in a csv file if required.
$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{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>"

$Body = Get-ChildItem D:\Folder -Recurse | 
   Select Name,FullName,LastWriteTime |
	 ConvertTo-HTML -head $a -body "<H3>Folder Information</H3>"

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

Send-MailMessage `
	-Body ($Body | Out-String) `
	-Subject "Folder Information - $Date" `
	-From subsun@domain.com `
	-To subsun@domain.com `
	-SmtpServer smtp.domain.com

Open in new window

0
 

Author Comment

by:AXISHK
ID: 40322720
Tks. How to modify the code to include CSV as a attachment ?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40323275
Do you need both HTML & CSV reports? if yes try following code..
$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{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>"
$Csv = "$($Env:TEMP)\Report.csv"

#collect data
$folder = Get-ChildItem D:\Folder -Recurse | Select Name,FullName,LastWriteTime
#Export to csv
$folder | Select Name,FullName,LastWriteTime | Export-Csv $Csv -nti
#Prepare HTML body
$Body = $folder | ConvertTo-HTML -Property Name,FullName,LastWriteTime -head $a -body "<H3>Folder Information</H3>"

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

Send-MailMessage `
	-Body ($Body | Out-String) `
	-Subject "Folder Information - $Date" `
	-From subsun@domain.com `
	-To subsun@domain.com `
	-SmtpServer smtp.domain.com `
	-Attachments $Csv

Open in new window

0
 

Author Comment

by:AXISHK
ID: 40324843
Send-MailMessage `
      -Body ($Body | Out-String) `
      -Subject "Folder Information - $Date" `
      -From subsun@domain.com `
      -To subsun@domain.com `
      -SmtpServer smtp.domain.com `
      -Attachments $Csv

What's the meaning for '  at the end of each statement ?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40324857
Those are parameters of Send-MailMessage command.. You can read help here..

http://ss64.com/ps/send-mailmessage.html

Let me know if you need further details on any particular parameter..

' means the command continues in next line..
http://www.computerperformance.co.uk/powershell/powershell_backtick.htm
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

I had a question today where the user wanted to know how to delete an SSL Certificate, so I thought that I would quickly add this How to! Article for your reference. WHY WOULD YOU WANT TO DELETE A CERTIFICATE? 1. If an incorrect certificate was …
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

919 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

18 Experts available now in Live!

Get 1:1 Help Now