Set default print via VBS in Windows 7

I have, until now, been running a script on all desktops in our domain, each time a user login. The script (.vbs) is executed through a .bat file (our login script).

The script sets the default printer on a pc depending on the Location attribute in our Active Directory.

 
Const ForWriting = 2 
Dim fso, f 
Set fso = CreateObject("Scripting.FileSystemObject") 


Const ADS_SCOPE_SUBTREE = 2 

Set objShell = CreateObject("WScript.Shell")
Set objPrinter = CreateObject("WScript.Network") 
Set objConnection = CreateObject("ADODB.Connection") 
Set objCommand =   CreateObject("ADODB.Command") 
objConnection.Provider = "ADsDSOObject" 
objConnection.Open "Active Directory Provider" 
strComputername = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

Set objCOmmand.ActiveConnection = objConnection 
objCommand.CommandText = "Select Name, Location from 'LDAP://DC=EVA,DC=DK' Where objectClass='computer' AND Name = '" & strComputername & "'" 

objCommand.Properties("Page Size") = 1000 
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute 
objRecordSet.MoveFirst 



'set default printer depending on Location value in AD:

if objRecordSet.Fields("location").Value = "" then wScript.Quit
     
if objRecordSet.Fields("location").Value =  "1NORD" then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi1sal"
end if

if objRecordSet.Fields("location").Value = "1MIDT" then
     objPrinter.SetDefaultPrinter "\\ServerName\Arkiv1sal"
end if
     
if objRecordSet.Fields("location").Value = "1SYD" then
     objPrinter.SetDefaultPrinter "\\ServerName\Classensgade"
end if

if objRecordSet.Fields("location").Value = "2. sal Bibliotek" then
     objPrinter.SetDefaultPrinter "\\ServerName\Bibliotek"
end if
     
if objRecordSet.Fields("location").Value = "3NORD" then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi3sal"
end if

if objRecordSet.Fields("location").Value = "3MIDT" then
     objPrinter.SetDefaultPrinter "\\ServerName\receptionsprinter"
end if

if objRecordSet.Fields("location").Value = "2SYD" then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi2Sal"
end if

Open in new window


Until now it has been working as it should... on Windows XP. But when I try to execute the script in Windows 7, nothing happens.

Anyone who know how to convert the script to Windows 7?
Kasper KatzmannSeniorkonsulentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, first off, let me offer a cleaner version of your code:
Const ForWriting = 2 
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject("LDAP://" & objADSysInfo.ComputerName)
Set objPrinter = CreateObject("WScript.Network")
strLocation = objComputer.Location

'set default printer depending on Location value in AD:

If strLocation = "" Then WScript.Quit
     
If strLocation =  "1NORD" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi1sal"
ElseIf objRecordSet.Fields("location").Value = "1MIDT" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Arkiv1sal"
ElseIf objRecordSet.Fields("location").Value = "1SYD" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Classensgade"
ElseIf objRecordSet.Fields("location").Value = "2. sal Bibliotek" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Bibliotek"
ElseIf objRecordSet.Fields("location").Value = "3NORD" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi3sal"
ElseIf objRecordSet.Fields("location").Value = "3MIDT" Then
     objPrinter.SetDefaultPrinter "\\ServerName\receptionsprinter"
ElseIf objRecordSet.Fields("location").Value = "2SYD" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi2Sal"
End If

Open in new window


Secondly, there's no reason this wouldn't work, provided that the printer is actually already mapped.  It relies on the user to have the printer connected before being able to set it as the default.

Regards,

Rob.
RobSampsonCommented:
Oops, forgot to change the other locations....

Const ForWriting = 2 
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject("LDAP://" & objADSysInfo.ComputerName)
Set objPrinter = CreateObject("WScript.Network")
strLocation = objComputer.Location

'set default printer depending on Location value in AD:

If strLocation = "" Then WScript.Quit
     
If strLocation =  "1NORD" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi1sal"
ElseIf strLocation = "1MIDT" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Arkiv1sal"
ElseIf strLocation = "1SYD" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Classensgade"
ElseIf strLocation = "2. sal Bibliotek" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Bibliotek"
ElseIf strLocation = "3NORD" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi3sal"
ElseIf strLocation = "3MIDT" Then
     objPrinter.SetDefaultPrinter "\\ServerName\receptionsprinter"
ElseIf strLocation = "2SYD" Then
     objPrinter.SetDefaultPrinter "\\ServerName\Kopi2Sal"
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kasper KatzmannSeniorkonsulentAuthor Commented:
Absolutely nothing happens when I run the script as Administrator. No error messages and nothing changes in whitch printer is the default printer.

I run CMD as Administrator and executes the script. Shouldn't it be enough?

/Kasper
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

RobSampsonCommented:
It certainly should.  Let's see what your location is for your computer.  Above this line:
If strLocation = "" Then WScript.Quit

add this
WScript.Echo "Your computers location: " & strLocation

Note that because you haven't checked the case of your string matches (as in uppercase or lowercase) the location must match exactly.

If that does match, add something like
WScript.Echo "Setting printer as default: \\ServerName\Kopi1sal"

above the corresponding the SetDefaultPrinter line, and see what output you get.

Regards,

Rob.
Kasper KatzmannSeniorkonsulentAuthor Commented:
WScript.Echo "Your computers location: " & strLocation gives me 3NORD

And WScript.Echo "Setting printer as default: \\ServerName\Kopi1sal" shows up nicely.

Still no changes


/Kasper

Kasper KatzmannSeniorkonsulentAuthor Commented:
Ahhh it works.

I ran the script as administrator in a CMD box, but I waited to long, which caused it to loose its administrator credentials.

Thanks for pointing the direction

/Kasper
RobSampsonCommented:
>> but I waited to long, which caused it to loose its administrator credentials

Really?  I've never seen that....so if you right click cmd.exe and click Run As Administrator, then leave it for a while, it loses admin rights?  I'll have to look out for that....

Thanks for the grade.

Regards,

Rob.
Kasper KatzmannSeniorkonsulentAuthor Commented:
Our security guy told me that it its kerberos that handles the credentials and that it has a timeout depending on how the system is set. He couldn't remember the standard timeout though.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.