HOWTO “grep” any Windows platform commandline executable
For years I scoured the IT world for ways to make life a bit easier for me and my colleagues when it came to running simple commandline executables. Since discovering PSTOOLS years ago my life has become significantly easier but I still needed a specific tool and no one seemed to know how to do what I wanted.
I have dabbled in Linux and though I found the platform fun to play with for use as an edge device it never was as stable as the robust Windows platforms for use by the common variety user.
So when I needed to be able to GREP in any Windows OS I was terribly annoyed that no one seemed to know how to get this done. Even when I contracted for Microsoft the full-time guys still didn’t know or see the real need for such capability. I know MS came up with an add-on tool for this a while back but I wanted a native tool to use in my scripting with commandline executables like the SysInternals PSTOOLS.
Well I’ve stumbled across the method and it’s not advertised anywhere I could find so I offer it freely to you in expecting that some of you will find it very useful.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
HOWTO “GREP” any WINDOWS cmdline executables
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Let’s try the simple query that almost all of us would like to do but didn’t know was possible. You have 7 THOUSAND systems and need a list of a certain type of server (like a cacti server) and don’t want to take the time to load the GUI for AD (and certainly don’t want to CSV the mess every time). Your network team uses the naming convention CACT for your cacti servers (and if you don’t have a naming standard you’d better get busy making one), so you simply “grep” a net view of your domain for the content like this, looking for CACT content in the response, with Find String:
net view |FINDSTR /i /r cact
(a simple findstr /? will explain the switches)
I know it sounds ridiculously simple but most power tools are of the most simple sort. And if you think just how many ways you can use this it starts to get real interesting.
ping -t hungserver |findstr /i /r “Request timed out”
With this simple “grep” you don’t have to see anything but when the server is offline (like when you ran a forced shutdown on the hungserver and don’t care when it is up but only when it is finally down).
I used FINDSTR years ago to nightly auto Exchange 5.5 sendmail myself the CSV report for 22 Exchange 5.5 server tape backup jobs for any log file content with words like “failed” in the text and the day’s date. It was that batch file that made me realize that I was grepping, or “piping out content”, as MS says, the whole time and I didn’t even realize it!
This method of piping out content is not new, that is certain. But piping out to findstr sure does make some tasks easier, especially when you use psexec and can call a text file of servers to run this tool against. It makes the task of, for example, sending out a delete and update of a defrag job to 100+ servers with the schtasks command much quicker. You don’t want a hung batch job waiting for a downed server so you “grep” out the net view for the server name (like BLD for the “blade” naming convention) with findstr and redirect it to a txt file. Then you take this newly created list, clean out the trash and easily run a task against all those servers with a Mark Russinovich PSTOOLS executable.
(BTW: You can clean out the description fields with a VB script using PSEXEC before hand)
Hope that helps you with your commandline scripting.
Patrick J. Burwell
BurwellFamily.us