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
190 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

What Is Threat Intelligence?

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

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
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…

743 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

13 Experts available now in Live!

Get 1:1 Help Now