'Defrag script ' ## Zum Defragmentieren per Scheduler geeignet, läuft hierdurch ## ' ## problemlos mit administrativen Berechtigungen ## 'Author: Cory Dunkle
'Scannt nach lokalen, festeingebauten disks und defragmentiert diese Option Explicit
'Variables Dim WshShell, WshNetwork, fso, d, dc, ErrStr(), Return, X, A(), MyFile, I, MyBox, Drive Dim CompName 'Computer name 'Dim statement for Event Logging Dim objELShell 'Event writer Set objELShell = WScript.CreateObject("Wscript.Shell") Set WshShell = WScript.CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") X = 0
'Detect drives and add to array X if fixed... counts fixed drives Set dc = fso.Drives For Each d In dc If d.DriveType = 2 Then X = X + 1 'Determine drive letter of first fixed disk 'This is the drive that the error report will be placed on If X = 1 Then Drive = d End If End If Next 'Resize arrays to number of fixed disks ReDim A(X) ReDim ErrStr(X) X = 0 For Each d In dc If d.DriveType = 2 Then X = X + 1 Return = WshShell.Run("defrag " & d & " -f", 0, True) 'Determine the Error code returned by Defrag for the current drive and save it If Return = 0 Then ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag completed successfully" & VbCrLf ElseIf Return = 1 Then ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " _ & Return & " _(defrag was cancelled manually) " & VbCrLf ElseIf Return = 2 Then ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " _ & Return & "(there was A command line Error. Check your command line For valid" _ &"switches And drives)" & VbCrLf ElseIf Return = 3 Then ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " _ & Return & " (there was an unknown Error)" & VbCrLf ElseIf Return = 4 Then ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " _ & Return & " (defrag could Not run due To insufficient memory resources)" & VbCrLf 'errorlevel 5 is not currently used ElseIf Return = 5 Then ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " _ & Return & " (general Error)" & VbCrLf ElseIf Return = 6 Then ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & Return & _ "(System error: either the account used to run defrag is not an administrator, " _ & "there Is A problem loading the resource DLL, Or A defrag engine could Not be accessed. " _ & "Check For proper user permissions And run Sfc.exe To validate system files)" & VbCrLf ElseIf Return = 7 Then ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & Return & _ "(There Is Not enough free Space On the Drive. Defrag needs 15% free Space To run " _ & "On A volume)" & VbCrLf Else ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with an unknown error level: " _ & Return & VbCrLf End If
End If Next 'Create report in Event Log For I = 1 To X If Return = 0 Then objELShell.LogEvent 0, ErrStr(I) Else If Return <> 0 Then objELShell.LogEvent 2, ErrStr(I) End If End If Next
Set WshShell = Nothing Set fso = Nothing
|