Jump to content

Recommended Posts

Posted (edited)

I am so lost with this, never done vbs scripts before but i'm looking to create a script that looks at the computers OU (in this case "upper" or "lower") and then sets the defaulkt printer.

 

I have a batch file that actually adds the printer connections all i was hoping to do was add in a .vbs to the end of the batch script that ran after the printers had been added to then set the default printer based on location HELP!!!

 

This is what i have so far and isn't working but hopefully you guys should be able to tell me if i'm a n idiot and completely missing something

 

Set objADInfo = CreateObject("ADSystemInfo")
strUserOU = objADInfo.UserName
strComputerOU = objADInfo.ComputerName

If InStr(1, strComputerOU,"OU=upper OU") > 0 Then
objNetwork.SetDefaultPrinter("\\*servername*\topcorri")
End If

 

edit: the batch file that sets printers also sets mapped network drives etc. and is a USER logon script and i want ot set default printer by COMPUER

 

edit2: i'm not sure if it even has to be a vbs file can you determine the computers OU and set default printer using just the batch file?

Edited by Pyroman
Posted

Below is (a cut down version of)our solution - its run from a vbs file attached to login scripts area in the GPO - seems to work OK

 

 

 

' Get the Computer Name

 

sCompPath = oAdSysInfo.ComputerName

 

 

' Set the printer dependent on the OU name for the PC

 

if InStr (1, sCompPath, "OU=ICT", 1) then

WShNetwork.AddWindowsPrinterConnection "\\server9\IT_1300n"

WShNetwork.SetDefaultPrinter "\\server9\IT_1300n"

elseif InStr(1, SCompPath, "OU=B29", 1) then

WShNetwork.AddWindowsPrinterConnection "\\server9\B29_3300"

WShNetwork.SetDefaultPrinter "\\server9\B29_3300"

 

*lots more elseif's here - I'm sure theres a more elegant way - but this works...

 

else

WScript.Echo "A default printer has not been assigned. Please contact IT so that the problem can be resolved"

end if

  • Thanks 1
  • 2 months later...
Posted

Hi

 

Trying to use Spuffmonkey's script for our network (vbs newb). This is what I've got so foar:

 

' Get the Computer Name

 

sCompPath = oAdSysInfo.ComputerName

 

 

' Set the printer dependent on the OU name for the PC

 

if InStr (1, sCompPath, "OU=R19", 1) then

WShNetwork.AddWindowsPrinterConnection "\\servername\R19-Printer"

WShNetwork.SetDefaultPrinter "\\servername\R19-Printer"

elseif InStr(1, SCompPath, "OU=R20", 1) then

WShNetwork.AddWindowsPrinterConnection "\\servername\R20-printer"

WShNetwork.SetDefaultPrinter "\\servername\R20-printer"

 

else

WScript.Echo "A default printer has not been assigned. Please contact IT so that the problem can be

resolved"

end if

 

I get the attached error:

 

Now, I think its me being thick as the computer is in the OU called R19, but the computer does not have a name that begins with R19 - is that the problem?

scripterror.JPG

Posted
Hi

 

Trying to use Spuffmonkey's script for our network (vbs newb). This is what I've got so foar:

 

' Get the Computer Name

Set objADInfo = CreateObject("ADSystemInfo")

 

sCompPath = oAdSysInfo.ComputerName

 

 

' Set the printer dependent on the OU name for the PC

 

if InStr (1, sCompPath, "OU=R19", 1) then

WShNetwork.AddWindowsPrinterConnection "\\servername\R19-Printer"

WShNetwork.SetDefaultPrinter "\\servername\R19-Printer"

elseif InStr(1, SCompPath, "OU=R20", 1) then

WShNetwork.AddWindowsPrinterConnection "\\servername\R20-printer"

WShNetwork.SetDefaultPrinter "\\servername\R20-printer"

 

else

WScript.Echo "A default printer has not been assigned. Please contact IT so that the problem can be

resolved"

end if

 

I get the attached error:

 

Now, I think its me being thick as the computer is in the OU called R19, but the computer does not have a name that begins with R19 - is that the problem?

 

 

You need to set the variable and create the object ie

 

 

Set objADInfo = CreateObject("ADSystemInfo")

 

Then you can use the properties of the variable so in this case

 

sCompPath = oAdSysInfo.ComputerName

 

oAdSysInfo notice that this is what you Set and you are accessing a property from the oAdSysInfo which is ComputerName ?

Posted

*lots more elseif's here - I'm sure theres a more elegant way - but this works...

 

 

elegance needs the case statement and a little bit of tweaking :-)

 

sCompPath = oAdSysInfo.ComputerName
sBits=split(sCompPath,",") 'split up the name at the commas and store in an array
sOU=lcase(sBits(1)) 'take the second bit of the name (item 0 is the computername)

select case sOU
 case sOU="ou=r19"
   sPrinter="\\server\printerr19"
 case sOU="ou=r20"
   sPrinter="\\server\printerr20"
 case else
   sPrinter="\\server\mainlibrary"
end select

WShNetwork.AddWindowsPrinterConnection sPrinter
WShNetwork.SetDefaultPrinter sPrinter

 

 

Note that this also allows you to have a default if there's nothing specific ("mainlibrary" at the end)

Posted
elegance needs the case statement and a little bit of tweaking :-)

 

sCompPath = oAdSysInfo.ComputerName
sBits=split(sCompPath,",") 'split up the name at the commas and store in an array
sOU=lcase(sBits(1)) 'take the second bit of the name (item 0 is the computername)

select case sOU
 case sOU="ou=r19"
   sPrinter="\\server\printerr19"
 case sOU="ou=r20"
   sPrinter="\\server\printerr20"
 case else
   sPrinter="\\server\mainlibrary"
end select

WShNetwork.AddWindowsPrinterConnection sPrinter
WShNetwork.SetDefaultPrinter sPrinter

Note that this also allows you to have a default if there's nothing specific ("mainlibrary" at the end)

 

I did not see that comment otherwise I would of replied the same lol

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...