connection

Accessing WMI on other machines

Another admin here has been using PowerShell to check services running on a group of servers and restart them if they have stopped. Not wanting to learn PowerShell just for this I decided to have a go at writing the same script in Python.

Again Tim Golden allows the process to be simplified a little with his WMI module. There is a windows installer for this one which hides all the installation.

To access a remote server rather than your local computer just pass the servername as a parameter to the WMI function. A list of services is obtained from Win32_Service() and rest is fairly straight forward.

I created an example script which goes through a list of servers and compares all theĀ  services against a tuple of services I am interested in for any that have stopped. If it finds any it prints out the server number and the service caption before trying to start the server again.

I’ve not bothered with authorisation, the script would need to be run under a user account with the necessary privileges. The script is also not optimised, I might come back to this at a later date.

Advertisements