CMD and SG 2010 – Part 3.

Monday. One day in week I’m not looking forward to and would sometimes like to erase it from calendar. Tomorrow will be hopefully different – mainly because we expect new furniture in our office. I really was looking forward to it, old ones didn’t behave well with stresses of equipment we kept inside (laptops, hard drives, switches…). New one should be fine with that. Will see…

One bad news: my meeting at podcast is postponed: it will happen on 1st July.

One good news: I have more time to prepare for it. But knowing myself long enough – it probably won’t have any effect on my preparations. I’m the ‘last minute’ type. Meaning: if I can do something later – I will do it too late. 😉

So now to the script. Event 5 – here I come equipped with CMD only! 😀 OK, I’ve used wmic.exe, but it’s not external tool for windows, so it’s valid. 🙂

CMD, using GeSHi – Beginner Event 5
  1. @echo off
  2. for /f "tokens=1,2" %%A in (‘wmic CPU GET Manufacturer^, MaxClockSpeed’) do CALL :Process %%A %%B
  3. echo CPU speed on %COMPUTERNAME% is %SPEED%. The maker is %Manu%.
  4. goto :EOF
  6.   :Process
  7.   if [%1]==[] goto :EOF
  8.   SET Manu=%1
  9.   SET SPEED=%2
  10.   goto :EOF

It’s using call :label technique I’ve learned ages ago. Almost like subs with pure ‘DOS’ as people tend to call it. Worked like charm so I have moved quickly to next event, which was quite big, and took me a while to get all pieces together. 😉

Script is pretty long:

CMD, using GeSHi – Beginner Event 6
  1. @echo off
  2. echo IP,ServiceState,ServiceStartMode,ComputerName,IsDomainMaster,MaintainServerList
  3. for /l %%N in (1,1,255) do CALL :CheckIP %%N
  4. goto :EOF
  6.     :CheckIP
  7.     SET IP=192.168.1.%1
  8.     ping %IP% -n 1 | find "bytes=" > nul || (
  9.         echo %IP% is not responding 1>&2
  10.         goto :EOF
  11.     )
  12.     for /f "tokens=1,2,3 skip=1 eol= " %%A in (‘wmic /node:%IP% SERVICE WHERE "Name=’Browser’" GET Started^, StartMode^, SystemName’) do call :SetService %%A %%B %%C
  13.     goto :EOF
  15.     :SetService
  16.     if [%1]==[] (
  17.         goto :EOF
  18.     )
  19.     if [%1]==[TRUE] (SET ServiceState=Running) ELSE (SET ServiceState=Stopped)
  20.     SET ServiceStartMode=%2
  21.     SET ComputerName=%3
  22.     for /f "tokens=3" %%I in (‘reg query \%ComputerName%HKLMSystemCurrentControlSetServicesBrowserParameters /v MaintainServerList ^| find "MaintainServerList"’) do SET MaintainServerList=%%I
  23.     for /f "tokens=3" %%I in (‘reg query \%ComputerName%HKLMSystemCurrentControlSetServicesBrowserParameters /v IsDomainMaster ^| find "IsDomainMaster"’) do SET IsDomainMaster=%%I
  24.     echo %IP%,%ServiceState%,%ServiceStartMode%,%ComputerName%,%IsDomainMaster%,%MaintainServerList%
  25.     goto :EOF

What I like about that script? It uses few of my favorite techniques:

  • || to react to an error (one can expect timeouts, so script should respond to it and send info about it to ‘error’ pipe
  • subs to make code more reliable and easier to read/ debug
  • separating stdout and errout with use of 1>&2
  • for here, for there – in cmd it’s ‘for’ everywhere. Here used, besides well known and often used ‘for /f’- way in for (i=0; i<max; i++) fashion.

If you redirect 2> nul – you won’t see any error. If you redirect 1> file.csv, you will see only errors and useful data will go to file. Should help to get decent file that excel can work with. No COM object required. 😉

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s