Reconnecting Home Drive and Network Drive Mapping when on users are on VPN or ZPA

In System Administrator, VBScript by jtruong6 Comments

In this article, I will show you how to automate reconnecting of user’s network mapping (home drive and all network drives) using VBScript and schedule task. When the workstation connects through VPN or ZPA, the home directory does not map automatically and network drives are set in the “Disconnected” state.

I will update this post with a list of other VPN’s registry value changes, but for now we’ll stick with Zscaler’s ZPA. Subscribe below to get an update on when the list is posted.

To begin, we need to determine the registry value changes when ZPA is enabled; this is because we don’t want the script to run when the user is on-premises. When ZPA is enabled on the client, the registry value changes to “TUNNEL_FORWARDING”. The below VBScript will obtain the value in the registry to get the current state of ZPA.

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Zscaler\App"
strValueName = "ZPA_State"
objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

If dwValue = "TUNNEL_FORWARDING" Then
  'TRIGGER EVENTS HERE
End If

Refer to:
https://docs.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks–registry

Next, we need to check if the H drive (home directory) exists on the workstation and also get the user’s homedirectory path from the domain. This will grab the information from the homedirectory attribute value in Active Directory via ADSystemInfo object. To do this, we need to use the FileSystemObject and GetObject commands.

Set objFS = CreateObject("Scripting.FileSystemObject")

If NOT objFS.DriveExists("H:\") Then
    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

    Set objNetwork = WScript.CreateObject("WScript.Network")
    strHomeDir = objUser.homedirectory
    objNetwork.MapNetworkDrive "H:", strHomeDir, True
End If

Refer to:
https://ss64.com/vb/filesystemobject.html
https://ss64.com/vb/syntax-userinfo.html
https://ss64.com/vb/mapnetworkdrive.html

In this next step, we will scan the local workstation for any other network drives that are in the “Disconnected” state; then remap to make them in active “Connected” in order to avoid the “Red X” on the network drive.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\Root\CIMv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)

For Each objItem in colItems
    If objItem.ConnectionState = "Disconnected" Then
        Set objNetwork = WScript.CreateObject("WScript.Network")
        objNetwork.MapNetworkDrive objItem.LocalName, objItem.RemoteName, True
    End If
Next

Refer to:
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkconnection

So to put it all together, the full script should look like this:

On Error Resume Next
Dim objFS,objNetwork

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Zscaler\App"
strValueName = "ZPA_State"
objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

If dwValue = "TUNNEL_FORWARDING" Then
    Set objFS = CreateObject("Scripting.FileSystemObject")

    If NOT objFS.DriveExists("H:\") Then
    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

    Set objNetwork = WScript.CreateObject("WScript.Network")
    strHomeDir = objUser.homedirectory
    objNetwork.MapNetworkDrive "H:", strHomeDir, True
    End If

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\Root\CIMv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)

    For Each objItem in colItems
        If objItem.ConnectionState = "Disconnected" Then
            Set objNetwork = WScript.CreateObject("WScript.Network")
            objNetwork.MapNetworkDrive objItem.LocalName, objItem.RemoteName, True
        End If
    Next
End If

We’re not done, we still need to create a group policy (GPO) to do two things: (1) copy the VBScript and (2) run the VBScript at startup and/or when a new network connection happens. The script needs to be copied over to the local workstation because the end-user will receive a “file not found error” when ZPA is not enabled off the network.

The .vbs file copy group policy should have the following settings:

Action: Update
Source file(s): \\<domain DNS name>\SYSVOL\scripts\ReconnectNetworkDrive.vbs
Destination File: C:\Windows\System32\ReconnectNetworkDrive.vbs
Item-Level Targeting: a battery is present

The Scheduled Task group policy should have the following settings:

Name: Reconnect Drive Mapping
Action: Create
When running the task, use the following user account: %LogonDomain%\%LogonUser%
Run only when user is logged on: Selected
Hidden: Checked

Trigger: On An Event – New Network Connection (See image below)
Trigger: At log on
Trigger Delay: 1 minute

New Action: Start a program
Program/script: C:\Windows\System32\wscript.exe
Add Arguments: C:\WINDOWS\System32\ReconnectNetworkDrive.vbs
Start the task only if the computer is on AC power: Unchecked
Item-Level Targeting: a battery is present

That’s it! Well, let me know if you have any questions or if this script helped you resolve your issues in the comment section below!

5 5 votes
Article Rating
Subscribe
Notify of
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jason R

Are you running this GPO in the user or computer context? I can get the file copy component to work, but with these settings for scheduled task I am getting 0x80070057 The parameter is incorrect.’ on client and task is not created. Tried filling “start in” with C:\windows\system32 but still get same error.

Jason R.

I got it to run under the user context as recommended, but unfortunately it does not remove the red disconnects on the drives– user still has to “touch” them to get them to reconnect. I even ran it from the command line, and I did make sure to replace “H:” with our home drive letter U:. This issue has plagued us for ages with laptops, with or without ZPA.

Jason R.

Yes we do use GPO’s to map drives, including the home drive. One thing that I have noticed is that drives that are mapped explicitly by server name don’t get the red X, but those mapped by DFS namespace have to be “touched” to wake up and go into a connected state. For instance, in the output below, M is mapped directly to a specific server, while the other drives map off the dfs namespace. zpa state value: tunnel_forwarding home drive exists: false user value=me home drive value \\domain.com\data\mydocs\me caption: resource remembered connection state: disconnected connection type: persistent displaytype=share localname:… Read more »