Capturing WMIC output as a Variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sourbread
Posts: 23
Joined: 26 Apr 2012 09:10

Capturing WMIC output as a Variable

#1 Post by sourbread » 27 Sep 2012 09:37

This one should be pretty simple, I'm assuming, but I can't figure it out.

Want I need to do is write a script that will queue WMIC to retrieve both the Model# and Serial# of the machine and display it in a pretty neat fashion (in a nutshell, I plan on doing much more with it later)

So far

Code: Select all

@echo off
for /F "tokens=*" %%a in ('wmic csproduct get name') do set model=%%a
for /F "tokens=*" %%b in ('wmic bios get serialnumber') do set serial=%%b
echo %serial% and %model%
pause


This alone is only displaying an output of " and "

Anyone know why that is? Is there some anomaly behind WMIC when trying to capture the output? Even if I redirect the output to a text file, then try to import the text file and make it a variable, it doesn't work correctly.

For the record, I'm a total noob with for /F so I may have completely destroyed the code here, I was assuming %%a was the first variable and I could set %%b to be the second variable.

As always, Thanks a million!

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Capturing WMIC output as a Variable

#2 Post by Squashman » 27 Sep 2012 10:18

This works for me on XP SP3

Code: Select all

@echo off
for /F "skip=1 tokens=*" %%a in ('wmic csproduct get name') do set model=%%a
for /F "skip=1 tokens=*" %%b in ('wmic bios get serialnumber') do set serial=%%b
echo %serial% and %model%
pause

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

Re: Capturing WMIC output as a Variable

#3 Post by foxidrive » 27 Sep 2012 10:30

It needs a tweak to work on Win7. This should work on any machine with wmic.

Code: Select all

@echo off
for /F "skip=1 tokens=*" %%a in ('wmic csproduct get name') do if not defined model set model=%%a
for /F "skip=1 tokens=*" %%b in ('wmic bios get serialnumber') do if not defined serial set serial=%%b
echo %serial% and %model%
pause



Note that not all PCs have a serial number.

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

Re: Capturing WMIC output as a Variable

#4 Post by foxidrive » 27 Sep 2012 10:33

sourbread wrote:For the record, I'm a total noob with for /F so I may have completely destroyed the code here, I was assuming %%a was the first variable and I could set %%b to be the second variable.



They could both use %%a or %%b or %%c as it has no bearing on the order of the output, unless 2 or more tokens are used.

sourbread
Posts: 23
Joined: 26 Apr 2012 09:10

Re: Capturing WMIC output as a Variable

#5 Post by sourbread » 27 Sep 2012 11:58

Oh okay great! Just so I know, what does the skip=1 do?

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Capturing WMIC output as a Variable

#6 Post by Squashman » 27 Sep 2012 12:07

sourbread wrote:Oh okay great! Just so I know, what does the skip=1 do?

From the help file.

Code: Select all

skip=n          - specifies the number of lines to skip at the
                  beginning of the file.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Capturing WMIC output as a Variable

#7 Post by Dos_Probie » 27 Sep 2012 20:41

Try This ! :mrgreen:

Code: Select all

@echo off

wmic Bios Get SerialNumber /format:hform >PCAudit.htm

wmic baseboard list brief /format:hform >PCAudit.htm

wmic computersystem list brief /format:hform >>PCAudit.htm

wmic useraccount list brief /format:hform >>PCAudit.htm

wmic cpu list full /format:hform >>PCAudit.htm

wmic bios list brief /format:hform >>PCAudit.htm

wmic diskdrive list brief /format:hform >>PCAudit.htm

PCAudit.htm

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

Re: Capturing WMIC output as a Variable

#8 Post by foxidrive » 27 Sep 2012 22:41

I get this under Windows 7, Dos_probie, and a zero byte file.

Invalid XSL format (or) file name.
Invalid XSL format (or) file name.
Invalid XSL format (or) file name.
Invalid XSL format (or) file name.
Invalid XSL format (or) file name.
Invalid XSL format (or) file name.
Invalid XSL format (or) file name.


Windows 7 has a bug with the format command: see here http://www.ctkn.net/2011/10/fix-for-win ... file-name/

In Windows 7, there is a bug with wmic and the /format switch. When using the /format switch, you will get the error “Invalid XSL format (or) file name.”
1. Example:

C:\Tmp>wmic /output:C:\tmp\procinfo.html CPU get Description,
DeviceID, Manufacturer, MaxClockSpeed, Name, Status, SystemName /format:hform.xsl
Invalid XSL format (or) file name.

2. Cause:

Windows looks for the XSL stylesheets but cannot find them, which produces the error above.
3. Resolution:

Copy *.xsl from x:\Windows\system32\wbem\en-US\ to x:\Windows\system32 – this can be done quickly from command prompt: (run as administrator)

copy /b /y %WINDIR%\system32\wbem\en-US\*.xsl %WINDIR%\system32\


I think you'll need to reboot too. There is more info in the comments for 64 bit systems.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Capturing WMIC output as a Variable

#9 Post by Dos_Probie » 28 Sep 2012 06:12

That's bizarre! - .xml stylesheets have always opened up Perfect on all of my 64-bit W7 Machines!
Do you have MS Office or Open Office installed?, anyway now you Know a easy fix for those who may
have an issue with the /format switch and .xml so thats the good thing.. :mrgreen:

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

Re: Capturing WMIC output as a Variable

#10 Post by foxidrive » 29 Sep 2012 00:14

That still didn't fix it on my Windows 7 - I had to specify hform.xsl and then it worked.

Code: Select all

@echo off
wmic Bios Get SerialNumber /format:hform.xsl >PCAudit.htm
wmic baseboard list brief /format:hform.xsl >PCAudit.htm
wmic computersystem list brief /format:hform.xsl >>PCAudit.htm
wmic useraccount list brief /format:hform.xsl >>PCAudit.htm
wmic cpu list full /format:hform.xsl >>PCAudit.htm
wmic bios list brief /format:hform.xsl >>PCAudit.htm
wmic diskdrive list brief /format:hform.xsl >>PCAudit.htm
PCAudit.htm

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Capturing WMIC output as a Variable

#11 Post by Dos_Probie » 29 Sep 2012 16:37

[quote="foxidrive"]That still didn't fix it on my Windows 7 - I had to specify hform.xsl and then it worked.

Foxi, pops up "Invalid format" when I run your updated code.. :evil:

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Capturing WMIC output as a Variable

#12 Post by aGerman » 30 Sep 2012 07:29

Merging several HTML outputs will produce non valid HTML source due to several <html> tags.
You're lucky if your bowser forgives you :lol:

Write your own (X)HTML formatted file if you like that kind of output. E.g.:

Code: Select all

@echo off &setlocal EnableExtensions DisableDelayedExpansion

:: fits almost to the XHTML charset ISO-8859-1
>nul chcp 1252

:: predefined variables
set "xhtml=test.xhtml"
set LF=^


set ^"\n=^^^%LF%%LF%^%LF%%LF%^^" &REM Don't remove the 2 blank lines!!!
set header=^
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"^>%\n%
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"^>%\n%
  ^<head^>%\n%
    ^<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /^>%\n%
    ^<title^>%computername%^</title^>%\n%
    ^<style type="text/css"^>%\n%
    /* ^<![CDATA[ */%\n%
      table {background-color:#DDEEEE; font-family:Tahoma; font-size:10pt;}%\n%
    /* ]]^> */%\n%
    ^</style^>%\n%
  ^</head^>%\n%
  ^<body^>%\n%
    ^<h3^>Computer: %computername%^</h3^>%\n%
    ^<table border="1" width="100%%"^>%\n%
      ^<colgroup^>%\n%
        ^<col width="20%%" /^>%\n%
        ^<col width="20%%" /^>%\n%
        ^<col width="60%%" /^>%\n%
      ^</colgroup^>%\n%
      ^<tr^>^<th^>Alias^</th^>^<th^>Value^</th^>^<th^>Data^</th^>^</tr^>

set line_1=      ^<tr^>^<td^>
set line_2=^</td^>^<td^>
set line_3=^</td^>^</tr^>

set footer=^
    ^</table^>%\n%
  ^</body^>%\n%
</html^>

:: write the XHTML content
setlocal EnableDelayedExpansion
  >"!xhtml!" echo(!header!
endlocal

  REM append more WMIC statements inside here
for %%i in (
  "csproduct get name"
  "bios get serialnumber"
) do (
  for /f "tokens=1,2*" %%j in (%%i) do (set "alias=%%j" &set "value=%%l")
  for /f "delims=" %%j in ('wmic %%~i /format:list') do for /f "tokens=1* delims==" %%k in ("%%j") do set "data=%%l"
  setlocal EnableDelayedExpansion
    >>"!xhtml!" echo(!line_1!!alias!!line_2!!value!!line_2!!data!!line_3!
  endlocal
)

setlocal EnableDelayedExpansion
  >>"!xhtml!" echo(!footer!
endlocal

:: run it
start "" "%xhtml%"

Regards
aGerman

Post Reply