Kasper Katzmann
asked on
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.
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?
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
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
I run CMD as Administrator and executes the script. Shouldn't it be enough?
/Kasper
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.
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.
ASKER
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
And WScript.Echo "Setting printer as default: \\ServerName\Kopi1sal" shows up nicely.
Still no changes
/Kasper
ASKER
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
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
>> 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.
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.
ASKER
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.
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.