Check wmic is available

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Check wmic is available

#1 Post by Cleptography » 03 Jun 2011 13:02

What is a good way to check if wmic is available on a machine
Does this work...

Code: Select all

wmic /?
if errorlevel 1 echo.Not available

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Check wmic is available

#2 Post by orange_batch » 03 Jun 2011 21:38

WMIC is found in a static directory.

Code: Select all

if exist "%systemroot%\system32\wbem\wmic.exe" (echo:WMIC is installed.) else echo:WMIC is not installed.


AFAIK, WMIC should be installed in every Windows OS since XP (except for XP Home).

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Check wmic is available

#3 Post by dbenham » 03 Jun 2011 22:28

orange_batch wrote:WMIC is found in a static directory.

As long as the file is in your PATH, you don't even need to know the directory to look for it efficiently. You just need to know the file name:

Code: Select all

C:\>for %a in (wmic.exe) do @echo %~$path:a
C:\Windows\System32\wbem\WMIC.exe
If not found in the PATH, the modified for variable will expand to the empty string.

So a test in batch could be:

Code: Select all

for %%a in (wmic.exe) do set wmicPath=%%~$path:a
if defined wmicPath (echo WMIC is installed) else echo WMIC is not installed


Dave Benham

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Check wmic is available

#4 Post by Cleptography » 03 Jun 2011 23:13

@Dave & Orange
Thank you.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Check wmic is available

#5 Post by Ed Dyreen » 04 Jun 2011 01:57

@Cleptography
What is a good way to check if wmic is available on a machine

It's not because WMIC is present that it is also working &installed correctly!

for example a batch command for WMIC will fail in this situation

Code: Select all

Please wait while WMIC compiles updated MOF files.

Next, some code I use trying to figure out if it'll work;

Code: Select all

:ERR.Get.WMIC ( /Alias: "STRING" /ToWorkFile /Int_ERR )
::
:: SJAB v1.142c beta
::
:: Checks wheter WMIC can process @Alias
::
::INPUT:
:: @Alias             STRING    Required;       Alias to check
:: @ToWorkFile             STRING    Optional;       Save querry results to WorkFile
:: @Int_ERR             STRING    Optional;       Halt on error
::
::OUTPUT
:: @WorkFile_Succes          BYVAL    Return if @ToWorkFile;    Handle to WorkFile
:: @FullPathFile.WorkFile_Succes    BYVAL    Return if @ToWorkFile;    @FullPathFile to WorkFile
:: @ERR             BYVAL    Return;       0 for succes, 1 for error
::
SetLocal
::(
   ::set "Debug.Sub=!Sub.%Sub.Depth%.Name!"
   ::(
      ::%Debug% "" "inside !Sub.%Sub.Depth%.Name! Sub.!Sub.Depth!.Par.AsIS:" &echo.!Sub.%Sub.Depth%.Par.AsIS!_
      set "Sub.!Sub.Depth!.Use=:ERR.Get.WMIC ( /Alias: "STRING" /Int_ERR )"
   ::)

   ::%Debug% "PROC PAR"
   ::(
      set "PAR.FIX=Alias"
      set "PAR.OPT=ToWorkFile Int_ERR"
      %PROC.PAR.FAST% "()"

      ::%Debug.Equals% for %%! in ( Alias ToWorkFile Int_ERR ) do echo. %%~!=!%%~!!_
   ::)

   ::%Debug% "VALID"
   ::(
      for %%! in ( Alias ) do                %ERR.Chk.IsDefined.TokenSTR% %()% >nul

      for %%! in (

         "WorkFile_Succes"
         "WorkFile_Error"
         "WorkFile_Int_WMIC"
      ) do (
         %OpenWorkFile% "()"
         ::
         set "%%~!=!WorkFile!"
      )

      ::%Debug.Equals% for %%! in ( WorkFile_Succes WorkFile_Error WorkFile_Int_WMIC ) do echo. %%~!=!%%~!!_
   ::)

   ::%Debug% "Perform"
   ::(
      ::%Debug% " WinWaitClose: 'INT_WMIC'"
      ::(
         %WinWaitClose% /Title: "INT_WMIC" /File: "CMD.EXE"
      ::)

      ::%Debug% " Intercept 'WMIC.EXE' error: 'lockup'"
      ::(
         for %%! in (

            "!FullPath.WorkFile!\WorkFile.!WorkFile_Int_WMIC!.CMD"

         ) do    > "%%~!" (
            ::
            echo.@echo off ^&SetLocal EnableExtensions EnableDelayedExpansion
            echo.
            echo.:INIT ^(^)
            echo.::^(
            echo.   :: 120 secs * 18 = 2160 ticks
            echo.   "!FullPathFile.STACKEY.COM!" {WAIT=2160}
            echo.::^)
            echo.
            echo.:MAIN ^(^)
            echo.::^(
            echo.   taskKill.EXE /t /f /im "WMIC.EXE"
            echo.   ::
            echo.   2^>nul tasklist /nh ^|^>nul findstr.EXE /i /c:"WMIC.EXE" ^&^&goto :MAIN "^(^)"
            echo.::^)
            echo.
            echo.:EXIT ^(^)
            echo.::^(
            echo.::
            echo.goto :eof ^(^)
            echo.::^)
         )
         ::
         start "INT_WMIC" /low /min /D "!SystemROOT!" "%comspec%" /c "!FullPath.WorkFile!\WorkFile.!WorkFile_Int_WMIC!.CMD"
      ::)

      ::%Debug% " Querry 'WMIC.EXE' service: '!Alias!'"
      ::%Debug.Equals% WMIC.EXE !Alias!
      ::%Debug.Equals% pause
      ::(
         set /a ERR.MEM = 1
         ::
         for %%? in (

            "!FullPath.WorkFile!\WorkFile.!WorkFile_Error!.TMP"

         ) do    for %%! in (

            "!FullPath.WorkFile!\WorkFile.!WorkFile_Succes!.TMP"

         ) do (
            2> "%%~?" 1> "%%~!" WMIC.EXE !Alias!
            ::
            for /f "tokens=*" %%! in (

               'type "%%~!"'

            ) do    set /a ERR.MEM = 0
            ::
            for /f "tokens=*" %%! in (

               'type "%%~?"'

            ) do    set /a ERR.MEM = 1
            ::
            if !ERR.MEM! equ 0 (
               ::
               set "?="
               ::
               set /p ?=< "!FullPath.WorkFile!\WorkFile.!WorkFile_Succes!.TMP"
               ::
               set "MEM=!?!"
               ::
               set "?=!?:Please wait while WMIC compiles updated MOF files.=!"
               ::
               if /i ["!?!"] neq ["!MEM!"] (
                  ::
                  1> "%%~!" WMIC.EXE !Alias!
               )
            )
         )

         ::%Debug.Equals% echo. WorkFile_Error &type "!FullPath.WorkFile!\WorkFile.!WorkFile_Error!.TMP"
         ::%Debug.Equals% echo. WorkFile_Succes &type "!FullPath.WorkFile!\WorkFile.!WorkFile_Succes!.TMP"
         ::%Debug% " ERR.MEM=!ERR.MEM!_"
      ::)

      for %%! in (

         "WorkFile_Error"
         "WorkFile_Int_WMIC"

      ) do (
         set "WorkFile=!%%~!!"
         ::
         %CloseWorkFile% %()% >nul
      )

      for %%! in (

         "WorkFile_Succes"

      ) do (
         set "WorkFile=!%%~!!"
         ::
         if not defined ToWorkFile (

            ::%Debug% " Closing WorkFile_Succes"
            %CloseWorkFile% %()% >nul
         )
      )

      ::%Debug% " Terminate intercept: 'lockup'"
      ::(
         %WinWaitOpen%          /Title: "INT_WMIC" /File: "CMD.EXE" /Timeout: "30"
         ::
         if !ERR! equ 0 %ProcessClose%    /Title: "INT_WMIC" /File: "CMD.EXE"
      ::)

      set /a ERR = !ERR.MEM!
      ::
      if !ERR! neq 0 if defined Int_ERR (
         ::
         %ERR.Set.THIS% "'WMIC.EXE' service error:" "Alias:!Alias!_ ^!"
      )
   ::)
::
::%Debug% /Pause " WorkFile_Succes=!WorkFile!_" " ERR=!ERR!_" "outside !Sub.%Sub.Depth%.Name!"
(
   EndLocal

   set "WorkFile_Succes=%WorkFile_Succes%"
   set "FullPathFile.WorkFile_Succes=!FullPath.WorkFile!\WorkFile.!WorkFile_Succes!.TMP"

   set /a ERR = %ERR%
)
goto :eof ()
::)

Post Reply