Batch Scripting References
Tuesday, August 30, 2011
“Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime”—Author unknown.
My ability to use my weak kung fu to put together batch scripts has only been a recent occurrence. For the most part I was always constricted by my tools. If my tool wasn’t able to automate a process then I’d adapt and take a little bit more time to complete a task. If my tools didn’t perform a task then I’d search for another tool or script to accomplish what I needed. Basically, I had to adapt to my tools to perform a task instead of making my tools adapt to the task at hand. Things changed when I spent a week working on a case when I realized knowing how to script was a necessity. I’m sharing the references I came across that did a decent job of teaching me how to write batch files.
The first reference was what taught me how to fish. Batch Files (Scripts) in Windows provides an introductory overview about batch files. The article starts out explaining what a batch file is and how to construct one before it covers more advanced topics. A few topics include explanations about using if statements and for loops in scripts. The author provides links pointing to explanations about terms the reader may want more information on. The article taught me the basics of writing batch files and afterwards I was able to write simple scripts without needing to do anymore research. In a way the article converted me from being a person who receives fish from others (scripts) to one who is able to catch my own fish (write my own scripts).
The scripts I’ve been writing automate repetitive tasks such as running the same command against different folders. The for loop is one option to complete repetitive tasks and this is where the next reference comes into play. ss64.com’s For loop webpages breaks down the syntax for the different ways to implement a for loop. The information on the site gave me a better understanding on how to write for loops. If Batch File (Scripts) in Windows taught me how to fish then ss64 helped me to improve my casting.
Despite having a pretty decent cast, I’m still fishing with a bobber. Beginner fishermen may have a tough time knowing when to set the hook in the fish’s mouth so a bobber helps them. Bobbers are a visual indicator that a fish is biting your line which alerts the fisherman when to set the hook. Similar to a beginner fisherman, I still need to learn a lot more. Rob van der Woude’s Scripting Pages website has a few pages discussing batch scripting. So far the site has helped me solve a few scripting problems I encountered but there’s still a wealth of information I haven’t even read.
One item that makes batch scripting a little easier is native Windows commands can be used in addition to third party tools. Microsoft’s Command-line reference A-Z is a great resource for learning about commands. The command-line reference A-Z is the equivalent to adding additional lures and bait to your tackle box so you can catch bigger and better fish.
The last reference and one that shouldn’t be overlooked is having a person to bounce ideas off of. The person doesn’t need to be an expert either. My coworker is in the same boat as me and is trying to learn how to write batch files. It’s been helpful to have someone to provide feedback on what I’m trying to do and to help me work through complex code. A person is like a fishing buddy who can provide you with some tips, better ideas, or helps you become a better fisherman.
Learning how to write batch scripts has been an awaking. I’m leveraging my tools to extract data in different ways and I'm cutting the time required to complete some tasks in half. I constantly reflect on what tasks can be automated with scripting and how I can present extracted data to better suite my needs. Paraphrasing the quote I referenced through out my post is the best way to illustrate how I benefited from learning how to script.
“Give a man a script; you have solved his issue for today. Teach a man to script; and you help him solve his own issues for a lifetime.”
My ability to use my weak kung fu to put together batch scripts has only been a recent occurrence. For the most part I was always constricted by my tools. If my tool wasn’t able to automate a process then I’d adapt and take a little bit more time to complete a task. If my tools didn’t perform a task then I’d search for another tool or script to accomplish what I needed. Basically, I had to adapt to my tools to perform a task instead of making my tools adapt to the task at hand. Things changed when I spent a week working on a case when I realized knowing how to script was a necessity. I’m sharing the references I came across that did a decent job of teaching me how to write batch files.
The first reference was what taught me how to fish. Batch Files (Scripts) in Windows provides an introductory overview about batch files. The article starts out explaining what a batch file is and how to construct one before it covers more advanced topics. A few topics include explanations about using if statements and for loops in scripts. The author provides links pointing to explanations about terms the reader may want more information on. The article taught me the basics of writing batch files and afterwards I was able to write simple scripts without needing to do anymore research. In a way the article converted me from being a person who receives fish from others (scripts) to one who is able to catch my own fish (write my own scripts).
The scripts I’ve been writing automate repetitive tasks such as running the same command against different folders. The for loop is one option to complete repetitive tasks and this is where the next reference comes into play. ss64.com’s For loop webpages breaks down the syntax for the different ways to implement a for loop. The information on the site gave me a better understanding on how to write for loops. If Batch File (Scripts) in Windows taught me how to fish then ss64 helped me to improve my casting.
Despite having a pretty decent cast, I’m still fishing with a bobber. Beginner fishermen may have a tough time knowing when to set the hook in the fish’s mouth so a bobber helps them. Bobbers are a visual indicator that a fish is biting your line which alerts the fisherman when to set the hook. Similar to a beginner fisherman, I still need to learn a lot more. Rob van der Woude’s Scripting Pages website has a few pages discussing batch scripting. So far the site has helped me solve a few scripting problems I encountered but there’s still a wealth of information I haven’t even read.
One item that makes batch scripting a little easier is native Windows commands can be used in addition to third party tools. Microsoft’s Command-line reference A-Z is a great resource for learning about commands. The command-line reference A-Z is the equivalent to adding additional lures and bait to your tackle box so you can catch bigger and better fish.
The last reference and one that shouldn’t be overlooked is having a person to bounce ideas off of. The person doesn’t need to be an expert either. My coworker is in the same boat as me and is trying to learn how to write batch files. It’s been helpful to have someone to provide feedback on what I’m trying to do and to help me work through complex code. A person is like a fishing buddy who can provide you with some tips, better ideas, or helps you become a better fisherman.
Learning how to write batch scripts has been an awaking. I’m leveraging my tools to extract data in different ways and I'm cutting the time required to complete some tasks in half. I constantly reflect on what tasks can be automated with scripting and how I can present extracted data to better suite my needs. Paraphrasing the quote I referenced through out my post is the best way to illustrate how I benefited from learning how to script.
“Give a man a script; you have solved his issue for today. Teach a man to script; and you help him solve his own issues for a lifetime.”
Labels:
script
Corey,
Great post. I outlined a good example of how basic batch scripting can be used in DFIR to automate repetitive tasks such as USB analysis in Restore Points using Reg Ripper in a recent blog post(http://davnads.blogspot.com/2011/06/basic-groundwork-for-cmd-line-scripting.html). Batch scripting is basic stuff that can go a very long way. Also, it's a good way for anyone to get their feet wet in scripting.
David Nides
Agreed. Its amazing what a little scripting can do. I wrote a shell script which ran all the things from SANS's timeline anaylsis tools against an image. Sure it did require some user input, but after 2 minutes you can walk away on a coffee break knowing work is being done :)
Great post
Nice post, Corey. Good info and presented well. I'll bet this will become one of your pillar posts.