Jump to content

How do you automate basic tasks?


IDNeon

Recommended Posts

I think it'd be useful to share some basic tasks for beginners in order to help them start to conceptualize "automation".  And so the goal of this thread is to provide a "forum" to post useful tid-bits.  And I'll kick it off with something on my mind below, but first, I think it's important to mention that the basics of automation requires generalization.  So any solution requires a generalized form.  If you're involved in mathematics any that term should ring a bell.

A generalized form would for example, use variables to contain lists that are generated by the host, such as a list of computers generated by an Active Directory that is a variable with a For loop (or other mechanism) so your task applies to all computers, or a set of those computers, contained in that list.

The goal of this thread is not to contain the entirety of an automated process, but rather to contain the bits and pieces that form parts of automation.  In terms of Programming this might be considered "functions", or the tools in your program you call upon to do things.

The one that came to mind that led me to generate this thread is just a little tid-bit I picked-up and discarded as not useful right now.  Written for batch, and the purpose was to uninstall a program I don't know the full name of and remove (scour) any left-overs from an unclean uninstall.

Each post should be a contribution, or comment on a contribution.  I'll make a second post with another contribution just to keep the ball rolling.  For organizational reasons, putting the contribution in quotes seems pertinent.

 

Quote

wmic product where "name like '%%example%%'" call uninstall >> "append results to a debugger file (always good to have).txt"
ping -c 1 -w (time in seconds) >> debug.txt
for /d %%x in ("c:\program files (x86)\example*") do rd /s /q "%%x"  >> debug.txt
dir "c:\program files (x86)" >> debug.txt
ping -c 1 -w (time in seconds) >> "debug.txt


>do some other stuff<

So this short snip will unistall an example that is part of the program name with wild cards on either side of the part.  It'll scour for directories with that same snippet with a wild card after (for instance if the directory is formed by program name with a random number generated after).

You can get creative.

I was thinking about it, because strangely deleting folders with wild cards using Batch is NOT intuitive.

The pings are timers, to give each process enough time to complete, depending on your target system a WMIC might take a while, so having a timer is critical.

Post Script:  Oh I forgot to mention a critical part.  For some reason (which I've not taken the time to figure out it's just not that important to me right now) running this script REMOTELY breaks at the WMIC.  However, the script runs normally if run locally.  If the script were to start on start-up I'm sure it'd run normally.  There's something in the remote process that WMIC hangs-up the authentication token is my guess so when ran remotely it kills the batch.

 

Edited by IDNeon
Link to comment
Share on other sites

Here is an example of automating/generalizing a task mixing batch and powershell, the requirement of batch in this example is obvious, because to run a powershell script remotely requires an execution policy, but this can be bypassed using batch:

 

Quote

wmic product get name,version > t:\installed-programs-%computername%.txt

net start > t:\running-services-%computername%.txt


set Var1A=get-windowsfeature -computername $env:computername
set Var1B=where-object installstate -eq installed
set Var1C=out-file t:\server-roles-$env:computername.txt
echo:%Var1A% ^| %Var1B% ^| %Var1C% >> t:\server-roles.ps1

powershell -ExecutionPolicy ByPass -File t:\server-roles.ps1

There are features not always installed so you might need to include a "get module" to get, for instance, servermanager which is where "get-windwosfeature" lives in older versions of powershell.

The program creates a powershell script and executes it.

This can be made as complex and lengthy as you want it in order to grab as much information as you want in a generic process, and pipe the results (out-file, output, etc.) to a single folder, or even append to a single file, where you can collect the data from some common location.

If you create a loop you, with a few more pieces, you can cycle through a laundry list of variables to grab information from a list of computers, etc.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...