Solved

PowerShell to dump a particular folders

Posted on 2014-09-15
5
75 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

OfficeMate Freezes on login or does not load after login credentials are input.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

708 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

17 Experts available now in Live!

Get 1:1 Help Now