Scripts Thread, Set Default printer according to computers OU in Coding and Web Development; I am so lost with this, never done vbs scripts before but i'm looking to create a script that looks ...
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
Code:
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?
Last edited by Pyroman; 27th April 2009 at 10:23 AM.
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?
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 ?
*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 :-)
Code:
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)
elegance needs the case statement and a little bit of tweaking :-)
Code:
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