Mapping network drive for limited time

Home Forums Scripting General Scripting Mapping network drive for limited time

This topic contains 11 replies, has 2 voices, and was last updated by Avatar igor7 8 years, 8 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • Avatar
    igor7
    Member
    #155201

    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:

    ‘ —————————————————-‘
    Option Explicit
    Dim objNetwork
    Dim strDriveLetter, strRemotePath, strDriveAlias, oShell
    strDriveLetter = “Z:”
    strRemotePath = [URL=”file://server/Share”]\ServerShare[/URL]
    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
    ‘ —————————————————-‘
    [/CODE]

    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
    ‘ —————————————————‘

    [/CODE][CODE]



    Option Explicit
    Dim objNetwork
    Dim strDriveLetter, strRemotePath, strDriveAlias, oShell
    strDriveLetter = “Z:”
    strRemotePath = [URL=”file://server/Share”]\ServerShare[/URL]
    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



    [/CODE]

    Disconnect Network Drive:

    ‘ —————————————————-‘
    Option Explicit
    Dim objNetwork, strDriveLetter
    strDriveLetter = “Z:”
    Set objNetwork = CreateObject(“WScript.Network”)
    ‘ Section which removes strDriveLetter
    objNetwork.RemoveNetworkDrive strDriveLetter
    Wscript.Quit
    ‘ —————————————————‘

    [/CODE][CODE]



    Option Explicit
    Dim objNetwork, strDriveLetter
    strDriveLetter = “Z:”
    Set objNetwork = CreateObject(“WScript.Network”)
    ‘ Section which removes strDriveLetter
    objNetwork.RemoveNetworkDrive strDriveLetter
    Wscript.Quit


    [/CODE]

    Rems
    Rems
    Moderator
    #228011

    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_MACHINESOFTWAREMcAfeeDesktopProtectionTasks” 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)McAfeeVirusScan Enterprisex64Scan64.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

    Avatar
    igor7
    Member
    #295069

    Re: Mapping network drive for limited time

    Quote:
    Yes it can be done with one script, simply by making it Sleep for 8 hours.
    Quote:
    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…

    Quote:
    Try to find out what process is running during the scan.

    The process name is scan32.exe

    GUID of the task:

    “C:Program FilesMcAfeeVirusScan Enterprisescan32.exe” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}[/CODE][/FONT][/COLOR]
    [COLOR=#555a5f][FONT=Verdana]szTaskName: Mapped Network Drive Scan[/FONT][/COLOR]

    [COLOR=#555a5f][FONT=Verdana]So far this task need to be run only on one server so GUID could be hardcoded in the script.[/FONT][/COLOR]
    Thank you in advance,
    Igor.[CODE]”C:Program FilesMcAfeeVirusScan Enterprisescan32.exe” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}[/CODE]
    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.

    Avatar
    igor7
    Member
    #295070

    Re: Mapping network drive for limited time

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

    [COLOR=#555a5f][FONT=Verdana]Option Explicit[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]Dim objNetwork [/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]Dim strDriveLetter, strRemotePath, strDriveAlias, oShell, objShell, strProgramPath[/COLOR][/FONT]
    [COLOR=#555a5f][FONT=Verdana]strDriveLetter = “Z:” [/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]strRemotePath = “[URL=”file://server/Share”][COLOR=#0000ff]\ServerShare[/COLOR][/URL]”[/COLOR][/FONT][COLOR=#555a5f]
    [FONT=Verdana]strDriveAlias = “My Share” [/FONT]
    [FONT=Verdana]Set objNetwork = CreateObject(“WScript.Network”)[/FONT]
    [FONT=Verdana]Set oShell = CreateObject(“Shell.Application”) [/FONT]
    [FONT=Verdana]objNetwork.MapNetworkDrive strDriveLetter, strRemotePath[/FONT]
    [FONT=Verdana]If Err.Number = 0 Then[/FONT]
    [FONT=Verdana]oShell.NameSpace(strDriveLetter).Self.Name = strDriveAlias[/FONT]
    [FONT=Verdana]End IF[/FONT][/COLOR]

    [FONT=Verdana][COLOR=#555a5f]strProgramPath = “C:Program FilesMcAfeeVirusScan Enterprisescan32.exe” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]set objShell = createobject(“Wscript.Shell”)[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]objShell.Run strProgramPath [/COLOR][/FONT]
    [COLOR=#555a5f][FONT=Verdana]Wscript.Sleep 60000[/FONT][/COLOR]
    [COLOR=#555a5f][FONT=Verdana]Set objNetwork = CreateObject(“WScript.Network”)[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]objNetwork.RemoveNetworkDrive “Z:”, True, True[/COLOR][/FONT]
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]This script should connect network dive, execute McAfee On-Demand scan, wait for 1 minute and disconnect network drive.[/FONT][/COLOR]

    [COLOR=#555a5f][FONT=Verdana]For some reason command:[/FONT][/COLOR]

    [CODE]
    [COLOR=#555a5f][FONT=Verdana]strProgramPath = “C:Program FilesMcAfeeVirusScan Enterprisescan32.exe” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”[/FONT][/COLOR]
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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:[/FONT][/COLOR]

    [CODE]
    [COLOR=#555a5f][FONT=Verdana]strProgramPath = “C:windowssystem32calc.exe”[/FONT][/COLOR]
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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…[/FONT][/COLOR][CODE]
    Option Explicit
    Dim objNetwork
    Dim strDriveLetter, strRemotePath, strDriveAlias, oShell, objShell, strProgramPath
    strDriveLetter = “Z:”
    strRemotePath = “[URL=”file://server/Share”]\ServerShare[/URL]”
    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 FilesMcAfeeVirusScan Enterprisescan32.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
    [/CODE]

    This script should connect network dive, execute McAfee On-Demand scan, wait for 1 minute and disconnect network drive.

    For some reason command:

    [COLOR=#555a5f][FONT=Verdana]strProgramPath = “C:Program FilesMcAfeeVirusScan Enterprisescan32.exe” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”[/FONT][/COLOR]
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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:[/FONT][/COLOR]

    [CODE]
    [COLOR=#555a5f][FONT=Verdana]strProgramPath = “C:windowssystem32calc.exe”[/FONT][/COLOR]
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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…[/FONT][/COLOR][CODE]
    strProgramPath = “C:Program FilesMcAfeeVirusScan Enterprisescan32.exe” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”
    [/CODE]

    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:

    [COLOR=#555a5f][FONT=Verdana]strProgramPath = “C:windowssystem32calc.exe”[/FONT][/COLOR]
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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…[/FONT][/COLOR][CODE]
    strProgramPath = “C:windowssystem32calc.exe”
    [/CODE]

    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…

    Rems
    Rems
    Moderator
    #228012

    Re: Mapping network drive for limited time

    igor7;241062 wrote:
    For some reason command:


    strProgramPath = “C:Program FilesMcAfeeVirusScan Enterprisescan32.exe” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”[/CODE]

    [COLOR=#555a5f][FONT=Verdana]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:[/FONT][/COLOR]
    [/QUOTE]
    should be:
    [CODE]strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”[/CODE]
    A string should be wrapped in quotes in the script, and if the string itself contain quotes then double each quote that is [I]within[/I] 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][COLOR=”red”]’ map the drive first….[/COLOR]
    ‘then,

    strComputer = “.”
    Set objSWbemServices = GetObject _
    (“winmgmts:{impersonationLevel=impersonate}!\” _
    & strComputer & “rootcimv2”)

    ‘ start the the application
    strCMDline = “[COLOR=”RoyalBlue”]””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}[/COLOR]”

    errReturn = objSWbemServices.Get(“win32_process”).Create _
    (strCMDline, null, null, intProcessID)

    ‘ Initiate monitoring this process
    intTimeOut = 6e4*(360) ‘–> 6e4*(‘minutes’) monitoring time_out
    [COLOR=”Orange”]intTimeOut = -1[/COLOR]

    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

    [COLOR=”Red”]’ unmap drive….
    ‘end[/COLOR]
    [/CODE]

    /Rems[CODE]
    strProgramPath = “C:Program FilesMcAfeeVirusScan Enterprisescan32.exe” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”[/CODE]

    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:
    strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”[/CODE]
    A string should be wrapped in quotes in the script, and if the string itself contain quotes then double each quote that is [I]within[/I] 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][COLOR=”red”]’ map the drive first….[/COLOR]
    ‘then,

    strComputer = “.”
    Set objSWbemServices = GetObject _
    (“winmgmts:{impersonationLevel=impersonate}!\” _
    & strComputer & “rootcimv2”)

    ‘ start the the application
    strCMDline = “[COLOR=”RoyalBlue”]””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}[/COLOR]”

    errReturn = objSWbemServices.Get(“win32_process”).Create _
    (strCMDline, null, null, intProcessID)

    ‘ Initiate monitoring this process
    intTimeOut = 6e4*(360) ‘–> 6e4*(‘minutes’) monitoring time_out
    [COLOR=”Orange”]intTimeOut = -1[/COLOR]

    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

    [COLOR=”Red”]’ unmap drive….
    ‘end[/COLOR]
    [/CODE]

    /Rems[CODE]strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”[/CODE]
    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.)

    [COLOR=”red”]’ map the drive first….[/COLOR]
    ‘then,

    strComputer = “.”
    Set objSWbemServices = GetObject _
    (“winmgmts:{impersonationLevel=impersonate}!\” _
    & strComputer & “rootcimv2”)

    ‘ start the the application
    strCMDline = “[COLOR=”RoyalBlue”]””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}[/COLOR]”

    errReturn = objSWbemServices.Get(“win32_process”).Create _
    (strCMDline, null, null, intProcessID)

    ‘ Initiate monitoring this process
    intTimeOut = 6e4*(360) ‘–> 6e4*(‘minutes’) monitoring time_out
    [COLOR=”Orange”]intTimeOut = -1[/COLOR]

    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

    [COLOR=”Red”]’ unmap drive….
    ‘end[/COLOR]
    [/CODE]

    /Rems[CODE]‘ map the drive first….
    ‘then,

    strComputer = “.”
    Set objSWbemServices = GetObject _
    (“winmgmts:{impersonationLevel=impersonate}!\” _
    & strComputer & “rootcimv2”)

    ‘ start the the application
    strCMDline = ““”C:Program FilesMcAfeeVirusScan Enterprisescan32.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

    [/CODE]

    /Rems

    Avatar
    igor7
    Member
    #295071

    Re: Mapping network drive for limited time

    Quote:
    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:

    Option Explicit
    Dim objNetwork
    Dim strDriveLetter, strRemotePath, strDriveAlias, oShell, objShell, strProgramPath
    strDriveLetter = “Z:”
    strRemotePath = “[URL=”file://server/Share”]\ServerShare[/URL]”
    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 FilesMcAfeeVirusScan Enterprisescan32.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
    [/CODE]

    I tried to get work process monitoring code, but no success… If you’ll have time, please take a look on it..[CODE]
    Option Explicit
    Dim objNetwork
    Dim strDriveLetter, strRemotePath, strDriveAlias, oShell, objShell, strProgramPath
    strDriveLetter = “Z:”
    strRemotePath = “[URL=”file://server/Share”]\ServerShare[/URL]”
    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 FilesMcAfeeVirusScan Enterprisescan32.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
    [/CODE]

    I tried to get work process monitoring code, but no success… If you’ll have time, please take a look on it..

    Avatar
    igor7
    Member
    #295072

    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:

    strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”
    set objShell = createobject(“Wscript.Shell”)
    objShell.Run strProgramPath, 1, True[/CODE]

    [COLOR=#555a5f][FONT=Verdana]The scanning process ends, but scan32.exe window remain opened and as result, scan32.exe process still exist between running processes…[/FONT][/COLOR]

    [IMG]http://img37.picoodle.com/i578/igor7/9_6af_ucfvo.jpg[/IMG]

    [COLOR=#555a5f][FONT=Verdana]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…[/FONT][/COLOR]

    [COLOR=#555a5f][FONT=Verdana]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:[/FONT][/COLOR]

    [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 = [URL=”file://\servershare”]\servershare[/URL]
    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 FilesMcAfeeVirusScan Enterprisescan32.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
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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…:[/FONT][/COLOR]

    [COLOR=#555a5f][FONT=Verdana][URL]http://technet.microsoft.com/en-us/library/ee156592.aspx[/URL][/FONT][/COLOR]

    [COLOR=#555a5f][FONT=Verdana]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.[/FONT][/COLOR][CODE]strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”
    set objShell = createobject(“Wscript.Shell”)
    objShell.Run strProgramPath, 1, True[/CODE]

    The scanning process ends, but scan32.exe window remain opened and as result, scan32.exe process still exist between running processes…

    9_6af_ucfvo.jpg

    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:

    ‘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 = [URL=”file://\servershare”]\servershare[/URL]
    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 FilesMcAfeeVirusScan Enterprisescan32.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
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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…:[/FONT][/COLOR]

    [COLOR=#555a5f][FONT=Verdana][URL]http://technet.microsoft.com/en-us/library/ee156592.aspx[/URL][/FONT][/COLOR]

    [COLOR=#555a5f][FONT=Verdana]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.[/FONT][/COLOR][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 = [URL=”file://\servershare”]\servershare[/URL]
    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 FilesMcAfeeVirusScan Enterprisescan32.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
    [/CODE]

    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/library/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.

    Rems
    Rems
    Moderator
    #228021

    Re: Mapping network drive for limited time

    igor7;241932 wrote:
    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:
    strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”
    set objShell = createobject(“Wscript.Shell”)
    objShell.Run strProgramPath, 1, True[/CODE]
    The scanning process ends, but scan32.exe window remain opened and as result, scan32.exe process still exist between running processes…
    [/QUOTE]

    Instead of using SendKey, try this command line to run the task,
    [CODE]”””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -UINONE -START -task {19BAC370-F5A0-4C76-9591-A724D94F6059} -NOESTIMATE -AUTOEXIT -ALWAYSEXIT”[/CODE]
    Note.. the [B]-UINONE[/B] 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 [B]/uiNone[/B] and [B]/Task {…}[/B] would be sufficient, but if you don’t want the job run hidden then you use /autoExit and /alwaysExit.

    /Rems

    EDIT: [url=http://68.106.14.62:26359/Tipsheets/McAfee/McAfee_SCAN32_CommandLineOptions.pdf]McAfee_SCAN32_CommandLineOptions[/url][CODE]strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”
    set objShell = createobject(“Wscript.Shell”)
    objShell.Run strProgramPath, 1, True[/CODE]
    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,
    “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -UINONE -START -task {19BAC370-F5A0-4C76-9591-A724D94F6059} -NOESTIMATE -AUTOEXIT -ALWAYSEXIT”[/CODE]
    Note.. the [B]-UINONE[/B] 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 [B]/uiNone[/B] and [B]/Task {…}[/B] would be sufficient, but if you don’t want the job run hidden then you use /autoExit and /alwaysExit.

    /Rems

    EDIT: [url=http://68.106.14.62:26359/Tipsheets/McAfee/McAfee_SCAN32_CommandLineOptions.pdf]McAfee_SCAN32_CommandLineOptions[/url][CODE]”””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -UINONE -START -task {19BAC370-F5A0-4C76-9591-A724D94F6059} -NOESTIMATE -AUTOEXIT -ALWAYSEXIT”[/CODE]
    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

    Avatar
    igor7
    Member
    #295073

    Re: Mapping network drive for limited time

    Quote:
    …Note.. -UINONE makes it run HIDDEN.

    Rems,
    Many thanks, now it work as expected!

    Avatar
    igor7
    Member
    #295074

    Re: Mapping network drive for limited time

    One more question…
    To make McAfee scanning script more flexible, I’m trying to replace

    [COLOR=#555a5f][FONT=Verdana]Wscript.Sleep 28800000[/FONT][/COLOR]
    [/CODE]
    [COLOR=#555a5f][FONT=Verdana]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:[/FONT][/COLOR]

    [CODE]
    [COLOR=#555a5f][FONT=Verdana]option explicit[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]DIM strProcess, strComputer[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]strComputer = “.” [/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]strProcess = “scan32.exe”[/COLOR][/FONT]

    [COLOR=#555a5f][FONT=Verdana]’ Check if Application is running [/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]if isProcessRunning(strComputer,strProcess) then[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]wscript.echo strProcess & ” is running” [/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]else[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]wscript.echo strProcess & ” is NOT running”[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]end if[/COLOR][/FONT]

    [COLOR=#555a5f][FONT=Verdana]’ Function to check if a process is running[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]function isProcessRunning(byval strComputer,byval strProcessName)[/COLOR][/FONT]
    [COLOR=#555a5f][FONT=Verdana]Dim objWMIService, strWMIQuery[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]strWMIQuery = “Select * from Win32_Process where name like ‘” & strProcessName & “‘”[/COLOR][/FONT]
    [COLOR=#555a5f][FONT=Verdana]Set objWMIService = GetObject(“winmgmts:” _[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]& “{impersonationLevel=impersonate}!\” _ [/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]& strComputer & “rootcimv2”)[/COLOR][/FONT]

    [COLOR=#555a5f][FONT=Verdana]if objWMIService.ExecQuery(strWMIQuery).Count > 0 then[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]isProcessRunning = true[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]else[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]isProcessRunning = false[/COLOR][/FONT]

    [FONT=Verdana][COLOR=#555a5f]end if[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]end function[/COLOR][/FONT]
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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.[/FONT][/COLOR][CODE]
    Wscript.Sleep 28800000
    [/CODE]
    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:

    [COLOR=#555a5f][FONT=Verdana]option explicit[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]DIM strProcess, strComputer[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]strComputer = “.” [/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]strProcess = “scan32.exe”[/COLOR][/FONT]

    [COLOR=#555a5f][FONT=Verdana]’ Check if Application is running [/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]if isProcessRunning(strComputer,strProcess) then[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]wscript.echo strProcess & ” is running” [/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]else[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]wscript.echo strProcess & ” is NOT running”[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]end if[/COLOR][/FONT]

    [COLOR=#555a5f][FONT=Verdana]’ Function to check if a process is running[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]function isProcessRunning(byval strComputer,byval strProcessName)[/COLOR][/FONT]
    [COLOR=#555a5f][FONT=Verdana]Dim objWMIService, strWMIQuery[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]strWMIQuery = “Select * from Win32_Process where name like ‘” & strProcessName & “‘”[/COLOR][/FONT]
    [COLOR=#555a5f][FONT=Verdana]Set objWMIService = GetObject(“winmgmts:” _[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]& “{impersonationLevel=impersonate}!\” _ [/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]& strComputer & “rootcimv2”)[/COLOR][/FONT]

    [COLOR=#555a5f][FONT=Verdana]if objWMIService.ExecQuery(strWMIQuery).Count > 0 then[/FONT][/COLOR]
    [FONT=Verdana][COLOR=#555a5f]isProcessRunning = true[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]else[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]isProcessRunning = false[/COLOR][/FONT]

    [FONT=Verdana][COLOR=#555a5f]end if[/COLOR][/FONT]
    [FONT=Verdana][COLOR=#555a5f]end function[/COLOR][/FONT]
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]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.[/FONT][/COLOR][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 & “rootcimv2”)

    if objWMIService.ExecQuery(strWMIQuery).Count > 0 then
    isProcessRunning = true
    else
    isProcessRunning = false

    end if
    end function
    [/CODE]

    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.

    Rems
    Rems
    Moderator
    #228025

    Re: Mapping network drive for limited time

    igor7;241932 wrote:
    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:
    [COLOR=#555a5f][FONT=Verdana]strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” -task {19BAC370-F5A0-4C76-9591-A724D94F6059}”
    set objShell = createobject(“Wscript.Shell”)
    objShell.Run strProgramPath, 1, True[/FONT][/COLOR]
    [/CODE]
    [/SIZE][/QUOTE]

    If you would start the scan32.exe task like,
    [code]
    Set objSWbemServices = GetObject _
    (“winmgmts:{impersonationLevel=impersonate}!\.rootcimv2”)

    ‘ start the application
    strProgramPath = “””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” /UiNone /Task {19BAC370-F5A0-4C76-9591-A724D94F6059}”

    [COLOR=”Red”][B]errReturn[/B][/COLOR] = 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 [COLOR=”red”][B]errReturn[/B][/COLOR] = 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 “[COLOR=”Red”]connect[/COLOR]”

    ‘ Execute McAfee On-Demand Scan
    scantask

    ‘ Disconnect Network Drive
    Z_drive “[COLOR=”red”]disconnect[/COLOR]”

    wscript.quit 0

    Sub scantask
    Dim objSWbemServices, objSWbemEventSource
    Dim strProgramPath, errReturn, intProcessID

    Set objSWbemServices = GetObject _
    (“winmgmts:{impersonationLevel=impersonate}!\.rootcimv2”)

    ‘ start the the application
    strProgramPath = “[COLOR=”darkslateblue”]””C:Program FilesMcAfeeVirusScan Enterprisescan32.exe”” /UiNone /Task {19BAC370-F5A0-4C76-9591-A724D94F6059}[/COLOR]”

    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 = “[COLOR=”DarkSlateBlue”]Z:[/COLOR]”
    strRemotePath = “[COLOR=”darkslateblue”]\servershare$[/COLOR]”
    strDriveAlias = “[COLOR=”darkslateblue”]My Share[/COLOR]”

    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

    Avatar
    igor7
    Member
    #295075

    Re: Mapping network drive for limited time

    Rems, many thanks!!
    Script working perfectly now!

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.