Jump to content

Was this script helpful?  

23 members have voted

  1. 1. Was this script helpful?



Recommended Posts

Posted

In your first for loop instead of checking for the LY1-2250 and sr-4650 why not do a for loop that checks for any that aren't the cutePDF printer(s) and delete whats left over.

 

("Select * from Win32_Printer where DeviceID <> 'cutePDF'")

 

For Each objPrinter in colInstalledPrinters

objPrinter.Delete_

 

next

 

That way if its not cutePDF then it deletes everything else. BTW I forgot to mention that you probably wont need the 2nd for.... loop as your first one if modified should delete all the left over ones :)

Posted

Having had to try and figure this out the hard way (yes I know I should know but I didn't)...

 

... if you want to set the default printer for a particular room/etc.. and that printer is local then you need to find out how it's connected (eg: USB002) and then set it like this:

 

WshNetwork.SetDefaultPrinter "USB002"

 

Simple isn't it... :oops:

Posted
Yes, the information you need is the port name.

.. *gritted teeth*.. and where were you an hour ago when I needed ya.. :roll:

 

Ah well.. learned that one.

  • 1 month later...
Posted

Can some just help me to add into this script a variable for a specific user, ie, if joe bloggs wanted a certain printer? and fred smith got another printer?

 

Thanks

 

T

 

' Script to add network printers to workstations and thin clients WHEN THEY ARE PART OF A SPECIFIC GROUP IN AD

' Declare variables and enumerate existing printer connections

' Setting the parameters up

On error resume next

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set wshnetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.Username)
Set oPrinters = WshNetwork.EnumPrinterConnections
dim Counter, PrinterPath
dim strConnectString, strResult
strGroups = LCase(CurrentUser.MemberOf)
strGroups = LCase(Join(CurrentUser.MemberOf))



' Sets the groups up with the member of settings in AD

	Const Maths	= "cn=mathematics"
	Const ITstaff	= "cn=information technology"
	Const ITAdmin	= "cn=admin"
	Const DesTech	= "cn=design and technology"
	Const Careers	= "cn=careers"
	Const Enterprise = "cn=enterprise"


Dim computerName
computerName = LCase(WshNetwork.ComputerName)


'WScript.Echo "computername variable: " & (computerName)


'Deletes All Network Printers... Not manually installed
For i = 0 to oPrinters.Count - 1 step 2
		On Error Resume Next
							
		Wshnetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
	
NEXT



' Adding Printers

' ITstaff Group
if Instr(strGroups,ITstaff) then

wshnetwork.AddwindowsPrinterConnection "\\hades\office-ml2250"
	
	End If 'ITstaff


' Maths Group
If Instr(strGroups,Maths) then

wshnetwork.AddwindowsPrinterConnection "\\hades\MATHS-1320"
wshnetwork.AddwindowsPrinterConnection "\\hades\MATHS-6980"
	wshnetwork.setdefaultprinter "\\hades\maths-1320"

	End If 'maths


' ITAdmin Group
If Instr(strGroups,ITadmin) then

wshnetwork.AddwindowsPrinterConnection "\\hades\SR-4650"
	
	End If 'ITadmin


' DesTech Group
If Instr(strGroups,DesTech) then

wshnetwork.AddwindowsPrinterConnection "\\hades\r7-2600n"
	
	End If 'DesTech


' Careers Group
If Instr(strGroups,Careers) then

wshnetwork.AddwindowsPrinterConnection "\\hades\enterprise-2600n"
wshnetwork.AddwindowsPrinterConnection "\\careers-01\CareersPrinter"
	wshnetwork.setdefaultprinter "\\careers-01\CareersPrinter"

	End If 'Careers

' enterprise group
If Instr(strGroups,Enterprise) then

wshnetwork.AddwindowsPrinterConnection "\\hades\r7-2600n"

	End If 'Enterprise


' Adding Printers depentant on computer name

Select case (Left(computername,3))

Case "wr-"
	wshnetwork.addwindowsprinterconnection "\\hades\wr-2200"
		wshnetwork.setdefaultprinter "\\hades\wr-2200"

End Select



Select Case (computerName)

Case "lft_6169-at"
	
	wshnetwork.addwindowsprinterconnection "\\hades\LY1-2200"
	wshnetwork.setdefaultprinter "\\hades\LY1-2200"

End Select
Select Case (computerName)

Case "lft_6169-at"
	
	wshnetwork.addwindowsprinterconnection "\\hades\LY1-2200"
	wshnetwork.setdefaultprinter "\\hades\LY1-2200"

End Select

Select Case (computerName)

Case "hoy7"
	
	wshnetwork.addwindowsprinterconnection "\\hades\wr-2200"
	wshnetwork.setdefaultprinter "\\hades\wr-2200"

End Select

Posted

If you use struser then you could do

 

Select Case struser

 

Case "joe.bloggs"

wshnetwork.AddwindowsPrinterConnection

Case "John.Smith"

wshnetwork.AddwindowsPrinterConnection

Case Else

' Code here to add printer connection if its anyone else or if you want to

' do anything else afterwards that does not match any of the above users 'in the above case select statements.

End Select

 

If you use Uname then just change the first line of code from

 

Select Case struser

 

to

 

Select Case Uname

 

Easier then all the if else statements and easier to read etc.

Posted

If strUser returns

 

domain\username

 

Then you can use the split function before you do the select case to attain the username from it.

 

Dim x

 

x = split(struser,"\")

 

Select Case x(1)

 

Case "username_one"

wshnetwork.AddwindowsPrinterConnection

 

Case "username_two"

wshnetwork.AddwindowsPrinterConnection

 

End Select

 

'------------------

 

Just thought that would be handy.

Posted
got it working... needed to change struser = wshnetwork.domain to struser = wshnetwork.username

 

 

Sorry timbo343, I think that was my mistake! :oops:

  • 4 weeks later...
Posted

Greetings! I, too, am attempting to use a .vbs script to assign specific printers to specific computers in specific rooms. I've been reading this, and other threads, on the subject matter, seeing some of the same scripting concept that I'm attempt to use.

Here is my dilemma-I have named each of my clients with the following structure: RM400-1-PRN1 with the "PRN1" to be used to indicate, in the scripting, the specific printer. In my script, I use the " = right(xxxxx, 3)" but it does not seem to be reading the "PRNx" and mapping the specified printer. Below is the script that I'm trying to use:

 

Example 1:

 

Dim wshNetwork

Dim strUsername

Dim intCnt

Dim CurrentUser

Dim strCompName

Dim strShortName

Dim PrinterPath

Set wshNetwork = CreateObject("WScript.Network")

 

On Error Resume Next

 

wshNetwork.RemoveNetworkDrive "h:"

wshNetwork.RemoveNetworkDrive "P:"

 

wshNetwork.MapNetworkDrive "h:", "\\xxxx\" & wshNetwork.UserName & "$"

 

strCompName = wshNetwork.ComputerName

 

strShortName = RIGHT(strCompName,4)

Select Case strShortName

 

Case "PRN1"

PrinterPath = "\\xxxx\LAB1(KOELLER)"

wshNetwork.AddWindowsPrinterConnection PrinterPath

wshNetwork.SetDefaultPrinter "\\xxxx\LAB1(KOELLER)"

 

Case "PRN3"

Set objWSHNetwork = CreateObject("WScript.Network") 'create network object

strConnectString = "\\xxxx\lab2 (open)"

strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

wshNetwork.SetDefaultPrinter "\\xxxx\lab2 (open)"

 

End Select

 

WScript.Quit

 

or

 

Example 2:

 

Dim wshNetwork

Dim strUsername

Dim intCnt

Dim CurrentUser

Dim strCompName

Dim strShortName

Dim PrinterPath

Set wshNetwork = CreateObject("WScript.Network")

 

On Error Resume Next

 

wshNetwork.RemoveNetworkDrive "h:"

wshNetwork.RemoveNetworkDrive "P:"

 

wshNetwork.MapNetworkDrive "h:", "\\xxxx\" & wshNetwork.UserName & "$"

 

 

strCompName = wshNetwork.ComputerName

strShortName = RIGHT(strCompName,4)

Select Case strShortName

 

Case "PRN3"

PrinterPath = "\\xxxx\MediaCtr3500"

wshNetwork.AddWindowsPrinterConnection PrinterPath

wshNetwork.SetDefaultPrinter "\\xxxx\MediaCtr3500"

 

End Select

 

WScript.Quit

 

("xxxx" is sever name)

 

If anyone has any suggestions and/or knows what needs to be fixed to correct the problem, it would be greatly appreciated!

 

Behuck

Posted

im not sure if its supposed to be case specific but the problem i had was that in the script the computer name had to be lower case otherwise i didn't pick it up

 

ie instead of

Case "PRN1"

 

try

case "prn1"

Posted
yes, Ric's script will lcase() the PC names prior to doing the select....case. So you need to make sure all your case "blah" are lowercase.
Posted

We had a similar problem with vbscripts. I would say test

 

Case "prn1" instead of

Case "PRN1"

This should help you however I would also try testing the script to make sure it is getting to that line. A simple test by replacing declared variables with test code.

For example

 

strShortName = "prn1"

try this in places where variables are passed to identify if there is a error in your code. But looks fine to me.

Posted

Hi

 

Great script Ric, I have one little problem which am sure you can help with, I have setup using 3 letters to inspect for our computer rooms etc.

 

This has worked fine expect for our Science laptops which are named S-01, S-02 to S-30.

 

If I took the letter inspect down to 2, this would clash with RM2 and RM15.

 

Any ideas? I know I could write them out individually but just to be lazy and smarter.

 

Thanks.

 

Ste.

Posted

@mullet_man: Not sure what you are after... can you say what you want to achieve, e.g.

 

S-01 to S-30 -> \\server\printer1

RM2-01 to RM2-30 -> \\server\printer2

 

or whatever?

 

If it is as above, you can use a split on the computerName variable in the script and then you can inspect the bit before the -

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...