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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…

821 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