CMD and SG 2010 – Part 4.

Monday at my door and I’m really worried about it. Deadline is approaching and we have no chance to meet the goal. I hate it when somebody is leading a project in a way, that prevents members from doing things in a proposed schedule. Anyway, Thursday is pretty close so I’m slowly getting excited. And still 4 scripts to go in my cmd series. So here is the one that took me most of time to figure out, mainly because both in VBS and in Powershell it was usually done with COM object, and I have no idea how to use those in CMD. But then I thought, that this type of information has to be in the registry somewhere, looked for it a while, and – there it was. 🙂

CMD, using GeSHi 1.0.8.8 – Beginner Event 7
  1. @echo off
  2. set /a Counter=0
  3. set AdminPath=Empty
  4. for /f "delims=" %%A in (‘reg query "hklmsoftwaremicrosoftwindowscurrentversionexplorershell folders" /v "Common Administrative Tools" ^| find "Administrative Tools"’) do call :ProcessResult %%A
  5. for %%F in (%AdminPath:~0,-2%*.lnk") do call :CountAndDisplay "%%~nF"
  6. echo Done! Total %Counter% items . . .
  7. goto :EOF
  8.  
  9.   :ProcessResult
  10.   if [%AdminPath%]==[Empty] (
  11.     if [%1]==[REG_SZ] (
  12.     set AdminPath="
  13.     ) else (
  14.       shift
  15.       goto :ProcessResult
  16.     )
  17.   )
  18.   :JoinPath
  19.   if [%1]==[] goto :EOF
  20.   set AdminPath=%AdminPath%%1
  21.   goto :JoinPath
  22.  
  23.   :CountAndDisplay
  24.   echo %~1
  25.   set /a Counter+=1
  26.   goto :EOF

I’m almost sure there is easier way to get only path from reg output. But this one works too, so I do not really care. 😉

Script # 8 was easier to write, but I still seem to have some problems with output. Anyway – it works better than I would expect, so I left it like that.

CMD, using GeSHi 1.0.8.8 – Beginner Event 8
  1. @echo off
  2. echo Process Owner
  3. for /f "skip=1" %%I in (‘wmic PROCESS where "Name!=’wmic.exe’" get ProcessID’) do call :GetOwners %%I
  4. echo Done!
  5. goto :EOF
  6.  
  7.     :GetOwners
  8.     SET ID=%1
  9.     for /f "delims=" %%P in (‘wmic PROCESS where "ProcessID=’%ID%‘ and name!=’wmic.exe’" get name ^| findstr /r /v "^$ ^Name"’) do (
  10.       set Process=%%P
  11.     )
  12.     for /f "tokens=2,3 delims=;= " %%O in (‘wmic PROCESS where "ProcessID=’%ID%‘ and name!=’wmic.exe’" call getowner ^| find "User"’) do (
  13.       set Owner=%%~O %%P
  14.     )
  15.     echo %Process% %Owner%
  16.     goto :EOF

As you can see most of stuff is done with usage of wmic.exe – pretty handy tool I must admit. Even if gwmi is easier to use and remember. 😉

Advertisements

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