We help IT Professionals succeed at work.

How do I add a line to my login script that will connect the user to some network printers?

367 Views
Last Modified: 2012-05-08
We have a login script that automatically adds network drives for anyone logging into the domain. I'd like to add network printers as well. I'd also like to remove some old network printers. How do I edit this script to do that?

'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalSCRIPT(TM)
'
' NAME: CSC.vbs
'
' AUTHOR:
' DATE  : 7/27/2005
'
' COMMENT: CSC User Logon Script
'Global Variable Declaration & Object Initialization for the Script.
'**************************************************************************
On Error Resume Next
Dim oNetwork
Dim usrDomain
Dim oGrp
Dim oUsr
Dim oResult
Dim oServer
Dim oChoice
Dim FSO
Dim oBindusr
Dim oFullName
Dim oComma
Dim oLen
Dim oFirstName
Dim oLastName
Dim oTemp
Dim oLserver
Dim oLpath
Dim oWindir
Dim oNewPath
Dim oLastLogin
Dim DomainObj

Set oNetwork=Wscript.CreateObject("Wscript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")

'*****************************************************************************
'Find Users Full Name
'*****************************************************************************

 oUsr = UCase(oNetwork.UserName)
        Set oBindusr = GetObject("WinNT://CSC/" & oUsr & ",User")
        oFullname=oBindusr.Get("Fullname")
      oLen = Len(oFullname)
        oComma = InStr(1,oFullname, ",",0)
        oFirstName = Right(oFullname, Abs(oComma - oLen))
      oLastName = Left(oFullname, oComma - 1)


'*************************************************************************
'WScript.Echo "Mapping Common Drives......."
'*************************************************************************

' Remove any Persistent drive Mappings

oNetwork.RemoveNetworkDrive "G:",True
oNetwork.RemoveNetworkDrive "P:",True
oNetwork.RemoveNetworkDrive "U:",True
oNetwork.RemoveNetworkDrive "X:",True



' Common Drive Mapping
            oNetwork.MapNetworkDrive "G:", "\\CSCSQL01\BillMaster"
            oNetwork.MapNetworkDrive "P:", "\\CSCBU01\Public"
            oNetwork.MapNetworkDrive "U:", "\\CSCBU01\" & oUsr & "$"
            oNetwork.MapNetworkDrive "X:", "\\CSCBU01\Private$"
            



'**********************************************************************************************************
' Call Seb procedure to clean the Variables
'**********************************************************************************************************
Sub Clean

oNetwork=Nothing
WshShell=Nothing
FSO=Nothing
usrDomain=""
oGrp=""
oUsr=""
oResult=""
oServer=""
oChoice=""
oBindusr=""
oFullName=""
oComma=""
oLen=""
oFirstName=""
oLastName=""
oTemp=""
oLserver=""
oLpath=""
oWindir=""
oNewPath=""
folder=""
myFolder=""
RemoveFolder=""
File=""
Files=""
RemoveFile=""
End Sub



Is there a better way to do this through active directory?
Comment
Watch Question

Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\server\printer"

to add a printer

Commented:
You should take a look at this:

http://technet.microsoft.com/en-us/library/cc755424(WS.10).aspx

I highly recommend using this approach.  pusprinterconnections.exe is wonderful.

Author

Commented:
I love that idea wdurrett, but we dont have an R2 machine on our network. (Sad Day)

Author

Commented:
MikeLogsdon, I'll try what you gave me. Is there a line that we could use to remove old network printers on client machines through the script as well?
Try this

Full Username will pop up in a message box.  You can use the oUser.FullName however you want to
'FINDS USERS FULL NAME
Set oNetwork = CreateObject("WScript.Network")
sDomain = oNetwork.UserDomain
sUser = oNetwork.UserName
sADSPath= sDomain & "/" & sUser
Set oUser = GetObject("WinNT://" & sADSPath & ",user")
WScript.Echo oUser.FullName 'Full name
 
'DISCONNECT ALL EXISTING NETWORK DRIVES
Set AllDrives = oNetwork.EnumNetworkDrives()
For i = 0 To AllDrives.Count - 1 Step 2 
	oNetwork.RemoveNetworkDrive AllDrives(i) 
Next
 
'MAP COMMON NETWORK DRIVES
oNetwork.MapNetworkDrive "G:", "\\CSCSQL01\BillMaster"
oNetwork.MapNetworkDrive "P:", "\\CSCBU01\Public"
oNetwork.MapNetworkDrive "U:", "\\CSCBU01\" & oUsr & "$"
oNetwork.MapNetworkDrive "X:", "\\CSCBU01\Private$"
 
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For Loop_Counter = 0 To WSHPrinters.Count - 1 Step 2
	If Left(WSHPrinters.Item(Loop_counter + 1),2) = "\\" then
       WSHNetwork.RemovePrinterConnection WSHPrinters.Item(Loop_Counter +1),True,T
    End If
Next

Open in new window

Author

Commented:
Jeff,
I don't know how to read this (besides educated guesses). If I wanted to add \\server\printer using the login script, what would that script look like?

Author

Commented:
Jeff,
If I wanted to remove an old printer with your script, would I use a line like this?

If Left(WSHPrinters.Item(Loop_counter + 1),2) = "\\server\printer" then
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
No, the below line will not remove a specific printer.

If Left(WSHPrinters.Item(Loop_counter + 1),2) = "\\server\printer" then

This line does this:

WSHPrinters.Item(Loop_counter + 1)  is the name of the printer in the form of "\\server\printer"

The line If Left(WSHPrinters.Item(Loop_counter + 1),2) = "\\server\printer" then  looks at the FIRST TWO CHARACTERS of the returned printer name and sees if the 2 characters are "\\"  if it IS THESE TWO CHARACTERS, then it is a network printer and NOT a local printer.  So that line you asked about ONLY looks at the first two characters to see if it is a network printer or not.
Does that make sense?
My script will remove ALL network printers, whether old or not.  See the revised script to check for a specific network printer to delete.

The part that removes ALL network printers and SPECIFIC network printer are both in the script.  Just remove which part you don't want.
Jeff,
If I wanted to remove an old printer with your script, would I use a line like this?

If Left(WSHPrinters.Item(Loop_counter + 1),2) = "\\server\printer" then

I think I was unclear on this question of yours...
It would be



If WSHPrinters.Item(Loop_Counter + 1) = "\\server\printer" then
             WSHNetwork.RemovePrinterConnection WSHPrinters.Item(Loop_Counter +1),True,T
      end if

Author

Commented:
I tried this script, but it didn't work. I only needed the two network drives G and P btw, so I removed the other two.

 

'FINDS USERS FULL NAME
Set oNetwork = CreateObject("WScript.Network")
sDomain = oNetwork.UserDomain
sUser = oNetwork.UserName
sADSPath= sDomain & "/" & sUser
Set oUser = GetObject("WinNT://" & sADSPath & ",user")
WScript.Echo oUser.FullName 'Full name
 
'DISCONNECT ALL EXISTING NETWORK DRIVES
Set AllDrives = oNetwork.EnumNetworkDrives()
For i = 0 To AllDrives.Count - 1 Step 2 
	oNetwork.RemoveNetworkDrive AllDrives(i) 
Next
 
'MAP COMMON NETWORK DRIVES
oNetwork.MapNetworkDrive "G:", "\\CSCSQL01\BillMaster"
oNetwork.MapNetworkDrive "P:", "\\CSCBU01\Public"
 
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For Loop_Counter = 0 To WSHPrinters.Count - 1 Step 2
	
	'THIS REMOVES A SPECIFIC NETWORK PRINTER
	If WSHPrinters.Item(Loop_Counter + 1) = "\\cscbu01\Lexmarkms2" then
		 WSHNetwork.RemovePrinterConnection WSHPrinters.Item(Loop_Counter +1),True,T
	end if
Next
 
'ADD PRINTERS YOU WANTED 
oNetwork.AddWindowsPrinterConnection "\\cscdc01\Printer0"
oNetwork.AddWindowsPrinterConnection "\\cscdc01\Printer1"

Open in new window

Author

Commented:
I'm not sure if this matters, but the file that executes this vbs script is called test.cmd Here's the code:
@echo off
REM ======================================================================
REM
REM Batch File -- Created with SAPIEN Technologies PrimalSCRIPT(TM)
REM
REM NAME: 
REM
REM AUTHOR:   
REM DATE  : 05/27/2005
REM
REM COMMENT: Starts Logon Script		
REM
REM ======================================================================
Setlocal
if not "%OS%"=="Windows_NT" goto EXIT
 
Echo Login Script is Running, Please Wait...........
 
if not exist %windir%\system32\wscript.exe goto error
 
if defined CLIENTNAME goto MAIN
 
 
:MAIN
REM Echo "Running Main"
cscript.exe //nologo //B %0\..\test.vbs
 
:EXIT
Endlocal
Exit
 
:Error
Echo Windows Scripting Host does not exist. Call ACG for help.
Pause
EndLocal
exit

Open in new window

Author

Commented:
it looks like the drives are being mapped, but the printers aren't being removed or added.

Author

Commented:
It looks like some security settings were prohibiting the addition of the printers. I fixed that, so now it is adding the printers and the mapped drives. The only thing that isn't working now is the removal of the old printers.
You are only wanting to remove the one old printer?

\\cscbu01\Lexmarkms2

Correct?

In the script.  Look for this

'THIS REMOVES A SPECIFIC NETWORK PRINTER
      If WSHPrinters.Item(Loop_Counter + 1) = "\\cscbu01\Lexmarkms2" then
             WSHNetwork.RemovePrinterConnection WSHPrinters.Item(Loop_Counter +1),True,T
      end if

Make it:

'THIS REMOVES A SPECIFIC NETWORK PRINTER
msgbox WSHPrinters.Item(Loop_Counter + 1)      
If WSHPrinters.Item(Loop_Counter + 1) = "\\cscbu01\Lexmarkms2" then
             WSHNetwork.RemovePrinterConnection WSHPrinters.Item(Loop_Counter +1),True,T
      end if


With this change for every printer it finds it will pop a message box with the printers names as it sees them.  Watch each one pop up and see if the printer you want to remove shows up.  If it does make a note of EXACTLY how it shows up in the message box.  Then change the portion of the script in:

'THIS REMOVES A SPECIFIC NETWORK PRINTER
      If WSHPrinters.Item(Loop_Counter + 1) = "<EXACT PRINTER NAME FROM MSGBOX" then
             WSHNetwork.RemovePrinterConnection WSHPrinters.Item(Loop_Counter +1),True,T
      end if

Put it within the double-quotes.

For instance if the message box pops up and just says "Lexmark2"  then replace the

"\\cscbu01\Lexmarkms2"   with    "Lexmark2"

That is just an example.
Once it works, then remove or comment out the line

msgbox WSHPrinters.Item(Loop_Counter + 1)  

To comment out but leave it for future use when needed just put a single quote in front of it

msgbox WSHPrinters.Item(Loop_Counter + 1)       changes to

' msgbox WSHPrinters.Item(Loop_Counter + 1)      commented out with single quote
You see most times a NETWORK printer is mapped using 1 of 3 ways.

Connection to  \\servername\printername
Connection DIRECTLY to the printer using Standard TCP/IP Port
Custom port connection using custom software

The script looked for the pattern of "\\servername\printername"

If your printer you want to delete is NOT in this form of connection but instead uses TCP/IP Directly to Printer then it will not show up correctly.  That is why I said use the msgbox to find the exact name of the printer as the script sees it and use that instead.

Thus the "Lexmark2" instead of "\\servername\Lexmark2" .  Not sure how would be best unless you list what printers show up in the msgbox that "matches" which printer you want to delete.

Author

Commented:
Jeff, you are the man! Thanks for doing this for me. I really appreciate it. The script works now completely. It adds the network drives and network printers and it removes the old printers.

Thanks alot!
You are very welcome :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.