Announcement

Collapse
No announcement yet.

Mapping network drive for limited time

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

  • Mapping network drive for limited time

    Hi, all!!
    I'm trying automating some task in our environment... We have Windows Server 2003 with special McAfee AV version for NetApp. We need scan Network Share using this AV at least 3 times in week. I've created scheduled On-Demand scan task in McAfee antivirus to run during night hours and scan particular network drive. Also I've created two scheduled tasks on the same Windows 2003 Server, - one running vbs script which map network drive, and another one for disconnect this network drive.
    So here is scenario:
    1. At 20:00 running first scheduled task on Server which map network drive
    2. At 20:15 starting McAfee scheduled network drive scan
    3. At 06:00 AM running second scheduled task on Server which disconnect network drive.

    My question, if it possible to create only one script which will map network drive and after given time (8 hours for example) disconnect this network drive automatically? If it possible it will require less administrative effort so I can run McAfee each night and not only 3 times at week...

    Here are scripts codes:

    Mapping network drive:

    Code:
     
    ' ----------------------------------------------------' 
    Option Explicit
    Dim objNetwork 
    Dim strDriveLetter, strRemotePath, strDriveAlias, oShell
    strDriveLetter = "Z:" 
    strRemotePath = \\Server\Share
    strDriveAlias = "My Share" 
    Set objNetwork = CreateObject("WScript.Network")
    Set oShell = CreateObject("Shell.Application") 
    objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
    If Err.Number = 0 Then
    oShell.NameSpace(strDriveLetter).Self.Name = strDriveAlias
    End IF
    ' ----------------------------------------------------'
    Disconnect Network Drive:

    Code:
     
    ' ----------------------------------------------------' 
    Option Explicit
    Dim objNetwork, strDriveLetter
    strDriveLetter = "Z:" 
    Set objNetwork = CreateObject("WScript.Network") 
    ' Section which removes strDriveLetter
    objNetwork.RemoveNetworkDrive strDriveLetter
    Wscript.Quit 
    ' ---------------------------------------------------'

  • #2
    Re: Mapping network drive for limited time

    Yes it can be done with one script, simply by making it Sleep for 8 hours.

    The script could also monitor the process, if the process ends the scrip continue and disconnect the drive. Try to find out what process is running during the scan.

    btw do you have to use a mapped drive for the scanner? The McAfee AV product I know does support network paths, we use VSE.


    One step further,
    I think that, depending on the McAfee AV product you have of course, it would also be possible to just create the On-Demand Scan task. Do not 'Schedule' the job in the McAfee console but use the script to start the defined task instead.

    Start the task by scheduled script
    If a same on-Demand Scan task is configured on and started from multiple servers, then make sure to give the job on each server the exact same name, so the script is able find the ODS task by name. It searches each subkey in the registry under the key "HKEY_LOCAL_MACHINE\SOFTWARE\McAfee\DesktopProtect ion\Tasks" for the value of the item "szTaskName". If it matches the name that you gave the On-demand Scan task then the script will use the name of that Subkey (is a GUID like string) in the command line for starting the job.
    e.g. "C:\Program Files (x86)\McAfee\VirusScan Enterprise\x64\Scan64.Exe" -task {6C2918E2-2244-4FD3-A8BD-093DC85A58ED}
    (if it is a 32 bit version of windows the path and executable should be adjusted)

    If the task is on one server only, you could also hardcode the GUID of the task in the script. If there are to many subkey to manually search them for the GUID you can also use the tool "Process Explorer". Run the task and "Process Explorer" can show you the complete command line you can copy and paste in the script.


    /Rems

    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

    Comment


    • #3
      Re: Mapping network drive for limited time

      Yes it can be done with one script, simply by making it Sleep for 8 hours.

      The script could also monitor the process, if the process ends the scrip continue and disconnect the drive. Try to find out what process is running during the scan.

      btw do you have to use a mapped drive for the scanner? The McAfee AV product I know does support network paths, we use VSE.
      Rems,
      Thank you for respond!
      Yes, I know that McAfee support network patches, but still I prefer to map shared folder and create On-Demand scan task. In this way with McAfee GUI I have more flexibility - I can choose what to scan and what to exclude, can schedule it etc...
      You gave me a great idea - not schedule On-Demand scan and use the command which run On-Demand task within a script. Actually it sounds very good for me if it possible to create script which monitors the process, if the process ends the scrip continues and disconnects the drive. I followed your advice and found everything I need… I just need your help with script code...

      Try to find out what process is running during the scan.
      The process name is scan32.exe

      GUID of the task:

      Code:
      "C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}

      szTaskName: Mapped Network Drive Scan

      So far this task need to be run only on one server so GUID could be hardcoded in the script.
      Thank you in advance,
      Igor.

      Comment


      • #4
        Re: Mapping network drive for limited time

        OK, I played around and here is what I have so far:

        Code:
        Option Explicit
        Dim objNetwork 
        Dim strDriveLetter, strRemotePath, strDriveAlias, oShell, objShell, strProgramPath
        strDriveLetter = "Z:" 
        strRemotePath = "\\Server\Share"
        strDriveAlias = "My Share" 
        Set objNetwork = CreateObject("WScript.Network")
        Set oShell = CreateObject("Shell.Application") 
        objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
        If Err.Number = 0 Then
        oShell.NameSpace(strDriveLetter).Self.Name = strDriveAlias
        End IF
         
        strProgramPath = "C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
        set objShell = createobject("Wscript.Shell")
        objShell.Run strProgramPath 
        Wscript.Sleep 60000
        Set objNetwork = CreateObject("WScript.Network")
        objNetwork.RemoveNetworkDrive "Z:", True, True
        This script should connect network dive, execute McAfee On-Demand scan, wait for 1 minute and disconnect network drive.

        For some reason command:

        Code:
        strProgramPath = "C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
        not executed... I believe it vbs syntactic error. I can successfully execute the same command from the Windows "Run" menu, or if I change the command in script like:

        Code:
        strProgramPath = "C:\windows\system32\calc.exe"
        It working perfectly. I didn't have match experience with script languages, so I need help with McAfee command executing and if it possible how script can monitor scan32.exe process? When it finish just delete network drive. This is will be great instead of using Wscript.Sleep command...

        Comment


        • #5
          Re: Mapping network drive for limited time

          Originally posted by igor7 View Post
          [COLOR=#555a5f][FONT=Verdana]For some reason command:

          Code:
          strProgramPath = "C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
          not executed... I believe it vbs syntactic error. I can successfully execute the same command from the Windows "Run" menu, or if I change the command in script like:
          should be:
          Code:
          strProgramPath = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
          A string should be wrapped in quotes in the script, and if the string itself contain quotes then double each quote that is within this string.


          At the moment I have limited time till the end of the week, so I quick copied an example from and other script I wrote a while a go. See if you can adapt it.

          In the sample I have disabled the 'time out'-option for the event monitoring part. (It is good to put the 'time out' in later on and use it here as an extra check whether or not the processes was ended w/out noticing for some reason.)

          Code:
          ' map the drive first....
          'then,
          
          strComputer = "."
          Set objSWbemServices = GetObject _
            ("winmgmts:{impersonationLevel=impersonate}!\\"  _
            & strComputer & "\root\cimv2")
          
          ' start the the application
          strCMDline = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
          
          errReturn = objSWbemServices.Get("win32_process").Create _
             (strCMDline, null, null, intProcessID)
          
          ' Initiate monitoring this process
          intTimeOut = 6e4*(360) '--> 6e4*('minutes') monitoring time_out
          intTimeOut = -1
          
          Set objSWbemEventSource = objSWbemServices.ExecNotificationQuery _
              ("Select * From __InstanceDeletionEvent " _ 
              & "Within 10 Where TargetInstance ISA 'Win32_Process'" _
              & "AND TargetInstance.ProcessID = '" & intProcessID & "'")
          Set objSWbemServices = Nothing
          
          If errReturn = 0 then
            On Error Resume Next
            Do Until IsStopped = True
              Set objSWbemObject = objSWbemEventSource.NextEvent(intTimeOut)
              If Err.number <> 0 Then
               'Timed out!
               Err.Clear 
               WScript.Echo 'routine double-check if program still is running If not Exit DO
                   Exit Do
              Else
               If objSWbemObject.TargetInstance.ProcessID _
                = intProcessID Then IsStopped = True
              End If
            Loop
            On Error goTo 0
             If IsStopped = True Then _
               WScript.Echo Now()," process:", strProc,"is terminated"
          Else
            WScript.Echo Now()," There was a problem starting process: ",strProc
          End If
          
          ' unmap drive....
          'end
          /Rems
          Last edited by Rems; 26th June 2011, 16:10.

          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

          Comment


          • #6
            Re: Mapping network drive for limited time

            A string should be wrapped in quotes in the script, and if the string itself then double each quote that is within this string.
            Thats it!! Now it working! Here is the working code:

            Code:
             
            Option Explicit
            Dim objNetwork 
            Dim strDriveLetter, strRemotePath, strDriveAlias, oShell, objShell, strProgramPath
            strDriveLetter = "Z:" 
            strRemotePath = "\\Server\Share"
            strDriveAlias = "My Share" 
            Set objNetwork = CreateObject("WScript.Network")
            Set oShell = CreateObject("Shell.Application") 
            objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
            If Err.Number = 0 Then
            oShell.NameSpace(strDriveLetter).Self.Name = strDriveAlias
            End IF
            
            strProgramPath = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
            set objShell = createobject("Wscript.Shell")
            objShell.Run strProgramPath, 0, True 
            
            Wscript.Sleep 28800000
            
            Set objNetwork = CreateObject("WScript.Network")
            objNetwork.RemoveNetworkDrive "Z:", True, True
            I tried to get work process monitoring code, but no success... If you'll have time, please take a look on it..
            Last edited by igor7; 27th June 2011, 09:11.

            Comment


            • #7
              Re: Mapping network drive for limited time

              I played around with main idea to run McAfee scan using scheduled vbs rather than McAfee schedule task and discover some inconvenience... Actually if I running scan32.exe from the script with visible window:


              Code:
              strProgramPath = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
              set objShell = createobject("Wscript.Shell")
              objShell.Run strProgramPath, 1, True
              The scanning process ends, but scan32.exe window remain opened and as result, scan32.exe process still exist between running processes...




              So, this window remains opened as well as scan32.exe exists between running processes, until I click on "Close" button. This is not so good... because I need to schedule this task to run each 24 hours and if I'll forget to close On-Demand scan window next schedule task will not start because previous still opened...

              I tried to find workaround for this issue by monitoring running scan32.exe process and after desired scanning time (I measured it previously) close On-Demand scan window using "Send Keys" option. Here is my code:

              Code:
               
              'Map Desired Network Drive
              Option Explicit
              Dim objNetwork 
              Dim strDriveLetter, strRemotePath, strDriveAlias, oShell, objShell, strProgramPath
              Dim strComputer, strProcess, wbemLocator, wbemServices, wbemObjectSet, wbemObject  
              strDriveLetter = "Z:" 
              strRemotePath = \\server\share
              strDriveAlias = "My Share" 
              Set objNetwork = CreateObject("WScript.Network")
              Set oShell = CreateObject("Shell.Application") 
              objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
              If Err.Number = 0 Then
              oShell.NameSpace(strDriveLetter).Self.Name = strDriveAlias
              End IF
              ' Execute McAfee On-Demand Scan
              strProgramPath = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
              Const iNormalFocus = 1
              set objShell = createobject("Wscript.Shell")
              objShell.Run strProgramPath
              objShell.Run strProgramPath, iNormalFocus
              ' Force script sleeping during scan time
              Wscript.Sleep 28800000
              ' Monitoring McAfee running process
              strComputer = "."
              strProcess = "scan32.exe"
              Set wbemLocator = CreateObject("WbemScripting.SWbemLocator")
              Set wbemServices = wbemLocator.ConnectServer(strComputer)
              Set wbemObjectSet = wbemServices.InstancesOf("Win32_Process")
              For Each wbemObject In wbemObjectSet
              If LCase(wbemObject.Name) = strProcess Then
              ' Focus on On-Demand Scan window 
              Set objShell = WScript.CreateObject("WScript.Shell")
              ' Bring the Scan window to the foreground
              objShell.AppActivate "On-Demand Scan Progress - Mapped Network Drive Scan"
              WScript.Sleep 200
              ' Close On-Demand Scan window
              objShell.SendKeys "%{F4}", True
              End if
              Next
              ' Disconnect Network Drive
              objNetwork.RemoveNetworkDrive "Z:", True, True
              ' Send User notification 
              WScript.Echo "Network drive scan has been DONE!" & _
                           " Check On-Demand log file for details"
              wscript.quit


              I've ran this script few times for scan some network shares which are about few GB size and scanning process take not longer than ~ 1hour. Send Keys method working well together with AppActivate method, which is using to bring running application window to foreground. Here is explanation about Send Keys and AppActivate from Microsoft if somebody interesting...:

              http://technet.microsoft.com/en-us/l.../ee156592.aspx

              I have only problem with my own goal... When I running this script for scan network share which is about 1,5 TB size, scanning time take around 7-8 hours (as I said I measured time previously),- script is working perfect, but send keys and AppActivate methods are not worked,- On-Demand window remain opened after scanning was done. I have no idea why it’s working for "short time" scan and not working for "long time" scan... Any suggestions are much appreciated.

              Comment


              • #8
                Re: Mapping network drive for limited time

                Originally posted by igor7 View Post
                I played around with main idea to run McAfee scan using scheduled vbs rather than McAfee schedule task and discover some inconvenience... Actually if I running scan32.exe from the script with visible window:
                Code:
                strProgramPath = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
                set objShell = createobject("Wscript.Shell")
                objShell.Run strProgramPath, 1, True
                The scanning process ends, but scan32.exe window remain opened and as result, scan32.exe process still exist between running processes...
                Instead of using SendKey, try this command line to run the task,
                Code:
                """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" -UINONE -START -task {19BAC370-F5A0-4C76-9591-A724D94F6059} -NOESTIMATE -AUTOEXIT -ALWAYSEXIT"
                Note.. the -UINONE switchr by itself makes the scan32.exe task not only run HIDDEN it also auto-close it afterwards.
                So in your case only the two arguments /uiNone and /Task {...} would be sufficient, but if you don't want the job run hidden then you use /autoExit and /alwaysExit.

                /Rems

                EDIT: McAfee_SCAN32_CommandLineOptions
                Last edited by Rems; 13th July 2011, 13:57.

                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

                Comment


                • #9
                  Re: Mapping network drive for limited time

                  ...Note.. -UINONE makes it run HIDDEN.
                  Rems,
                  Many thanks, now it work as expected!

                  Comment


                  • #10
                    Re: Mapping network drive for limited time

                    One more question...
                    To make McAfee scanning script more flexible, I'm trying to replace
                    Code:
                    Wscript.Sleep 28800000
                    Code with process monitoring code which should check if scan32.exe is still running or not. The monitoring code currently using in my script depends on sleep option and if data on network share will grow, the scanning time will also grow, so I'll need to measure scanning time accordingly, again and again... and this is not so suitable... Here is some application monitoring code which I'm trying to adapt for my needs:

                    Code:
                    option explicit
                    DIM strProcess, strComputer
                    strComputer = "." 
                    strProcess = "scan32.exe"
                     
                    ' Check if Application is running 
                    if isProcessRunning(strComputer,strProcess) then
                    wscript.echo strProcess & " is running" 
                    else
                    wscript.echo strProcess & " is NOT running"
                    end if
                     
                    ' Function to check if a process is running
                    function isProcessRunning(byval strComputer,byval strProcessName)
                    Dim objWMIService, strWMIQuery
                    strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"
                    Set objWMIService = GetObject("winmgmts:" _
                    & "{impersonationLevel=impersonate}!\\" _ 
                    & strComputer & "\root\cimv2")
                     
                    if objWMIService.ExecQuery(strWMIQuery).Count > 0 then
                    isProcessRunning = true
                    else
                    isProcessRunning = false
                     
                    end if
                    end function
                    My question is how to make this code running with "Loop" option or Loop with condition based on "isProcessRunning = false" option? If this condition is true, for example, the script will go to the next line and disconnect mapped network drive.

                    Comment


                    • #11
                      Re: Mapping network drive for limited time

                      Originally posted by igor7 View Post
                      I played around with main idea to run McAfee scan using scheduled vbs rather than McAfee schedule task and discover some inconvenience... Actually if I running scan32.exe from the script <...>:

                      Code:
                      strProgramPath = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" -task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
                      set objShell = createobject("Wscript.Shell")
                      objShell.Run strProgramPath, 1, True

                      If you would start the scan32.exe task like,
                      Code:
                      Set objSWbemServices = GetObject _
                         ("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
                      
                      ' start the application
                      strProgramPath = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" /UiNone /Task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
                      
                      errReturn = objSWbemServices.Get("win32_process").Create _
                         (strProgramPath, null, null, intProcessID)
                      then the script set the variable intProcessID. The variable intProcessID expands the PID of the running job. Monitor the PID rather than the process image (scan32.exe) to always be sure the script cannot not start checking on a different scan32.exe job.

                      _
                      You can monitor the task by PID like,
                      Code:
                      If errReturn = 0 then   ' start monitoring the running job
                      
                         Set objSWbemEventSource = objSWbemServices.ExecNotificationQuery _
                           ("Select * From __InstanceDeletionEvent " _ 
                           & "Within 600 Where TargetInstance ISA 'Win32_Process'" _
                           & "AND TargetInstance.ProcessID = '" & intProcessID & "'")
                      
                         On Error Resume Next
                         Do Until IsStopped = True
                           Set objSWbemObject = objSWbemEventSource.NextEvent()
                           If (objSWbemObject.TargetInstance.ProcessID = intProcessID) _
                                Then IsStopped = True
                         Loop
                         On Error goTo 0
                      End If
                      
                      WScript.Echo Now()," process:", strProc,"is terminated"
                      Where "Within 600" means that the script checks only one time in every 10 minutes for the PID. This is why this script mostly will not respond in just a split second, some times it can take up 0 to 10 minutes before the script actually notice the task has ended. You could change the value for "Within" to 1 but it will increase the use of memory. Commonly a 5 or 10 seconds interval is used in scripts, but for a task that runs for approx. 8 hours long a polling interval of 600 seconds (or even higher) will be fine enough I guess.


                      _
                      The complete script will look something like,
                      Code:
                      rem Option Explicit
                      
                      ' Map Desired Network Drive
                      Z_drive "connect"
                      
                      ' Execute McAfee On-Demand Scan
                      scantask
                      
                      ' Disconnect Network Drive
                      Z_drive "disconnect"
                      
                      wscript.quit 0
                      
                      
                      Sub scantask
                         Dim objSWbemServices, objSWbemEventSource
                         Dim strProgramPath, errReturn, intProcessID
                      
                         Set objSWbemServices = GetObject _
                           ("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
                      
                         ' start the the application
                         strProgramPath = """C:\Program Files\McAfee\VirusScan Enterprise\scan32.exe"" /UiNone /Task {19BAC370-F5A0-4C76-9591-A724D94F6059}"
                      
                         errReturn = objSWbemServices.Get("win32_process").Create _
                           (strProgramPath, null, null, intProcessID)
                      
                         If errReturn = 0 then   ' start polling the running job
                           Set objSWbemEventSource = objSWbemServices.ExecNotificationQuery _
                             ("Select * From __InstanceDeletionEvent " _ 
                             & "Within 600 Where TargetInstance ISA 'Win32_Process'" _
                             & "AND TargetInstance.ProcessID = '" & intProcessID & "'")
                      
                           On Error Resume Next
                           Do
                             Set objSWbemObject = objSWbemEventSource.NextEvent()
                             If (objSWbemObject.TargetInstance.ProcessID = intProcessID) _
                                 Then exit Do
                           Loop
                           On Error goTo 0
                         Else
                           wscript.echo now(), "Unable to start the predifined scan task." & _
                             vbNewline & "-Job will be Canceled-"
                           wscript.quit
                         End If
                      End Sub
                      
                      Sub Z_drive (c)
                         Dim strDriveLetter, strRemotePath, strDriveAlias
                      
                         strDriveLetter = "Z:"
                         strRemotePath = "\\server\share$"
                         strDriveAlias = "My Share"
                      
                         On Error Resume Next
                         With CreateObject("WScript.Network")
                      
                           Select Case "connect"
                      
                             Case lcase(c)
                               .MapNetworkDrive strDriveLetter, strRemotePath, False
                      
                               If err.Number = 0 Then
                                With CreateObject("Shell.Application")
                                  .NameSpace(strDriveLetter).Self.Name = strDriveAlias
                                End With
                               Else
                                 wscript.echo now(), "Unable to map " & strDriveAlias _
                                   & " for the VSE ODS task." & _
                                   vbNewline & "-Job will be Canceled-"
                                 wscript.quit
                               End if
                      
                      
                             Case Else
                                 .RemoveNetworkDrive strDriveLetter, True, True
                      
                                 ' Send User notification 
                                 WScript.Echo now(), "Network drive scan has been DONE!" & _
                                   " Check On-Demand log file for details"
                           End Select
                      
                         End With
                         On Error Goto 0
                      End Sub
                      EDIT: For this vbscript I did not set the intTimeOut option for the objSWbemEventSource.NextEvent().
                      In cases he scan task sometimes would take more time than allowed or, the task hangs once in a while or, the process monitor appears not to be reliable for this process then you probably would like to implement an "If timed-out Do..." procedure in the script.


                      /Rems
                      Last edited by Rems; 13th July 2011, 17:17.

                      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

                      Comment


                      • #12
                        Re: Mapping network drive for limited time

                        Rems, many thanks!!
                        Script working perfectly now!
                        Last edited by igor7; 18th July 2011, 12:42.

                        Comment

                        Working...
                        X