Sunday, June 3, 2012

Automating the Automator

The Control Center doesn't allow multi-selections so removing multiple computers from inventory seems impossible, right?  Here, I'll show you how to do this with a script but I hope you take away more than just one simple script. For with LabTech you can automate the LabTech server too - not just your remotes. There's lots of reasons to do this; this is just one example.

To setup for this, you'll need a test system with SqlYOG installed and a few vm's reporting into it. Use SqlYOG to open the database and look inside. Check the stored procedures. Is there a sp_DeleteComputer procedure? Open it for editing and you'll see it's looking for two properties: the ID of the computer to delete and a reason for deletion. Makes sense - when I use the GUI to delete a machine from LabTech, it knows the computer ID from my selection and the first thing it does is prompt me for a reason to delete. OK, now for the next step.

Next on the list is for us to confirm that stored procedure 1.) works as expected and 2.) is perfectly understood in exactly how it works.  Yes, I understand this is a simple stored procedure but before you rush to scripting before first testing, consider this: how much of a hassle will it be if you accidently delete the wrong computer from your production server?  What if you accidently delete more than just one?  The short of it is, you always need to test your automations before coding them.  Really.  A little effort here will pay in the long run.  Especially when those automations are running against your LabTech server.  OK, 'nuff said on that...

...so lets do this!  (Now is a good time to do a snapshot of your test environment.)  Open SqlYOG and the LabTech Control Center on your test server.  Identify the ComputerID of a vm we can delete (mine is lucky #7) and move over to SqlYOG.  Enter in this string:

call sp_DeleteComputer('7', 'Deleted thru SqlYOG')

...and press F9.  This will execute the line and, presumably delete computerID #7 (I really hope you substituted '7' for your run.)  Go back to the Control Center and refresh.  Check the navigation tree.  Is the computer you expected to be deleted, in fact, removed from the tree?  Good!  Now that we know this works as we assumed it should and we know exactly how to work it, we are ready to solidify it in script.

[aside: If you don't already have one, I suggest creating a 'Tests and Examples' script directory.  Unless you are the only person working in your LabTech server envrionment, there's always a chance someone may run one of your scripts as you are building it in a production environment... unless that script is in a directory named 'Tests and Examples!'  (Or, at least if they don't, you'll have plausable deniability for the ensuing chaos ;)  ]

New Script - If 'True' Then:
Line 1 Function: SQL Execute
  • SQL Query: call sp_DeleteComputer('%SQLDeleteMe%', 'Deleted by script')
  • ODBC Connection: (leave empty)
Name it, Uncheck 'Computer Script', check 'Function Script' and save it.  Done.

My editor is telling me this is a perfect opportunity for a cliffhanger so let's table this for a day.  Tomorrow, I'll show you how to schedule this script for daily operation and aim it at computers that haven't reported in over x days.  Until then, Happy LabTechIng!






No comments:

Post a Comment