No announcement yet.

Installation Script w/ Chkdsk

  • Filter
  • Time
  • Show
Clear All
new posts

  • Installation Script w/ Chkdsk

    Hi -

    I have used information in this forum to put together the below script which am deploying via Microsoft SMS using wscript command.

    The script seems to run fine, however, I am not sure that a chkdsk /f will actually take place if the first chkdsk session encounters errors (which is what I am hoping will occur). I am not too well versed on scripting but trying my best.

    Could someone go over the below code and tell me whether or not is written correctly? Again, the script seems to work and the first chkdsk as well as installation of the desired application takes place. I just want to make sure that IF errors are found during the first chkdsk pass, it will actually proceed with a reboot and a chkdsk /f on the boot drive, the way it is written.

    thanks much,

    ' Install script - 05/01/2008
    Const FIX_ERRORS = True
    Const RUN_AT_BOOT = True
    ' Get Local Computer Name
    	Set objNetwork = CreateObject("Wscript.Network")
    	strLocalComputer = objNetwork.ComputerName
    result = MsgBox("Welcome to the installaton script." & vbCrFL & vbCrFL & _
                    VBNewLine & "It is STRONGLY recommended that you proceed and do not Cancel this process." & vbCrFL & vbCrFL & _
    		VBNewLine & "Please save your work, close all running applications and click OK to begin the installation." & vbCrFL & vbCrFL & _	
    		VBNewLine & VBNewLine & "*** Your computer will automatically restart after Setup has been completed ***", _
                    vbInformation + vbOKCancel, _
                    "Software Deployment")
    If Result = vbOK Then
    strComputer = "."
    If NOT Left(strComputer,2) = "\\" Then
        strComputer = "\\" & strComputer
    End If
    strDrvLtr = "C:"
    If NOT Right(strDrvLtr,1) = ":" Then
        strDrvLtr = strDrvLtr & ":"
    End If
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!" & strComputer & "\root\cimv2")
    Set objDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='C:'")
    If errReturn = 0 Then
    ExecCmd "msiexec","/i \\server\app.msi /qn /L* C:\SGE.log CFGFILE=\\server\Install.cfg AUTOBACKUP=0 REBOOT=Force"
    end if
    Elseif errReturn = 1 Then
                intResults = MsgBox("Your hard drive contains errors and your machine must be restarted." & vbCrFL & vbCrFL & _
                    VBNewLine & "Click OK to restart now.  Click Cancel to restart later." & _
                    VBNewLine & VBNewLine & "It is recommended that you click OK " & _ 
                    "and restart your computer." & VBNewLine & "Upon clicking OK, " & _ 
                    "this session will end and any un-saved data will be lost." & VBNewLine & VBNewLine & _
                    "Note: This may take anything from 10-30+ minutes to complete.",49, "ChkDsk - Errors detected " _
                    & mid(strComputer,3, Len(strComputer) -2))
                If intResults = 1 Then
                    strShutdown = "shutdown /r /t 0 /f /m " & strComputer
                    set objShell = CreateObject("WScript.Shell")
                    objShell.Run strShutdown
                End If
       WScript.Echo "You have interrupted the installation - application will attempt to install again later " & errReturn 
    End If
    Sub ExecCmd(ProgName, Params)
    	Dim WshShell, oExec
    	Set WshShell = CreateObject("WScript.Shell")
    	Set oExec = WshShell.Exec(ProgName & " " & Params)
    	Do While oExec.Status = 0
    		WScript.Sleep 100
    	WScript.Echo ProgName & " done  Status = " & oExec.Status
    	Set oExec = Nothing
    	Set WshShell = Nothing
    End Sub

  • #2
    Re: Installation Script w/ Chkdsk

    Because although you named the boolean parameters, each parameter must still be used in the right order and placed on the right position - Use commas as position separator.

    If you want the ChkDsk method to schedule the job on next boot in case it determines the volume is in use (locked), you must use the RUN_AT_BOOT option too.

    And there are a few other errors in your script code

    about ChkDsk.

    The ChkDsk method (or CHKDSK.exe command-line tool) can be runned in change mode or, in read-only mode.

    - Change mode is when the FIX_ERRORS (or /f switch ) or the RECOVER_BAD_SECTORS (or /r switch ) option is used.
    - Read-only mode is when none of the above two options are used.

    It is not possible to run ChkDsk in change mode when the target volume is locked (e.g. the "system drive" when Windows is fully booted), in that case ChkDsk will be scheduled to run during the first comming boot.

    If I did understand your question correct, you want to start ChkDsk in read-only mode to check if there are problems, when problems found then automatically proceed in change mode? This is not possible!

    What could be possible is - execute the 'command-line tool' CHKDSK.exe in read-only mode first from within the script.
    After this is completed, the script checks what it reported;
    a) if no problems were found, then install the software you like to install with the script
    b) if problems were found, then run *again* ChkDsk, this time in change mode (with a chance that the system must restart)

    If the drive is NTFS, the first check (CHKDSK.exe read-only) can be runned with the /I and /C switches to speed up the process just a little bit.
    Do not use one of these options when the volume needs to be fixed/repaired.

    Here is an example:
    ' This sample vbscript was discussed here,
    strDrv = "C:"
    Set objNetwork = CreateObject("Wscript.Network")
    strComputer = objNetwork.ComputerName
    DIM WshShell ' WshShell has a Global scope
    Set WshShell = CreateObject("WScript.Shell")
    If NoProblemsFound(strDrv) = True Then
       InstallSoftware = True
       ' start ChkDsk again, this time to fix problems
       Select Case ChkDskFixProblems(strComputer, strDrv)
          Case 0 '("Success - chkdsk completed")
               InstallSoftware = True
          Case 1 '("Disk locked - chkdsk scheduled on reboot")
               intResults = MsgBox("The selected volume is locked, this " _
                    & "computer must re-boot in order to proceed chkdsk" _
                    & VBNewLine & "Click OK to restart now.  Click Cancel " _
                    & "to restart later." & VBNewLine & VBNewLine _
                    & "It is recommended that you click OK and restart your " _
                    & "computer." & VBNewLine & "Upon clicking OK, this " _
                    & "session will end and any un-saved data will be lost." _
                    & VBNewLine & VBNewLine & "Note: This may take anything " _
                    & "from 10-30+ minutes to complete.",49, "ChkDsk - Errors " _
                    & "detected " & strComputer)
               If intResults = 1 Then
                 WSH.Echo "Error - install again after the next boot"
               End If
          Case else  '("unSuccessfull") 
               WSH.Echo "Error - install again later"
      End Select
    End If
    If InstallSoftware = True Then
      wsh.echo "software setup commands here" ' * * *
    End If 
    Function NoProblemsFound(strDrv)
      ' "Running CHKDSK.exe in read-only mode"
      strTitle = "Performing a file system check on " & strDrv & " - please wait..."
      strCommand = "chkdsk.exe /I /C " & strDrv   'note for using the switches /I and /C  the volume must be a ntfs format.
      problemsfound = Empty
      ' (WshShell has a Global scope)
      Set objExecObject = WshShell.Exec _
         ("%comspec% /c title " & strTitle & chr(38) & strCommand)
      Do While Not objExecObject.StdOut.AtEndOfStream
        strText = objExecObject.StdOut.ReadLine()
        If Instr(strText, "/F (fix)") > 0 Then
          problemsfound = True
          Exit Do
          problemsfound = False
        End If
      If Not problemsfound = False Then
           NoProblemsFound = False
      Else NoProblemsFound = True
      End If
    End Function
    Function ChkDskFixProblems(strComputer, strDrv)
      DIM Chkdsk
      ' Order (!) of parameterers for the ChkDsk method;
      Const FIX_ERRORS = True               '(The default is false)
      Const VIGOROUS_INDEX_CHECK = False    '(The default is true)
      Const Skip_Folder_Cycle = False       '(The default is true)
      Const Force_Dismount = False          '(The default is false)
      Const RECOVER_BAD_SECTORS = True      '(The default is false)
      Const RUN_AT_BOOT = True              '(The default is false)
      Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      Set objDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='" & strDrv & "'")
      '( HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\BootExecute )
    End Function
    Sub ForcedReboot(strComputer)
       Const EWX_REBOOT   = 2
       Const EWX_FORCE    = 4
       iCmd = EWX_REBOOT + EWX_FORCE
       On Error Resume Next
       Set oWMI = GetObject("winmgmts:" _
             & "{impersonationLevel=impersonate,(Shutdown)}!" _
             & "\\" & strComputer & "\root\cimv2")
       Set colOperatingSystems = oWMI.ExecQuery _
             ("Select * from Win32_OperatingSystem")
         For Each oOS in colOperatingSystems
    End Sub

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts


    • #3
      Re: Installation Script w/ Chkdsk

      Thanks so much for your help! this is exactly what I am intending to do, that is, check the drive in read-only mode and then, if issues are found, inform the user about the machine having to restart in order to perform the changemode chkdsk.

      I gave you script a try and it works, however, since I am deploying this software via SMS (executing the vbs file using wscript) I was hoping to keep things behind the scenes in order to keep the user from interrupting the initial process as it occured.

      How would I go about incorporating your changes into my original posted script?, whereas, the user is notified about an installation about to occur (they click ok or cancel) If ok, read-only chkdsk takes place and user prompted to restart (if needed) and if the read-only chkdsk passes, then I can begin the install.