Do not use on any
shared computer
September 7, 2008 09:01pm pdt
 
[x]
Attachment Details

Event Log Entries in Defrag VB Script

Tags: VB Script
I found the script below that was written by someone else on EE.  The script works great.  The only thing that I would like to change is the event log entries.  If the defrag job is successful I would like to log an informational event.  If the defrag fails with any other result I would like to log a warning or an error in the event log.  I know nothing about scripting and would appreciate any help with this.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
' ------------------------------------------------------------
'
' Defragger.vbs - Script to analyze disk fragmentation
' and to run defrag.
'
' Parameters: 
'      1) Computer to be examined
'      2) Drive to defrag, empty string "" means all drives
'      3) ForceDefrag - True/False to Force Defrag even if it is not needed 
' Usage examples: 
'      cscript //nologo Defragger.vbs Server01 C False -> Analyse and defragment C -drive of Server01
'      cscript //nologo Defragger.vbs Server03 "" False -> Analyse and defragment All drives of Server03
'      cscript //nologo Defragger.vbs MyServer "" True -> Analyse and Force defragmentation All drives of MyServer
'
' Defragger.vbs - 17.04.2005 - Ver 0.1
' ------------------------------------------------------------
 
Dim arrArgs
Dim strComputer
Dim strDrive
Dim strForce
 
arrDefragResults = Array("Success", "Access denied", "Not supported", _
      "Volume dirty bit is set", "Not enough free space", "Corrupt Master File Table detected", _
      "Call cancelled", "Call cancellation request too late", "Defrag engine is already running", _
      "Unable to connect to defrag engine", "Defrag engine error", "Unknown error")
 
' ----- Main
      CheckArgs()
      RunAnalysis strComputer, strDrive, strForce
      
Function CheckArgs()
      Set arrArgs = Wscript.Arguments
      If arrArgs.Count <> 3 Then
            Usage()
      End If
      strComputer = arrArgs(0)
      strDrive = arrArgs(1)
      strForce = arrArgs(2)
End Function
 
Function Usage()
      Wscript.Echo "Defragger.vbs - Script to defrag W2003 disks" & vbNewline 
      Wscript.Echo "USAGE: cscript //nologo <computername> <driveletter> <forcedefrag>" & vbNewline
      Wscript.Echo vbTab & "<computername> - computer where to run defrag"
      Wscript.Echo vbTab & "<driveletter> - specify dirve to analyze and defrag," & vbNewline & _
            vbTab & vbTab & "enter empty string """" to analyse and defrag all drives." 
      Wscript.Echo vbTab & "<forcedefrag> - True/False fo force defrag (even if it is not needed)" & vbNewline
      Wscript.Echo "Example: cscript //nologo MyServer F False" 
      Wscript.Echo "Example: cscript //nologo OtherServer """" True" & vbNewline
      Wscript.Echo "Use Event Viewer to examine results in Application log"
      Wscript.Quit(0)
End Function 
 
Function RunAnalysis(strComputer, strDrive, strForce)      
      Set objWshShell = Wscript.CreateObject("WScript.Shell")
      
      Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      If strDrive <> "" Then
            Set colVols = objWMI.ExecQuery("Select * from Win32_Volume " & _
                  "where DriveType = 3 and Name = '" & strDrive & ":\\'")
      Else
            Set colVols = objWMI.ExecQuery("Select * from Win32_Volume where DriveType = 3")
      End If
      
      for each objVol in colVols
            'strMsg = "Script Parameters: """ & strComputer & """, """ & strDrive & _
            '      """, """ & Cbool(strForce) & """" & vbNewline & vbNewline 
            
            strMsg = "Analyzing Computer: " & strComputer & vbNewline & vbNewline 
            strMsg = strMsg & "Analyzing volume " & objVol.DriveLetter & vbNewline & vbNewline
            intRC = objVol.DefragAnalysis(boolDefrag, objRpt)
            if intRC = 0 then
                  strMsg = strMsg & "Volume " & objVol.DriveLetter & " fragmentation report:" & vbNewline & vbNewline
                  strMsg = strMsg & "Volume size: " & objRpt.VolumeSize & vbNewline
                  strMsg = strMsg & "Cluster size: " & objRpt.ClusterSize & vbNewline
                  strMsg = strMsg & "Used space: " & objRpt.UsedSpace & vbNewline
                  strMsg = strMsg & "Free space: " & objRpt.FreeSpace & vbNewline
                  strMsg = strMsg & "Percent free space: " & objRpt.FreeSpacePercent & vbNewline
                  strMsg = strMsg & "Total fragmentation: " & objRpt.TotalPercentFragmentation & vbNewline
                  strMsg = strMsg & "File fragmentation: " & objRpt.FilePercentFragmentation & vbNewline
                  strMsg = strMsg & "Free space fragmentation: " & objRpt.FreeSpacePercentFragmentation & vbNewline
                  strMsg = strMsg & "Total files: " & objRpt.TotalFiles & vbNewline
                  strMsg = strMsg & "Average file size: " & objRpt.AverageFileSize & vbNewline
                  strMsg = strMsg & "Total fragmented files: " & objRpt.TotalFragmentedFiles & vbNewline
                  strMsg = strMsg & "Total excess fragments: " & objRpt.TotalExcessFragments & vbNewline
                  strMsg = strMsg & "Avg fragments per file: " & objRpt.AverageFragmentsPerFile & vbNewline
                  strMsg = strMsg & "Page file size: " & objRpt.PageFileSize & vbNewline
                  strMsg = strMsg & "Total page file fragments: " & objRpt.TotalPageFileFragments & vbNewline
                  strMsg = strMsg & "Total folders: " & objRpt.TotalFolders & vbNewline
                  strMsg = strMsg & "Fragmented folders: " & objRpt.FragmentedFolders & vbNewline
                  strMsg = strMsg & "Excess folder fragments: " & objRpt.ExcessFolderFragments & vbNewline
                  strMsg = strMsg & "Total MFT size: " & objRpt.TotalMFTSize & vbNewline
                  strMsg = strMsg & "MFT record count: " & objRpt.MFTRecordCount & vbNewline
                  strMsg = strMsg & "MFT percent in use: " & objRpt.MFTPercentInUse & vbNewline
                  strMsg = strMsg & "Total MFT fragments: " & objRpt.TotalMFTFragments & vbNewline
                  
                  If boolDefrag Then
                        strMsg = strMsg & vbNewline & "You should defragment this volume."
                  else
                        strMsg = strMsg & vbNewline & "You do not need to defragment this volume."
                  end if
            Else
                  strMsg = strMsg & "Error during defragmentation analysis: " & arrDefragResults(intRC)
            end if
            objWshShell.Run "C:\WINDOWS\System32\eventcreate.exe " & _
                  "/T Information /L Application /id 1 /SO ""Defragger Script"" " & _
                  " /D """ & strMsg & """"
 
            If boolDefrag Or Cbool(strForce) Then
                  ' ----- Running Defrag on given volume
                  strTmp = "Defragmenting of " & strComputer & " volume " & objVol.DriveLetter & " started at " & Now()
                  objWshShell.Run "C:\WINDOWS\System32\eventcreate.exe " & _
                        "/T Information /L Application /id 1 /SO ""Defragger Script"" " & _
                        " /D """ & strTmp & """"
                  ' ----- Change False -> True to force defrag
                  nResult = objVol.Defrag(Cbool(strForce), objDefRpt)
                  
                  ' ----- Create report of defragmentation
                  strDRptTmp = "Volume " & objVol.DriveLetter & " fragmentation report:" & vbNewline & vbNewline
                  strDRptTmp = strDRptTmp & "Volume size: " & objDefRpt.VolumeSize & vbNewline
                  strDRptTmp = strDRptTmp & "Cluster size: " & objDefRpt.ClusterSize & vbNewline
                  strDRptTmp = strDRptTmp & "Used space: " & objDefRpt.UsedSpace & vbNewline
                  strDRptTmp = strDRptTmp & "Free space: " & objDefRpt.FreeSpace & vbNewline
                  strDRptTmp = strDRptTmp & "Percent free space: " & objDefRpt.FreeSpacePercent & vbNewline
                  strDRptTmp = strDRptTmp & "Total fragmentation: " & objDefRpt.TotalPercentFragmentation & vbNewline
                  strDRptTmp = strDRptTmp & "File fragmentation: " & objDefRpt.FilePercentFragmentation & vbNewline
                  strDRptTmp = strDRptTmp & "Free space fragmentation: " & objDefRpt.FreeSpacePercentFragmentation & vbNewline
                  strDRptTmp = strDRptTmp & "Total files: " & objDefRpt.TotalFiles & vbNewline
                  strDRptTmp = strDRptTmp & "Average file size: " & objDefRpt.AverageFileSize & vbNewline
                  strDRptTmp = strDRptTmp & "Total fragmented files: " & objDefRpt.TotalFragmentedFiles & vbNewline
                  strDRptTmp = strDRptTmp & "Total excess fragments: " & objDefRpt.TotalExcessFragments & vbNewline
                  strDRptTmp = strDRptTmp & "Avg fragments per file: " & objDefRpt.AverageFragmentsPerFile & vbNewline
                  strDRptTmp = strDRptTmp & "Page file size: " & objDefRpt.PageFileSize & vbNewline
                  strDRptTmp = strDRptTmp & "Total page file fragments: " & objDefRpt.TotalPageFileFragments & vbNewline
                  strDRptTmp = strDRptTmp & "Total folders: " & objDefRpt.TotalFolders & vbNewline
                  strDRptTmp = strDRptTmp & "Fragmented folders: " & objDefRpt.FragmentedFolders & vbNewline
                  strDRptTmp = strDRptTmp & "Excess folder fragments: " & objDefRpt.ExcessFolderFragments & vbNewline
                  strDRptTmp = strDRptTmp & "Total MFT size: " & objDefRpt.TotalMFTSize & vbNewline
                  strDRptTmp = strDRptTmp & "MFT record count: " & objDefRpt.MFTRecordCount & vbNewline
                  strDRptTmp = strDRptTmp & "MFT percent in use: " & objDefRpt.MFTPercentInUse & vbNewline
                  strDRptTmp = strDRptTmp & "Total MFT fragments: " & objDefRpt.TotalMFTFragments 
                  
                  strTmp = "Defragmenting of " & strComputer & " volume " & objVol.DriveLetter & _
                        " ended at " & Now() & vbNewline & vbNewline & _
                        "Result of defrag was " & arrDefragResults(nResult) & vbNewline & vbNewline
                  strTmp = strTmp & strDRptTmp       
                        
                  objWshShell.Run "C:\WINDOWS\System32\eventcreate.exe " & _
                        "/T Information /L Application /id 1 /SO ""Defragger Script"" " & _
                        " /D """ & strTmp & """"
            End If
      Next
End Function
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: kharoldsen
Solution Provided By: roytal
Participating Experts: 1
Solution Grade: B
Views: 62
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by roytal
Expert Comment by roytal:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by kharoldsen
Author Comment by kharoldsen:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by roytal
Accepted Solution by roytal:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628