CMD and SG 2010 – Part 1.

First of all: I was third in SG this year. 🙂 It means I will have a chance to talk with Jeffrey Snover! Yupi!

That was a great news, but next one was even greater than first one: it will be on my favorite podcast, and Ed will be also there! How cool is that?! 🙂

I was thinking about 1000s of questions I could possibly ask, things I could say, and so long, and so forth. And while doing so I had to thing about how simple beginner events was. And I thought how cool it would be to get all events in cmd. And you know what? It worked! I learned few new tricks, so it was again learning experience. I would like to share my simple scripts, and because there is no cmd scripts repository (at least I failed to find one), I will have to copy it here. Good news – it was short, so it won’t be hard to read it. I don’t encourage you to use it however: go get Powershell and do it all in one line. 😉

CMD, using GeSHi 1.0.8.6 – Beginner Event 1
  1. @echo off
  2. if [%1] == [] (
  3.   SET KEY="HKCUSoftwareScriptingGuys2010ScriptingGames"
  4. ) ELSE (
  5.   SET KEY=%1
  6. )
  7.  
  8. if [%2] == [] (
  9.   SET VAL="LastUpdate"
  10. ) ELSE (
  11.   SET VAL=%2
  12. )
  13.  
  14. if [%3] == [] (
  15.   SET DATA="%DATE%"
  16. ) ELSE (
  17.   SET DATA=%3
  18. )
  19.  
  20. echo Setting key:%KEY% val:%VAL% with data:%DATA%
  21. REG ADD %KEY% /V %VAL% /D %DATA% /F

As you can see it work fine without any parameters, but it’s also possible to pass arguments to the script. No error handling, but I think it’s something I would not need anyway. 😉

So now for Beginner event no 2, where actual learning experience started. I’ve never used wmic.exe before. Fortunately I knew it existed and had something to start with. Read some help, googled a bit, and see what I got:

CMD, using GeSHi 1.0.8.6 – Beginner Event 2
  1. @echo off
  2. set MyDate=Unknown
  3. for /f %%T in (‘wmic NTEVENT where "LogFile=’system’ and EventCode=’6005‘" GET TimeGenerated’) do call :SetDate %%T
  4. echo Last boot date: %MyDate%
  5. goto :EOF
  6.  
  7.   :SetDate
  8.   if not [%MyDate%]==[Unknown] goto :EOF
  9.   if %1==TimeGenerated goto :EOF
  10.   SET TempDate=%1
  11.   SET Year=%TempDate:~0,4%
  12.   SET Month=%TempDate:~4,2%
  13.   SET Day=%TempDate:~6,2%
  14.   SET MyDate=%Year%%Month%%Day%
  15.   goto :EOF
  16.  

Next two tomorrow. 🙂

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s