Announcement

Collapse
No announcement yet.

Remotely run Check Disk via VBS

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Remotely run Check Disk via VBS

    I often have to connect to a users machine and run a check disk as they don't have rights...

    * runas iexplore.exe as me and browse to c:, or 'explorer /separate' as myself,
    * right click local disc c:
    * properties
    * Tools
    * check Now...
    * tick automatically fix file system errors and scan for and attempt recovery of bad sectors

    then I get the "do you want to schedule this for next restart of the computer" message, say yes and then close all the windows and let the user finish off what they are doing and reboot later.


    is it possible to create a script that can sit on my desktop, I open it, it prompts for a computer name for me to type in and connect up to that machine and do all that without the hasstle of connecting up and doing the above?
    This is my signature.

  • #2
    Re: Remotely run Check Disk via VBS

    Hi,

    I am hopeless with scripting, but why do many of your machines need chkdsk?

    Comment


    • #3
      Re: Remotely run Check Disk via VBS

      a quick search on google led me to this. I am not someone that is big into scripting, I usually find bits and piece's of code on the internet and do my best with it. Hope this helps some

      Run chkdsk via WMI

      Chkdsk return codes:

      0 - No errors were found.
      1 - Errors were found and fixed.
      2 - Disk cleanup, such as garbage collection, was performed, or cleanup was not performed because /f was not specified.
      3 - Could not check the disk, errors could not be fixed, or errors were not fixed because /f was not specified.

      Code:
      Const FIX_ERRORS = False
      
      strComputer = "Server01"
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!" & strComputer & " ootcimv2")
      
      Set objDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='C:'")
      
      errReturn = objDisk.ChkDsk(FIX_ERRORS)
      
      if errReturn = 0 then
         WScript.Echo "Chkdsk completed successfully."
      
      elseif errReturn = 1 then
         WScript.Echo "Chkdsk scheduled on next reboot."
      else
         WScript.Echo "Error running chkdsk: " & errReturn 
      end if
      Justin
      A+ Net+ MCP

      Comment


      • #4
        Re: Remotely run Check Disk via VBS

        Originally posted by simonsays View Post
        Hi,

        I am hopeless with scripting, but why do many of your machines need chkdsk?
        They are old, with fairly naff hard disks in them - users aren't looking after them and complain they are running slowly - have to do the best we can - chkdsk is actually fixing some issues but users don't have the rights to run it... could do with also running a defrag as well to be honest... but I thought i'd just start with the chkdsk

        Thanks justinmirsky - will give it a go and be back
        This is my signature.

        Comment


        • #5
          Re: Remotely run Check Disk via VBS

          Well I'm new on the scripting scene and in the process of reading the Windows Scripting Guide so I thought I'd take a shot at this script.

          I did notice a few typos and hopefully I corrected them all.
          Code:
          Const FIX_ERRORS = True
          Const RUN_AT_BOOT = True
          
          
          strComputer = InputBox("Enter the name of the computer you want to check" & _
                vbCrLF & "e.g." & Chr(34) & "\\workstation1" & Chr(34))
          
          If NOT Left(strComputer,2) = "\\" Then
              strComputer = "\\" & strComputer
          End If
          
          strDrvLtr = InputBox("Enter the drive letter you want to check" & _
                vbCrLF & "e.g." & Chr(34) & "C:" & Chr(34))
          
          If NOT Right(strDrvLtr,1) = ":" Then
              strDrvLtr = strDrvLtr & ":"
          
          Set objWMIService = GetObject("winmgmts:" _
              & "{impersonationLevel=impersonate}!" & strComputer & "\root\cimv2")
          
          Set objDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='" & strDrvLtr & "'")
          
          errReturn = objDisk.ChkDsk(FIX_ERRORS,,,,RUN_AT_BOOT)
          
          If errReturn = 0 Then
             WScript.Echo "Chkdsk completed successfully."
          
          Elseif errReturn = 1 Then
          
               intResults = MsgBox("Chkdsk scheduled on next reboot." & vbCrFL & vbCrFL & _
                  "Click OK to reboot now. Click Cancel to manually reboot it.",49, "Reboot " _
                  & 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
          
          Else
             WScript.Echo "Error running chkdsk: " & errReturn 
          End If
          Probably the most notable change is the prompt for a computer name. I made some other changes as well, like remote restart, some syntax checking, etc.

          I'd love to get some thoughts from some of the avid scripters here.
          Regards,
          Jeremy

          Network Consultant/Engineer
          Baltimore - Washington area and beyond
          www.gma-cpa.com

          Comment


          • #6
            Re: Remotely run Check Disk via VBS

            I started by modifying the script adding prompt for the computer name... just as you have...

            Code:
            strComputer = InputBox("Enter Computer Name:", "Disk Checker")
            into it...

            Unfortunately I'm going to be too busy this weekend to give this a good going through...
            This is my signature.

            Comment


            • #7
              Re: Remotely run Check Disk via VBS

              argh getting an error on line: 41
              Char: 7
              Error: Expected 'End'
              Code: 800A03F6
              Source: Microsoft VBScript Compilation error
              This is my signature.

              Comment


              • #8
                Re: Remotely run Check Disk via VBS

                Sorry about that. Left out an "End If"

                Code:
                Const FIX_ERRORS = True
                Const RUN_AT_BOOT = True
                
                
                strComputer = InputBox("Enter the name of the computer you want to check" & _
                      vbCrLF & "e.g." & Chr(34) & "\\workstation1" & Chr(34))
                
                If NOT Left(strComputer,2) = "\\" Then
                    strComputer = "\\" & strComputer
                End If
                
                strDrvLtr = InputBox("Enter the drive letter you want to check" & _
                      vbCrLF & "e.g." & Chr(34) & "C:" & Chr(34))
                
                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='" & strDrvLtr & "'")
                
                errReturn = objDisk.ChkDsk(FIX_ERRORS,,,,,RUN_AT_BOOT)
                
                If errReturn = 0 Then
                   WScript.Echo "Chkdsk completed successfully."
                
                Elseif errReturn = 1 Then
                
                     intResults = MsgBox("Chkdsk scheduled on next reboot." & vbCrFL & vbCrFL & _
                        "Click OK to reboot now. Click Cancel to manually reboot it.",49, "Reboot " _
                        & 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
                
                Else
                   WScript.Echo "Error running chkdsk: " & errReturn 
                End If
                Last edited by JeremyW; 25th June 2007, 18:52. Reason: Added one more comma to the ChkDsk method parameters
                Regards,
                Jeremy

                Network Consultant/Engineer
                Baltimore - Washington area and beyond
                www.gma-cpa.com

                Comment


                • #9
                  Re: Remotely run Check Disk via VBS

                  Looks very promising - I just need to find a spare machine to test it on now

                  that pesky end if - i was looking for one towards the lower end...
                  This is my signature.

                  Comment


                  • #10
                    Re: Remotely run Check Disk via VBS

                    Yeah, the script was looking at the end too because it saw those "If" statements as nested in the one I failed to close.

                    Let me know if you have any issues because I'm still learning this stuff and it would be helpful for me to troubleshoot them.
                    Regards,
                    Jeremy

                    Network Consultant/Engineer
                    Baltimore - Washington area and beyond
                    www.gma-cpa.com

                    Comment


                    • #11
                      Re: Remotely run Check Disk via VBS

                      hm my issue at the moment is it doesn't seem to be doing the chkdsk upon reboot...

                      it may be because i am canceling to do a manual reboot? trying doing the ok thang... script has no errors which seems to look promising - will keep you informed
                      This is my signature.

                      Comment


                      • #12
                        Re: Remotely run Check Disk via VBS

                        I left out a comma.
                        OK, I was able to test it and it works now.
                        I edited the script above so that it's correct.
                        Regards,
                        Jeremy

                        Network Consultant/Engineer
                        Baltimore - Washington area and beyond
                        www.gma-cpa.com

                        Comment


                        • #13
                          Re: Remotely run Check Disk via VBS

                          I wouldn't have spotted that... ;p

                          Don't suppose you can explain a little what the commas are doing?
                          This is my signature.

                          Comment


                          • #14
                            Re: Remotely run Check Disk via VBS

                            Oh yeah, I guess I should.

                            The objDisk is a reference to the WMI class Win32_LogicalDisk and the particular instance of what ever the drive letter you punch in using the DeviceID.

                            ChkDsk is a method of the Win32_LogicalDisk class. It has six parameters you can specify. If you don't specify any it will use the default.

                            Parameters can be different data types(integer, string, boolean, etc.) but in this case they are all boolean i.e. True (aka "-1" don't ask me why but that's what it is in vbscript) or False ("0")

                            So in the below line I specified the first and the sixth parameter.
                            objDisk.ChkDsk(FIX_ERRORS,,,,,RUN_AT_BOOT)

                            Here's the link to the ChkDsk method http://msdn2.microsoft.com/EN-US/library/aa384913.aspx

                            EDIT - If you have no idea what I'm talking about when I refer to WMI classes then check out this thread http://forums.petri.com/showthread.php?t=300. Sorin's post in particular has a great link to a webcast on WMI.

                            PS - you'll notice in a lot of scripts people using variables with prefixes like str, int, obj, col. They do that to make it easy to tell what kind of data you're dealing with.
                            Last edited by JeremyW; 26th June 2007, 13:56.
                            Regards,
                            Jeremy

                            Network Consultant/Engineer
                            Baltimore - Washington area and beyond
                            www.gma-cpa.com

                            Comment


                            • #15
                              Re: Remotely run Check Disk via VBS

                              ah yes... I see!
                              This is my signature.

                              Comment

                              Working...
                              X