Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Drive does not exist

Status
Not open for further replies.

SHampton

MIS
Feb 5, 2009
16
US
thread329-1442569
yes I know it is an extra level and possibly over kill, but if you don't check if drive exists and is mapped correctly the script errors and exits. I had my script working fine initially on some computers and then had to included the removenetworkdrive option because of errors, but now when the drive does not exist in some situations, the script exits even with the On Error Resume Next, unless I am placing it in the wrong place.
 
It would be nice to see the script you referring to.
Code:
Dim objFSO, strDrive, strShare
Set objFSO = CreateObject("Scripting.FileSystemObject")
      strDrive = "H:"
      strShare = "\\server\share"
If objFSO.DriveExists(strDrive) = True Then
   objNetwork.RemoveNetworkDrive strDrive
End If
objNetwork.MapNetworkDrive strDrive, strShare

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top