I’ve successfully written a Powershell script which:
- query AD to obtain the list of computer
- query every computer through WMI to obtain software/hardware information
- insert the collected data into a MySQL database.
The script works fine but I don’t like how it’s implemented. It’s procedural and there is a lot of code duplication, causing a mess everytime I need to change something.
Now, what I would like to ask you is: what is the cleanest way to implement it in python using OOP? I’ve thought something similar to this (pseudocode):
Class ADquery
function get_computers( UO ): return a list of computers in the specified UO
Class Computer
constructor( computername )
function query(): connect to the computer and pull the info through WMI
function print(): print the collected info to the console (debug)
property RAM
property CPU
...
Questions:
-
In order to save the collected data into a Database, do I have to create another object (e.g. Database) and pass the Computer object to him or add a member function to the Computer class (e.g. save_db() ) ?
-
If I go for the second option, that wouldn’t cause a massive number of MySQL connections when I’m dealing with multiple objects?
Thanks a lot and sorry for my bad English
That architecture looks reasonable to me.
You could do either, I’m not sure it really makes a huge difference with a small application like this.
Potentially. Depending on how it’s implemented you could get a lot of connections going on. If you’re doing a reasonable number of inserts I’d stick them in a list and insert all at once, if that’s possible with your code.