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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error with Logon Script (Object Required: "WshShell") 1

Status
Not open for further replies.

SalemGrafix

IS-IT--Management
Jun 12, 2003
46
US
Hi all, was wondering if someone here might be able to help with this script. I'm recieving the error:

Object Required: "WshShell"

In the logon script. If I run the script from my machine, it works without a problem (already logged into the network). When I implement it as a script for a user to logon, that's when I recieve the error. Originally, I was recieving the error when I was using WshShell.PopUp prior to Set WshShell = WScript.CreateObject("WScript.Shell"), but now I have the Set WshShell line setup prior to the popup. Any help is greatly appreciated:

Code:
' ########################################################################
' Written in VBScript.
' Paul DeBrino .:. [URL unfurl="true"]www.infinity-rd.com[/URL] .:. March 2004.
' Establishes map drives.
' Assigned to OU Group Policy as User Logon Script.
'
' This script will: 
' (1) check if the drive is already connected and, if so, disconnect it.
' (2) map the drive.
'
' Command parameters are as follows: 
' MAPIT DRIVE-LETTER as string, PATH as string, USER as string, PASSWORD as string
' (1) Do not specify colon in drive letter.
' (2) Do not end path with a forward slash.
' (3) If user and password are not required to establish map, then specify null as follows: vbNull
'
' Reference Microsoft info at:
' [URL unfurl="true"]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthmapnetworkdrive.asp[/URL]
' ########################################################################
' Modified for SCC by Eddie D. Shelton Jr. December 2004.
' MAPIT DRIVE-LETTER now does not try to log in using a username/password if vbNull is specified
' ########################################################################

' ====================================
' DEFINE WHO TO CONTACT for pop-up messages:
' ====================================
strContactMessage = "If you require assistance, please contact IT Support."

' ==================
' DISPLAY MESSAGE THAT IT'S STARTED
' ==================
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.PopUp "  *** SCC Logon Script   " & vbcrlf  & "  ***  Mapping Drives    "

' ==================
' DEFINE DRIVES TO MAP:
' ==================
Mapit "M", "\\sccsrv-app02\apps", vbNull, vbNull
Mapit "U", "\\sccsrv-pdc02\divisional documents$", vbNull, vbNull

' ==================
' DISPLAY MESSAGE THAT IT'S DONE
' ==================
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.PopUp "  *** SCC Logon Script   " & vbcrlf & "  *** Script Complete!   "

' ##################################
' DO NOT MODIFY ANYTHING BELOW THIS POINT...
' unless you are familiar with the proper settings.
' ##################################
Sub Mapit(strLetter, strPath, strUser, strPass)

' Slight pause to ensure each pass has time to commit:
wscript.sleep 200

' Pop-up Notices (set to False to disable notices, otherwise set to True):
bPopReminder = True

' Define remembered map drive error number:
intErrRemembered = -2147023694

' Define whether the map information should be removed from the current user's profile:
bForceRemoveFromProfile = True
bRemoveFromProfile = True

' Define whether the map information should be stored in the current user's profile:
bStoreInProfile = False

' Create the Shell or environment for the commands:
Set WshShell = WScript.CreateObject("WScript.Shell")

' Define objects:
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives()

' Define the DriveLetter:
DriveLetter = strLetter & ":"

' Define the remote path:
RemotePath = strPath

' Check if already connected:
AlreadyConnected = False
For i = 0 To oDrives.Count - 1 Step 2
If LCase(oDrives.Item(i)) = LCase(DriveLetter) Then AlreadyConnected = True
Next

' Attempt to map the drive. If already mapped, first attempt disconnect:
If AlreadyConnected = True then
WshNetwork.RemoveNetworkDrive DriveLetter, bForceRemoveFromProfile, bRemoveFromProfile
If strUser <> vbNull then
 WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile, strUser, strPass
else
 WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile
end if

If bPopReminder Then nomsg = ""
Else
On Error Resume Next

if strUser <> vbNull then
 WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile, strUser, strPass
else
 WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile
end if

If Err.Number <> 0 Then
If Err.Number = intErrRemembered Then
WshShell.PopUp "WARNING!! WARNING!! WARNING!! WARNING!!" & vbCrLf & vbCrLf _
& "Unable to map drive " & DriveLetter & " to " & RemotePath _
& " due to a previously defined remembered map with the same letter." _
& vbCrLf & vbCrLf & "Please MANUALLY disconnect map drive " & DriveLetter _
& ", then Log Out and Log back in." _
& vbCrLf & vbCrLf & strContactMessage
' Display the Disconnect Network Drives window:
Set objWSH = Wscript.CreateObject("WScript.Shell")
objWSH.Run "rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL Disconnect", 1, true
Else
WshShell.PopUp "WARNING!! WARNING!! WARNING!! WARNING!!" & vbCrLf & vbCrLf _
& Err.Description & " (error " & Err.Number & ")" & vbCrLf & vbCrLf _
& "Unable to map drive " & DriveLetter & " to " & RemotePath _
& " due to above reason." _
& vbCrLf & vbCrLf & strContactMessage
End If
Else

End If
End If

' Release resources:
Set objWSH = Nothing
Set WshShell = Nothing
Set WshNetwork = Nothing
Set oDrives = Nothing
End Sub

Again, thanks for any help, it's greatly appreciated.
 
What OS are the end user machines? Not all OSs support WSH...... or you have to at least install it on those first.... I can't remember specifics on which OSs though....maybe that's the issue?
 
i would recommend not re-creating the WshShell object.

Every time you run your MapIt sub it creates a WshShell object then destorys it.

So far you have 4 creation of WshShell objects in your logonscript. Creation of such objects takes time. Plus your creation and then destroying of WshShell objects and your re-use of the same variable name leads to confusion for the reader.

I would recommend creating a WshShell object at the top of your script globally, that way any sub or function or main body of your script can re-use that object.

The same applies to your WshNetowrk object, create it once in the body and reuse it every where.

On a more sublte note, every time you call your MapIt sub you get a list of the currently mapped drives. I would suggest you only need to do this once. I would recommend reading this information once and populating an array or dictionary object with it.
 
Windows XP is the OS.

As for the 4 references, will clean that up. Also, thanks for the suggestion on using the array, rather than calling the subroutine multiple times.

As for issues with the above, I'm running Windows XP as well, and after creating the script, had no issues running the script locally myself. At first I even thought it might be related to the fact that I have VB installed, but even one of the workstations that should be able to run the script I have installed a VB program on it (EXE and runtime libraries), so I would think that would be enough.

Thanks for the tips so far, but still no luck at this time with what the error might be. :)
 
i have copied your code and run it on my machine and it finishes without error
 
Yes, I'm able to run the script as well, but not as a logon script.

I thought about it some more last night, and I have removed the WshShell.Popup commands, and made them MsgBox instead, hoping this will work. Will followup once I find out how that works.
 
Okay, changing the WshShell.Popup's to MsgBox's instead, seem to be working. Thank you to everyone who responded to this post.

Also, thank you very much mrmovie for pointing me in the right direction with the creation of the objects.

Last thing I'll need to do with this script is test it on the rest of the machines in our network, then clean up the code (including removing the multiple calls to Mapit, thanks again mrmovie). Hopefully all goes well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top