Defrag all disks at the same time

Reading Time: 2 minutes

The below script by Doug Knox is useful. It will defrag all disks at the same time. Can also be used as part of a scheduled task. It will also provide you with a report.

Copy the text into a .vbs file.

 

‘defrag_all2.vbs
‘Defrags all hard disks – Can be run as a Scheduled Task
‘Modified to create an error log and display it
‘© Doug Knox – 4/13/2002

Option Explicit

Dim WshShell, fso, d, dc, ErrStr(), Return, X, A(), MyFile, I, MyBox, Drive

Set WshShell = WScript.CreateObject(“WScript.Shell”)
Set fso = CreateObject(“Scripting.FileSystemObject”)
X = 0

   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

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”, 1, 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 the Error Report in the root of the first fixed disk.
Set MyFile = fso.OpenTextFile(Drive & “\defragreport.txt”, 2, True)
MyFile.WriteLine(Date) & vbCRLF
MyFile.WriteLine(Time) & vbCRLF
   For I = 1 to X
      MyFile.WriteLine(ErrStr(I))
   Next
   MyFile.Close

Return = WshShell.Run(Drive & “\defragreport.txt”,3,True)

Set WshShell = Nothing
Set fso = Nothing

List of Microsoft DOS color codes

Reading Time: < 1 minute

Ever wanted to create a DOS script but with a font colour of your choice. Well, you can add the color command to your scripts. For example Color 0C will run the script in the colour red. Color 05 will run the script in Purple. The color command is available to client operating systems.

0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White

Install a Windows Update via script

Reading Time: 3 minutes

To install software via group policy you must convert the .exe to a .msi but there is a way to get a script to install the .exe.

Copy the below script a .bat file and add the path. You will also need to download the update and store within the share you create. The script will also check WSUS for any missing updates by forcing a download of updates first.

As always, I would recommend you test.

@echo off
color 0A
ECHO. **************************************************************************************
ECHO.
ECHO.                                www.cloudbuild.co.uk/
ECHO. **************************************************************************************

echo Enabling BITs …
sc config bits start= auto
echo Starting BITs …
net start “Background Intelligent Transfer Service”
echo Enabling Automatic Updates …
sc config Wuauserv start= auto
echo Starting Automatic Updates …
net start “Windows Automatic Update Service”
echo Checking MS WSUS for any missing updates …
wuauclt.exe /detectnow
echo Enabling Windows Security Center Service (wscsvc) …
sc config wscsvc start= auto
echo Starting Windows Security Center …
net start wscsvc
echo Enabling Windows Error Reporting Service (ERSvc) …
sc config ERSvc start= auto
echo Starting Windows Error Reporting …
net start ERSvc
echo Patching MS08-067 …
“PATH HERE” /quiet /norestart
goto exit

 

Or if you wish to roll out to different operating systems, use the below script:

@echo off
color 0A
ECHO. ****************************************************************************************
ECHO.               www.sana-tech.net/blog
ECHO.
ECHO. ****************************************************************************************

ver | find “2003” > nul
if %ERRORLEVEL% == 0 goto ver_2003

ver | find “XP” > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find “2000” > nul
if %ERRORLEVEL% == 0 goto ver_2000

ver | find “Version 6.0.6000” > nul
if %ERRORLEVEL% == 0 goto ver_vista-sp0

ver | find “Version 6.0.6001” > nul
if %ERRORLEVEL% == 0 goto ver_vista-sp1

goto exit

:ver_xp
echo Enabling BITs …
sc config bits start= auto
echo Starting BITs …
net start “Background Intelligent Transfer Service”
echo Enabling Automatic Updates …
sc config Wuauserv start= auto
echo Starting Automatic Updates …
net start “Windows Automatic Update Service”
echo Checking MS WSUS for any missing updates …
wuauclt.exe /detectnow
echo Enabling Windows Security Center Service (wscsvc) …
sc config wscsvc start= auto
echo Starting Windows Security Center …
net start wscsvc
echo Enabling Windows Error Reporting Service (ERSvc) …
sc config ERSvc start= auto
echo Starting Windows Error Reporting …
net start ERSvc
echo Patching …
“path where update is stored” /quiet /norestart
goto exit

:ver_2000
echo Enabling BITs …
sc config bits start= auto
echo Starting BITs …
net start “Background Intelligent Transfer Service”
echo Enabling Automatic Updates …
sc config Wuauserv start= auto
echo Starting Automatic Updates …
net start “Windows Automatic Update Service”
echo Checking MS WSUS for any missing updates …
wuauclt.exe /detectnow
echo Patching MS08-067 …
“path to where update is stored” /quiet /norestart
goto exit

:ver_vista-sp0
echo Enabling BITs …
sc config bits start= auto
echo Starting BITs …
net start “Background Intelligent Transfer Service”
echo Enabling Automatic Updates …
sc config Wuauserv start= auto
echo Starting Automatic Updates …
net start “wuauserv”
echo Checking MS WSUS for any missing updates …
wuauclt.exe /detectnow
echo Enabling Windows Security Center Service (wscsvc) …
sc config wscsvc start= auto
echo Starting Windows Security Center …
net start wscsvc
echo Enabling Windows Defender Service (WinDefend) …
sc config WinDefend start= auto
echo Starting Windows Defender …
net start WinDefend
echo Enabling Windows Error Reporting Service (WerSvc) …
sc config WerSvc start= auto
echo Starting Windows Error Reporting …
net start WerSvc
echo Patching …
“path to where update is stored” /quiet /norestart
goto exit

:ver_vista-sp1
echo Enabling BITs …
sc config bits start= auto
echo Starting BITs …
net start “Background Intelligent Transfer Service”
echo Enabling Automatic Updates …
sc config Wuauserv start= auto
echo Starting Automatic Updates …
net start “Windows Automatic Update Service”
echo Checking MS WSUS for any missing updates …
wuauclt.exe /detectnow
echo Enabling Windows Security Center Service (wscsvc) …
sc config wscsvc start= auto
echo Starting Windows Security Center …
net start wscsvc
echo Enabling Windows Defender Service (WinDefend) …
sc config WinDefend start= auto
echo Starting Windows Defender …
net start WinDefend
echo Enabling Windows Error Reporting Service (WerSvc) …
sc config WerSvc start= auto
echo Starting Windows Error Reporting …
net start WerSvc
“path to where update is stored” /quiet /norestart
goto exit

:exit

How to check if a particular patch is installed

Reading Time: < 1 minute

To check if a particular update is installed on your machine, you could browse to the control panel or check the Windows directory.

If you wish to carry out this task via script, the script can be found below. Just replace the patch in the highlighted area.

Set objSession = CreateObject(“Microsoft.Update.Session”)
Set objSearcher = objSession.CreateUpdateSearcher
Set objResults = objSearcher.Search(“Type=’Software'”)
Set colUpdates = objResults.Updates

For i = 0 to colUpdates.Count – 1
    If colUpdates.Item(i).Title = _
        “Security Update for Windows XP (KB89000)” Then
        If colUpdates.Item(i).IsInstalled <> 0 Then
            Wscript.Echo “This update is installed.”
            Wscript.Quit
        Else
            Wscript.Echo “This update is not installed.”
            Wscript.Quit
        End If
    End If
Next

Wscript.Echo “This update is not installed.”