WMIC query in for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

WMIC query in for loop

#1 Post by A_Bobby » 04 Oct 2014 18:21

Hi,

By running the following query, I get my response with headers (IPAddressName and InstanceName) which I want to eliminate

wmic /NameSpace:\\root\Microsoft\SqlServer\ComputerManagement11 Path ServerNetworkProtocolProperty where (PropertyName='IPAddress' or IPAddresName='IPAll') get IPAddressName, InstanceName

so I am running the following

for /f "skip=1 tokens=1,2 delims= " %a in ('wmic /NameSpace:\\root\Microsoft\SqlServer\ComputerManagement11 Path ServerNetworkProtocolProperty where ^(PropertyName^='IPAddress' or IPAddressName^='IPAll') get InstanceName^, IPAddressName'^) do @echo %a %b

but I am getting error (get was unexpected at this time)

What I am doing wrong?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: WMIC query in for loop

#2 Post by foxidrive » 04 Oct 2014 21:29

Test the cmd prompt version of the command and verify that it returns the data you need.
Then paste it here and you may get a workaround.

In essence you can double quote the entire command and avoid escaping terms, like this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
(For /F "tokens=1,* delims==" %%A in ('"wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /value |find "M" "') do (
set "line=%%A= %%B"
set "line=!line:~0,-1!"
echo !line!
))
pause

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: WMIC query in for loop

#3 Post by ShadowThief » 04 Oct 2014 21:56

Is that going to work with find also using double quotes?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: WMIC query in for loop

#4 Post by foxidrive » 04 Oct 2014 21:58

ShadowThief wrote:Is that going to work with find also using double quotes?


Sure does.

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

Re: WMIC query in for loop

#5 Post by dbenham » 05 Oct 2014 05:54

foxidrive wrote:
ShadowThief wrote:Is that going to work with find also using double quotes?


Sure does.

Actually, that depends on what is inside the inner set of quotes, because they will no longer be quoted during the parent script parsing. Any poison characters would need to be escaped.


Dave Benham

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: WMIC query in for loop

#6 Post by A_Bobby » 05 Oct 2014 08:11

Double quotes did the trick. Thanks!
I am getting desired output except that the last line I get "Echo is on". I copied the command in .cmd file with @echo off in the beginning but it still does it.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: WMIC query in for loop

#7 Post by ShadowThief » 05 Oct 2014 08:18

That's the message you get when you try to print an empty variable. It sounds like !line! isn't equal to anything at some point.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: WMIC query in for loop

#8 Post by foxidrive » 05 Oct 2014 08:24

yeah, WMIC outputs things like two carriage returns at the same time without line feeds, etc.
There are workarounds - the one that is used depends on the command line and the data being extracted.

You can redirect the WMIC output into a text file and use a Hex viewer/editor to see the actual output and where the problem is.

Post Reply