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

Setting timeouts for WSHNetwork?

Status
Not open for further replies.

Leozack

MIS
Oct 25, 2002
867
GB
Hi all - I was wondering if there is a way to set a timeout for adding printers / mapped drives in a vbs? The problem is the pcs with the printers aren't always on, and currently it sems the script never finishes. Is there a way to set the timeout? My logon script looks like this

Code:
Dim WSHShell

Set WSHShell = CreateObject("Wscript.Shell")
'WSHShell.Run ("\\marksbury-srv\NETLOGON\ie7searchlist.bat")
WSHShell.Run ("regedit /s \\marksbury-srv\NETLOGON\ie7searchlist.reg")


'==========================================================================
'
' NAME: LogonScript.vbs
' FROM: [URL unfurl="true"]http://www.tek-tips.com/faqs.cfm?fid=5798[/URL]
'
' AUTHOR:  Mark D. MacLachlan, The Spider's Parlor
' URL   : [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 4/10/2003
'
' COMMENT: Enumerates current users' group memberships in given domain.
'          Maps and disconnects drives and printers
'
'==========================================================================


ON ERROR RESUME NEXT

Dim WSHNetwork, objDomain, DomainString, UserString, UserObj, Path


Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

'Grab the user name
UserString = WSHNetwork.UserName
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'Grab the computer name
strComputer = WSHNetwork.ComputerName



'----------------------------------- Don't run anything below if this is the server -----------------------------------
If Not strComputer = "marksbury-srv" Then

'Synchronizes the time with our NTP Server
WSHShell.Run "NET TIME \\marksbury-srv /set /y"

'Disconnect any drive mappings as needed.
'WSHNetwork.RemoveNetworkDrive "F:", True, True

'Disconnect ALL mapped drives
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next 

'Give the PC time to do the disconnect, wait 300 milliseconds
wscript.sleep 300

'Map drives needed by all
'Note the first command uses the user name as a variable to map to a user share.
'WSHNetwork.MapNetworkDrive "H:", "\\server\users\" & UserString,True
WSHNetwork.MapNetworkDrive "P:", "\\marksbury-srv\Apps$",True
WSHNetwork.MapNetworkDrive "U:", "\\marksbury-srv\Students",True

'Now check for group memberships and map appropriate drives
For Each GroupObj In UserObj.Groups
    Select Case GroupObj.Name
    'Check for group memberships and take needed action
        Case "Domain Admins"
            WSHNetwork.MapNetworkDrive "I:", "\\marksbury-srv\ict$",True
            WSHNetwork.MapNetworkDrive "N:", "\\marksbury-srv\NETLOGON",True
            WSHNetwork.MapNetworkDrive "X:", "\\marksbury-srv\Setups$",True
            WSHNetwork.MapNetworkDrive "Y:", "\\marksbury-srv\profiles$",True
            WSHNetwork.MapNetworkDrive "Z:", "\\marksbury-srv\d$",True
        Case "SimsUserGroup"
            WSHNetwork.MapNetworkDrive "S:", "\\marksbury-srv\CP$",True
        Case "AdminUserGroup"
            WSHNetwork.MapNetworkDrive "O:", "\\marksbury-srv\OfficeDocs$",True
            'Below is an example of how to set the default printer
            WSHNetwork.SetDefaultPrinter "\\marksbury-srv\hpLJ1005"
        Case "TeacherUserGroup"
            WSHNetwork.MapNetworkDrive "S:", "\\marksbury-srv\TeachDocs$",True
    End Select
Next

'Remove ALL old printers
'Enumerate all printers first, after that you can select the printers you want by performing some string checks
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
      WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If
    'To remove all printers incuding LOCAL printers use this statement and comment out the If Statement above
    'WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
Next

'Remove a specific printer
'WSHNetwork.RemovePrinterConnection "\\ServerOld\HP5si",True,True
                                
'Install Printers
WSHNetwork.AddWindowsPrinterConnection "\\room1pc1\room1left"
WSHNetwork.AddWindowsPrinterConnection "\\room1pc3\room1right"
WSHNetwork.AddWindowsPrinterConnection "\\room2pc2\room2left"
WSHNetwork.AddWindowsPrinterConnection "\\room2pc3\room2right"
WSHNetwork.AddWindowsPrinterConnection "\\hut1\hutleft"
WSHNetwork.AddWindowsPrinterConnection "\\hut2\hutmiddle"
WSHNetwork.AddWindowsPrinterConnection "\\hut3\hutright"
WSHNetwork.AddWindowsPrinterConnection "\\grouproom\grouproom"
WSHNetwork.AddWindowsPrinterConnection "\\hallwhiteboard\hall"

'Set default printers based on user name (local room) then computer name (local printer)
Select Case UserString
   Case "YearR"
        WSHNetwork.SetDefaultPrinter "\\room2pc2\room2left"
   Case "Year1"
        WSHNetwork.SetDefaultPrinter "\\room2pc2\room2left"
   Case "Year2"
        WSHNetwork.SetDefaultPrinter "\\room1pc1\room1left"
   Case "Year3"
        WSHNetwork.SetDefaultPrinter "\\room1pc1\room1left"
   Case "Year4"
        WSHNetwork.SetDefaultPrinter "\\hut1\hutleft"
   Case "Year5"
        WSHNetwork.SetDefaultPrinter "\\hut1\hutleft"
   Case "Year6"
        WSHNetwork.SetDefaultPrinter "\\hut1\hutleft"
End Select

Select Case strComputer
   Case "room1pc1"
        WSHNetwork.SetDefaultPrinter "\\room1pc1\room1left"
   Case "room1pc3"
        WSHNetwork.SetDefaultPrinter "\\room1pc3\room1right"
   Case "room2pc2"
        WSHNetwork.SetDefaultPrinter "\\room2pc2\room2left"
   Case "room2pc3"
        WSHNetwork.SetDefaultPrinter "\\room2pc3\room2right"
   Case "hut1"
        WSHNetwork.SetDefaultPrinter "\\hut1\hutleft"
   Case "hut2"
        WSHNetwork.SetDefaultPrinter "\\hut2\hutmiddle"
   Case "hut3"
        WSHNetwork.SetDefaultPrinter "\\hut3\hutright"
   Case "grouproom"
        WSHNetwork.SetDefaultPrinter "\\grouproom\grouproom"
   Case "hall"
        WSHNetwork.SetDefaultPrinter "\\hallwhiteboard\hall"
   Case "hallwhiteboard"
        WSHNetwork.SetDefaultPrinter "\\hallwhiteboard\hall"
End Select


'Add On Code goes below this line
'=====================================


' This section of script will prevent the balloon window 
' that appears when printing to a network shared printer
' after XP Service Pack 2 is installed.
'=====================================

Path = "HKCU\Printers\Settings\EnableBalloonNotificationsRemote"
WshShell.RegWrite Path, 0 ,"REG_DWORD"


'=====================================
'Add On Code goes above this line

'----------------------------------- Don't run anything above if this is the server -----------------------------------
End If




'Clean Up Memory We Used
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing
  

'Quit the Script
wscript.quit

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
with mapping drives to shares i would recommend that you check if the FolderExists before trying to map it. perhaps the outcome will be little different from a mapdrive call but there you.
 
The mapped drives isn't a problem it's the printers. With con2prt or kixtart I haven't had issues of sitting and waiting when the pc the printer is on isn't available. Only when using this vbscript :/

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
OK, so why don't you first verify if the PC is reachable before attempting to map the printers?

Also, please note that it is the intention of this script to be used in an Enterprise. Printers should be connected to print servers or be accessible directly on the network via IP rather than being connected to a specific PC. I would suggest you investigate the use of a print server applicance to free your printers from the chains of PC bondage. I've purchased such devices on sale for around $50.

A variety of such devices within many price ranges can be found here:
I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
I'm aware the use of your script is targetted more at enterprises and yes I'm aware printesr should be on print servers - but when it comes to educational rooms people often want a printer on-site as it were, in their own room on their pc. And for some reason they insist on having more than 1 in a room sometimes which is why I can't just map to local printers.

But good suggestion about checking for pc response before mapping that pcs printer - how is the best way to do this in the vbs?

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
+you can check a default share FSO.FolderExists?
+you can use Win32_Ping class
+check stdout of cmd /c ping
 
Here is a little script I use for checking if a machine is alive or not
Code:
'==========================================================================
'
' NAME: CheckIsAlive.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYRIGHT (c) 2005 All Rights Reserved
' DATE  : 3/17/2006
'
' COMMENT: Verifies if a PC is reachable before taking other actions.
'          Creates a list of PCs still needing attention.
'==========================================================================
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTextStream = oFSO.OpenTextFile("wslist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close
For Each strComputer In RemotePC
	'test if the machine is available
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	If Err Then
		'add the name to our list and clear the error
		Deadmachines = Deadmachines & strComputer & vbCrLf
		Err.Clear
	Else
	  'Code to execute if machine is alive
	End If
Next
Set ts = objFSO.CreateTextFile ("C:\DeadMachines.txt", ForWriting)
ts.write Deadmachines
WScript.Echo Deadmachines
MsgBox "Done"

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
That's a great script there but as you know I'm just looking for something to insert in front of my printer connection lines to check the machines rather than building a text file up or something? I suppose if it was just a list held in memory I could check it each line. But vbscript really isn't my thing, I'm just trying use it for this particular fiddly situation, so if there's a simple adjustment I can make lmk, else I'll ponder/attempt stuff. Thanks as always

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Leozack, you might want to try calling microsoft and requesting the hotfix for KB923218 which is a fix for Windows 2003 SP1 to fix printer status being reported sooner.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Sounds interesting haven't heard about that one, though the machines hosting the printers are all xp in this case and connecting to eachother for access to the various printers. A ridiculous scenario for sure but I don't get to make the rules I just have to make them work >_<
I might revert back to kixtart or something for the printer part of the login then, though it can't handle the complexity I have in the vbscript

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Since you know the computer name you can of course use the scritp I posted above. The following part could be used to quickly decide if the computer itself is turned on.
Code:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    If Err Then
        'add the name to our list and clear the error
        Deadmachines = Deadmachines & strComputer & vbCrLf
        Err.Clear
    Else
      'Code to execute if machine is alive
    End If

Try the binding by replacing the strComputer string with the target PC name or IP.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Looks good mate if I was more awake I would've probably disected that earlier. I'll give it a whirl when I'm next there and lyk how it goes, cheers

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top