Search code examples
batch-filewmic

Issue with WMIC and 'and' verb


I'm having a little trouble getting this WMIC query to work.

@echo off
setlocal

Call :wmic nicconfig where IPEnabled=TRUE and DefaultIPgateway is not null get ipaddress,macaddress,defaultipgateway /format:list
exit /b

:wmic
for /f "delims=" %%A in ('"wmic %*"') do for /f "delims=" %%B in ("%%A") do echo %%B
exit /b

I'm missing something simple but I don't know what. I keep getting "and - Invalid alias verb."

TIA

Matt


Solution

  • As Bill_Stewart points out, the DefaultIPGateway column holds an array datatype, which can't be queried with WQL. That doesn't mean wmic is incapable of handling compound where clauses, though. You simply have to enclose the argument to where within quotation marks.

    wmic nicconfig where "IPEnabled=TRUE and IPConnectionMetric>0" get ipaddress,macaddress,defaultipgateway /format:list
    

    See? That whole IPEnabled=TRUE and IPConnectionMetric>0 thing is one argument. You're getting an error because AND is occurring where wmic expects get or list or call, etc.

    Incidentally, if you ever need to query a string, you must use single quotes within the double quotes. Unlike booleans and ints, strings must be quoted.

    wmic nicconfig where "Description LIKE '%%NVIDIA%%'" list /format:list
    

    There are a couple of other issues to consider. When calling wmic within a for loop, equal signs and commas either need to be quoted or caret-escaped. Also, in a for /f loop, you don't need both single and double quotes surrounding the entire command. Change your script as follows and it should work:

    @echo off
    setlocal
    
    call :wmic nicconfig where "IPEnabled=TRUE and IPConnectionMetric>0" get ipaddress^^,macaddress^^,defaultipgateway /format:list
    exit /b
    
    :wmic
    for /f "delims=" %%A in ('wmic %*') do for /f "delims=" %%B in ("%%A") do echo %%B
    exit /b