Wednesday 6 February 2013

Appsense Wake Up disconnected Drives

The following is some handy code given out by Appsense to reconnect disconnected network drives.
This prevents the problems of network drives having the red X letter next to them. This red X can cause problems for applications and scripts that try to use the drive while its in this disconnected states.

The user clicking on the drive re-establishes the connection and this script does the same thing.


'DisconnectedHivedDriveReconnect    
'
'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'PARTICULAR PURPOSE.     
'
'v1.0 - 26/04/2012 Script Written by Mark Titley 

On Error Resume Next

Dim oShell, oNetwork, oWMI, oFSO, arrDiscNetCon, DiscNetCon, strLocalName, strRemotePath, strUserName

Dim bDebugLog : bDebugLog = False 'If set to True extra logging is sent to the event log

Set oShell = Wscript.CreateObject("Wscript.Shell")
Set oNetwork = WScript.CreateObject("WScript.Network")
Set oWMI = GetObject("winmgmts:\\.\root\CIMV2")
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set arrDiscNetCon = oWMI.ExecQuery("Select * from Win32_NetworkConnection where ConnectionState != 'Connected' and Persistent = True")
        
For Each DiscNetCon In arrDiscNetCon
   strLocalName = DiscNetCon.LocalName
   strRemotePath = DiscNetCon.RemotePath
   strUserName = DiscNetCon.UserName
If oFSO.DriveExists(strLocalName) Then
    debugLog "LocalName " & strLocalName & " is disconnected but DOES Exist and is mapped to RemotePath:" & strRemotePath, 1
    Else
    If strUserName = "" Then
  debugLog "LocalName: " & strLocalName & " is disconnected and DOES NOT Exist - will attempt to remap to RemotePath:" & strRemotePath, 1
  oNetwork.MapNetworkDrive strLocalName, strRemotePath, True
  Else
  debugLog "LocalName: " & strLocalName & " is disconnected and DOES NOT Exist - will attempt to remap to RemotePath:" & strRemotePath & " with UserName: " & strUserName, 1
  oNetwork.MapNetworkDrive strLocalName, strRemotePath, True, strUserName
End If
    If Err <> 0 Then
debugLog "Error mapping drive: " & strLocalName & " to " & strRemotePath & vbCr & Err.Description, 2
Err.Clear
Else
debugLog "Success mapping drive: " & strLocalName & " to " & strRemotePath, 1
End If
   End If
Next

Function debugLog(strLog, level)
If bDebugLog = True Or level > 1 Then
oShell.LogEvent 4, strLog
End If
End Function

No comments:

Post a Comment