Solved

i need script to email me the defragmentation analysis report for all servers (for all volume ) from input file,

Posted on 2014-09-18
5
197 Views
Last Modified: 2015-02-04
I need a script to email defractmentation analysis report for all servers and there voulumes, script will take input from input file.
0
Comment
Question by:biplabmukh
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
ID: 40360991
Hi

I think this will do what you wanted


save the following code into a script , It can be used in the following ways ( localhost is the default  server )

.\script  server1,server2,server3

or

get-content Servers.txt | ,\ script

or

.\script   for the local machine.


You will need to update the email settings to match  you environment.

#fragmentation report
param($servers = @("localhost"))
$report=@()
foreach ($server in $servers)
{
#
#  find the disks
#
$disks = Get-WmiObject -class Win32_LogicalDisk -ComputerName $server -Filter "DriveType=3"
	#
	#  check fragmentation of each disk
	#
	foreach ( $disk in $disks) 
	{
	$drive = Get-WmiObject -Class Win32_Volume      -ComputerName $server -Filter "DriveLetter = '$($disk.deviceId)'"
	$report += ($drive.DefragAnalysis()).DefragAnalysis | select pscomputername,@{n="Drive";e={$drive.name}},AverageFragmentsPerFile,AverageFileSize,FreeSpacePercent,TotalFragmentedFiles,ExcessFolderFragments,FragmentedFolders
	}
}
#
### send email
#
 $smtp = "Exchange-Server" 
 $to = "Test User <test.user@gmail.com>" 
 $from = "Fragmentation Report <test.user@Yourdomain.com>" 
 $subject = "Server Fragmentation Report"  
 
$content =   $report |   ConvertTo-Html -Fragment -PreContent "<h2>Fragmentation Report $(Get-date):</h2>" | Out-String
 
$head = @'
<style>
body { background-color:#dddddd;
       font-family:Tahoma;
       font-size:12pt; }
td, th { border:1px solid black; 
         border-collapse:collapse; }
th { color:white;
     background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
table { margin-left:50px; }
</style>
'@
 
$Body = ConvertTo-HTML -head $head  -Body $content | Out-String

 
#### Now send the email using \> Send-MailMessage  
 
send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml

Open in new window

0
 
LVL 10

Expert Comment

by:JoeKlimis
ID: 40413053
how did you get on ?
0
 

Author Comment

by:biplabmukh
ID: 40426162
Thanks J, will check and let you know if it works or any issue.
0

Featured Post

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
"Video DataSource is Offline or not responding"  - why? 1 35
add image on html mail 6 19
Power shell Script - Help v2 7 22
VBScript on Html 11 20
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

831 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