Page 1 of 1
Capturing WMIC output as a Variable
Posted: 27 Sep 2012 09:37
by sourbread
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!
Re: Capturing WMIC output as a Variable
Posted: 27 Sep 2012 10:18
by Squashman
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
Re: Capturing WMIC output as a Variable
Posted: 27 Sep 2012 10:30
by foxidrive
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.
Re: Capturing WMIC output as a Variable
Posted: 27 Sep 2012 10:33
by foxidrive
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.
Re: Capturing WMIC output as a Variable
Posted: 27 Sep 2012 11:58
by sourbread
Oh okay great! Just so I know, what does the skip=1 do?
Re: Capturing WMIC output as a Variable
Posted: 27 Sep 2012 12:07
by Squashman
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.
Re: Capturing WMIC output as a Variable
Posted: 27 Sep 2012 20:41
by Dos_Probie
Try This !
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
Re: Capturing WMIC output as a Variable
Posted: 27 Sep 2012 22:41
by foxidrive
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.
Re: Capturing WMIC output as a Variable
Posted: 28 Sep 2012 06:12
by Dos_Probie
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..
Re: Capturing WMIC output as a Variable
Posted: 29 Sep 2012 00:14
by foxidrive
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
Re: Capturing WMIC output as a Variable
Posted: 29 Sep 2012 16:37
by Dos_Probie
[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..
Re: Capturing WMIC output as a Variable
Posted: 30 Sep 2012 07:29
by aGerman
Merging several HTML outputs will produce non valid HTML source due to several <html> tags.
You're lucky if your bowser forgives you
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