#_ITandME blog

Psexec and batch scripting


Last Edit: October 23, 2014

My job for the last couple of years has been working as a system administrator in a Windows environment. Much of what I've been learning revolves around how to make my tasks faster and more efficient.

Enter scripting.

Now when I say scripting, you might be thinking something more this from the past decade, like PowerShell or C# for example, but I've been working more with batch than anything.
While quite old and deprecated, batch scripts are still very useful for automation
Here's an example:
I'm given a list of servers to install software on, but I'm not given the architecture type (x86 or x64). Further, the application isn't smart enough to descern which to install, so I write a simple batch script to churn through the list of servers and quickly allow me to find the architecture.

        
                @ECHO OFF

                FOR %%A in (serverlist.txt) DO (psexec \\%%A -u  -p  -s cmd.exe)
        
        

What this simple script does is utilize psexec, part of MS SysInternals, to remotely execute a command on each of the servers in the list, in this case cmd.exe. If everything works right on the network, this allows me to get to a command prompt on a remote server without ever having to initiate an RDP session. Once I get to prompt I plug in the following command:


        echo %PROCESSOR_ARCHITECTURE%


In the environment I'm in, this will almost always return either x86 or AMD64. Once I have the information, I type in 'exit', and the script initiates the next server on the list.
One thing I've played around with is how to pass a command through PSEXEC to the remote command prompt, allowing for a more fully automated process. I've haven't been able to get this to work yet, but depending on the size of the list it would not be very benefitial unless I was also writing that to a file. Having to query the system each time allows me to (1) verify I am on the right system, and (2) record the processor type before the data is lost.

Anyway, this is one simple example that has greatly helped automate some of my work. The biggest thing I've learned about scripting is that it more a mindset; thoroughly accessing a situation for what is needed, and missing out on other people having most likely done the same thing.

There is always the temptation to think "I don't have time to automate my tasks", and sometimes if you really don't have a clue, it's better to just gut it out. But frequently I've found that when I start the process, I realize I'm not only helping myself and others on my team in the future, but often there are other realizations for more complicated problems in the future. Sometimes a couple of simple pieces can be combined into something much more powerful.

Happy coding, everybody.