Using Ruby to change printer preferences in Win32

Our Windows XP sys admins recently changed things so that every day,
our networked printer preferences are reset using a script. Rather
than changing the preferences by hand every day so they suit my work
needs, I'd like to write a Ruby program I can run each morning. Any
pointers on where to get started? For example, is this something I
should
do via Win32OLE? If so, where do I find the right OLE calls to change
printer printing prefs?

Thanks,
Anders Schneiderman

If OLE then WMI is the most probable way. Have a look at WMI (Windows
Management Interface or something like that), if there's something
interesting. Other possibilities are through the registry or direct
WINAPI calls (Win32API).

If I understood correctly, you want top reverse the changes made by
your admins. If that is right, I assume the best place to look for
information is the admins' script itself. Chances are that it is a
.wsh or .vbs script, that is more or less directly translatable to
ruby, using Win32OLE. It's possible that it is an .exe file, and that
is a harder case (regmon from sysinternals might be helpful then).

···

On 12/8/06, Anders <anders.schneiderman@seiu.org> wrote:

Our Windows XP sys admins recently changed things so that every day,
our networked printer preferences are reset using a script. Rather
than changing the preferences by hand every day so they suit my work
needs, I'd like to write a Ruby program I can run each morning. Any
pointers on where to get started? For example, is this something I
should
do via Win32OLE? If so, where do I find the right OLE calls to change
printer printing prefs?

Anders wrote:

Our Windows XP sys admins recently changed things so that every day,
our networked printer preferences are reset using a script. Rather
than changing the preferences by hand every day so they suit my work
needs, I'd like to write a Ruby program I can run each morning. Any
pointers on where to get started? For example, is this something I
should
do via Win32OLE? If so, where do I find the right OLE calls to change
printer printing prefs?

Thanks,
Anders Schneiderman

do via Win32OLE? If so, where do I find the right OLE calls to change

printer printing prefs?

In the MSDN documentation, probably. Its one of the main sources of
docs for Windows development.

A few ideas:

- once you've found the docs (don't know if there are OLE calls, you
might have to use Win32 or .NET APIs), you can either:

a) write a C/C++ program that does the printer pref setting, then call
it from Ruby via system() and pass the needed arguments (if not too
many, and if it work well to pass them as command-line args) or else
via a file that the Ruby program writes to and the C/C++ program reads
from. Though system() is a little slow, as it means spawning an
external EXE, if its done only once a day it might not matter.

b) write a Ruby-C/C++ extension to wrap the needed Windows calls so you
can call the needed APIs directly from Ruby. This is probably more work
than a).

c) use some other option, like communicate between Ruby and C/C++
programs via sockets. Again, some more work.

···

~~~~~~~~~~~~~~~~~~~~~~
Vasudev Ram
Dancing Bison Enterprises

~~~~~~~~~~~~~~~~~~~~~~

This works for me:

require 'win32ole'
wn = WIN32OLE.new('Wscript.Network')
wn.SetDefaultPrinter('\\\mpe-ls1\mpe01lp')
puts("finished")

I'm not sure how the backslash escaping is working there, but like I
say--that's working for me...

HTH,

-Roy

Jan Svitok wrote:

···

On 12/8/06, Anders <anders.schneiderman@seiu.org> wrote:
> Our Windows XP sys admins recently changed things so that every day,
> our networked printer preferences are reset using a script. Rather
> than changing the preferences by hand every day so they suit my work
> needs, I'd like to write a Ruby program I can run each morning. Any
> pointers on where to get started? For example, is this something I
> should
> do via Win32OLE? If so, where do I find the right OLE calls to change
> printer printing prefs?

If OLE then WMI is the most probable way. Have a look at WMI (Windows
Management Interface or something like that), if there's something
interesting. Other possibilities are through the registry or direct
WINAPI calls (Win32API).

If I understood correctly, you want top reverse the changes made by
your admins. If that is right, I assume the best place to look for
information is the admins' script itself. Chances are that it is a
wsh or .vbs script, that is more or less directly translatable to
ruby, using Win32OLE. It's possible that it is an .exe file, and that
is a harder case (regmon from sysinternals might be helpful then).