Becky Mcquilling wrote:
> I'm new to Python and wanted to do what is a pretty routine and common admin
> function, simple enough in VBScript, but wanted to try it in Python as
> well. I'm having troulbe working it out on my own.
Welcome to Python!
> I have a text file c:\servernames.txt. I want the script to read from that
> file, each line being a different machine name, then give me a list of the
> services on the machine and the state, write the result to a log file.
Reading lines from a file is easy enough. My own preference
is this:
servers = open ("c:/servernames.txt").read ().splitlines ()
because it strips off the trailing line feeds, but there
are several alternatives.
> The wmi part is easy to produce, on a single machine, it's stripping the
> contents from a text file one at a time and then logging it to a file, that
> I havne't gotten quite right.
It's not entirely clear what you want to log and to how many files, but
assuming that:
a) You're using the wmi module from:
http://timgolden.me.uk/python/wmi.html
b) You have a list of servers in c:\servernames.txt
c) You want the list of services from each machine
to go into a file called <servername>-services.log
then this code should at least give you an outline:
One caveat: because in Python, as in other languages,
the backslash acts as a special-character escape, you
either need to double them up in Windows filenames,
("c:\\server...") or use raw strings (r"c:\server..")
or use forward slashes ("c:/server...")
<code - untested>
import wmi
servers = open ("c:/servernames.txt").read ().splitlines ()
for server in servers:
wmi_connection = wmi.WMI (server)
with open ("%s-services.log" % server, "w") as f:
for service in wmi_connection.Win32_Service ():
f.write ("%s\t%s\n" % (service.Caption, service.State))
</code>
HTH
TJG
_______________________________________________
python-win32 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-win32