Unfortunity, I have aready added a section to check if drive exist before disconnecting and now appears to be working fine. The issue was with the referenced the referenced thread saying that checking was over kill. Here it is:
Option Explicit
On Error Resume Next
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes, strRegMarkerValue, strRegKey
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN, objShell
Dim WshShell, strUserID , strDriveLetter, strHomeDir, objNetwork, objFSO
Set WshShell = CreateObject("WScript.Shell")
strUserID = WshShell.ExpandEnvironmentStrings("%USERNAME%")
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
if Err.Number <> 0 then
' An exception occurred
wscript.echo "Exception:" & vbCrLf &_
" Error number: " & Err.Number & vbCrLf &_
" Error description: '" & Err.Description & vbCrLf &_
" Domain: " & objRootDSE & vbCrLf &_
" Exiting Script"
wscript.quit
end if
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on user objects.
strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & struserid & "))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,cn,distinguishedName,UserAccountControl,l,mail,Department,homeDrive," _
& "homeDirectory,telephoneNumber,Title,employeeid"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Retrieve User values for Home Directory.
strName = adoRecordset.Fields("sAMAccountName").Value
strCN = adoRecordset.Fields("cn").value
strDriveLetter = adoRecordset.Fields("homeDrive").value
strHomeDir = adoRecordset.Fields("homeDirectory").value
if IsNull(strDriveLetter) or IsNull(strHomeDir) then
wscript.echo "Home Share is not setup in Active Directory. Please " & vbCrLf & _
"contact your Administrator or Call the Helpdesk x1572"
else
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
strRegKey = "HKLM\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters"
' check for marker
strRegMarkerValue = ""
' init value
strRegMarkerValue = objShell.RegRead( strRegKey & "\autodisconnect")
On Error Goto 0
If strRegMarkerValue <> "ffffffff" Then
objShell.RegWrite strRegKey & "\autodisconnect", "ffffffff"
End If
' Section to verify drive exists before disconnecting fixed the issue
If (objFSO.DriveExists(strDriveLetter) = True) Then
objNetwork.RemoveNetworkDrive strDriveLetter,true,true
end if
wscript.sleep 1000
if Err.Number <> 0 then
' An exception occurred
wscript.echo "Exception Disconnecting Drive:" & vbCrLf &_
" Error number: " & Err.Number & vbCrLf &_
" Error description: '" & Err.Description & vbCrLf &_
" Domain: " & objRootDSE & vbCrLf &_
" Exiting Script"
wscript.quit
end if
objNetwork.MapNetworkDrive strDriveLetter, strHomeDir
if Err.Number <> 0 then
' An exception occurred
wscript.echo "Exception:" & vbCrLf &_
" Error number: " & Err.Number & vbCrLf &_
" Error description: '" & Err.Description & vbCrLf &_
" Domain: " & objRootDSE & vbCrLf &_
" Exiting Script"
wscript.quit
end if
end if
' Clean up.
adoRecordset.Close
adoConnection.Close