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!

VBScript Default Network Printer Based on G.Thomas's Script 1

Status
Not open for further replies.

PMLee

Technical User
Jan 18, 2008
3
US
Code:
On Error Resume Next 
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2\") 
														
Set colItems = objWMIService.ExecQuery _
	("Select * From Win32_UserAccount Where LocalAccount = False") 'Searches all non-local clients

For Each objItem in colItems 
If objItem.Domain <> "printdeny" Then 
	Dim objNetwork 'Declaration of the network
	Set objNetwork = CreateObject("WScript.Network") 
	objNetwork.RemovePrinterConnection "\\oaktonhs03\library1", True, True 
	objNetwork.AddWindowsPrinterConnection "\\oaktonhs03\library1" 
	Dim objPrinter 'Declaration of new variable
	Set objPrinter = CreateObject("WScript.Network") 'Makes Object of current network
	objPrinter.SetDefaultPrinter "\\oaktonhs03\library1" 
	WScript.Echo("Success!")
End if

If objItem.Domain = "printdeny" then
	WScript.Echo("You do not have permissions!")
End if
Next
WScript.Quit


My purpose here is to check if the current domain account logged in to the computer is part of the group "printdeny" and if it is, then it shows a message that the user does not have permissions. Any user that is not prat of that group will have G.Thomas's script run and display success.

I don't understand a few parts of this script, although I wrote it, it was mainly based on research and not pure knowledge.
First off, I could not figure out why people online made the strComputer = "."
Is the dot a wildcard or what?
Also, is the script up there correct to connect to the network and check the domain of the user?
At the moment, I am getting both of the messages come up, Success and No Permissions on any computer, no matter what.

PLEASE HELP!!
 
strComputer = "." means current computer.
Comment out the On Error Resume Next instruction to discover why you're getting both of the messages come up,

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you.

It states that the error is in the 7th line, the part where it says:

Code:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2\")
 
I made a couple of changes here:
Code:
strComputer = "." 'Current Computer

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_UserAccount Where LocalAccount = False")

If isEmpty(colItems) Then
	WScript.Echo("This account is local")

Else
	For Each objItem in colItems

		If objItem.Domain <> "3050printdeny" Then 'If account is not in group "3050printdeny"

			Dim objNetwork 'Declaration of the network
			Set objNetwork = CreateObject("WScript.Network") 'Makes Object of current network
			objNetwork.RemovePrinterConnection "\\oaktonhs03\library1", True, True 'Removes printer, even in use, permanently
	
			objNetwork.AddWindowsPrinterConnection "\\oaktonhs03\library1" 'Adds new Printer to network

			Dim objPrinter 'Declaration of new variable
			Set objPrinter = CreateObject("WScript.Network") 'Makes Object of current network
			objPrinter.SetDefaultPrinter "\\oaktonhs03\library1" 'Sets printer to library printer
	
			WScript.Echo("Success!")

		Else
			WScript.Echo("You do not have permissions!")
		End if
	Next
End if
WScript.Quit

This checks if the account is not local by checking if the colItems is empty. However, nothing is showing up. Can anyone answer the question as to why?
 
[1] A better starting point would be to learn from this more friendly article.

[2] You just replace the step 2 by some functional lines targetted at setting printer connection as you have drafted soem in that direction. And if necessary, extend the group memberof checking by some recursion for nested group possibility.

[3] The scripts you've shown are fundamentally mis-conceived and incorrect in both syntax and semantic.
 
PMLee,

You might want to take a look at my login script FAQ faq329-5798 which makes it easy to check if a user is a member of a particular group. The code is well documented and will hopefully make it easier for you to understand.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top