?
Solved

Set default print via VBS in Windows 7

Posted on 2011-10-06
8
Medium Priority
?
567 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:Kasper Katzmann
  • 4
  • 4
8 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 36927653
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 36927659
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
 

Author Comment

by:Kasper Katzmann
ID: 36929135
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Expert Comment

by:RobSampson
ID: 36929643
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
 

Author Comment

by:Kasper Katzmann
ID: 36929910
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
 

Author Closing Comment

by:Kasper Katzmann
ID: 36930453
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 36934393
>> 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
 

Author Comment

by:Kasper Katzmann
ID: 36934506
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question