?
Solved

PowerShell to dump a particular folders

Posted on 2014-09-15
5
Medium Priority
?
85 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
[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
  • 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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

OfficeMate Freezes on login or does not load after login credentials are input.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Suggested Courses

741 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