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

0
 
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.
0
 
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
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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

0
 
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
0
 
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.
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.