help with wmic bat file ouput problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tom0567uk
Posts: 2
Joined: 23 Mar 2014 04:44

help with wmic bat file ouput problem

#1 Post by tom0567uk » 23 Mar 2014 05:19

hi all
hoping you can help me (please keep in mind I am not an experienced bat file creator)
I need to run Bat file on a number of Windows XP / 7 pc's and get required information from each one.
the bat file below gives me the results I want but I have x2 output formatting issues

@echo off

MD\"PC Details"
wmic nicconfig get DNSHostName >>C:\"PC Details"\PC_Details.txt
wmic csproduct get name/value >>C:\"PC Details"\PC_Details.txt
wmic bios get serialnumber/value >>C:\"PC Details"\PC_Details.txt
wmic diskdrive get model/value >>C:\"PC Details"\PC_Details.txt
wmic path win32_physicalmedia get SerialNumber/value >>C:\"PC Details"\PC_Details.txt

end


Issue No.1 issue seems to be invisible carrage returns for Hostname result:
DNSHostName





Tom_LPT





issue No.2 is there a way to have the out so it reads like the following:
Hostname PC Model PC Serial No. HDD Model HDD Serial No.
pc123 dell adfbs samsung 647eje

all help gratefully received

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

Re: help with wmic bat file ouput problem

#2 Post by foxidrive » 23 Mar 2014 05:58

dnshostname is the same as %computername% isn't it?

Code: Select all

echo %computername%


How do you intend to resolve the issue when there is more than one HDD?
How do you intend to resolve the issue when there is no serial number? (as in my machine)

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

Re: help with wmic bat file ouput problem

#3 Post by aGerman » 23 Mar 2014 08:07

foxidrive is right. Probably not a good idea to write everything into one line. Try something like that instead

Code: Select all

@echo off &setlocal

:: make sure variables are not predefined
for %%i in (Net_DNSHostName_ PC_Name_ PC_SerialNumber_ HDD_Model_ HDD_SerialNumber_) do (
  for /f "delims==" %%j in ('2^>nul set %%i') do set "%%j="
)


:: collect data
for /f "delims=" %%i in (
  'wmic nicconfig get DNSHostName /value^|findstr /n "^"'
) do for /f "tokens=1,2* delims=:=" %%j in ("%%i") do set "Net_%%k_%%j=%%l"

for /f "delims=" %%i in (
  'wmic csproduct get Name /value^|findstr /n "^"'
) do for /f "tokens=1,2* delims=:=" %%j in ("%%i") do set "PC_%%k_%%j=%%l"

for /f "delims=" %%i in (
  'wmic bios get SerialNumber /value^|findstr /n "^"'
) do for /f "tokens=1,2* delims=:=" %%j in ("%%i") do set "PC_%%k_%%j=%%l"

for /f "delims=" %%i in (
  'wmic diskdrive get Model /value^|findstr /n "^"'
) do for /f "tokens=1,2* delims=:=" %%j in ("%%i") do set "HDD_%%k_%%j=%%l"

for /f "delims=" %%i in (
  'wmic path win32_physicalmedia get SerialNumber /value^|findstr /n "^"'
) do for /f "tokens=1,2* delims=:=" %%j in ("%%i") do set "HDD_%%k_%%j=%%l"


:: output
>"C:\PC Details\PC_Details.txt" (
  for %%i in (Net_DNSHostName_ PC_Name_ PC_SerialNumber_ HDD_Model_ HDD_SerialNumber_) do (
    for /f "tokens=1,2 delims=_" %%j in ("%%i") do echo - %%j %%k:
    for /f "tokens=1* delims==" %%j in ('2^>nul set %%i') do echo(%%k
    echo(
  )
)

Regards
aGerman

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

Re: help with wmic bat file ouput problem

#4 Post by foxidrive » 23 Mar 2014 08:40

That works nicely here aGerman.

But because I love fiddling with sed and repl.bat I tried this - which is almost as good.

Code: Select all

@echo off
(
wmic nicconfig get DNSHostName /format:csv
wmic csproduct get name /format:csv
wmic bios get serialnumber /format:csv
wmic diskdrive get model /format:csv
wmic path win32_physicalmedia get SerialNumber /format:csv
) |repl "^.*?," "" |repl "\r\r\n" "\r\n" xm |repl "\n\r\n\r" "" xm >file.txt


This above code uses a helper batch file called `repl.bat` - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

EDIT: Added aGerman's enhancement to remove consecutive blank lines.

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

Re: help with wmic bat file ouput problem

#5 Post by aGerman » 23 Mar 2014 09:13

Looks OK. But it doesn't solve issue #1.

Code: Select all


DNSHostName












my-PC



Name
...

Is there a way for repl.bat to remove consecutive line breaks?
Tried

Code: Select all

) |repl "^.*?," "" |repl "\r\r\n" "\r\n" xm |repl "\n\r" "" xm >file.txt

But it remove all empty lines of course.

Regards
aGerman

Edit:

Code: Select all

) |repl "^.*?," "" |repl "\r\r\n" "\r\n" xm |repl "\n\r\n\r" "" xm >file.txt

works.

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

Re: help with wmic bat file ouput problem

#6 Post by foxidrive » 23 Mar 2014 09:44

Yes, that works well aGerman. I added your enhancement to my post above.


Funnily enough I get this at the top where APPLE is my PC name - probably because of VirtualBox networking - so it's possible to get more than one DNShostname too.

DNSHostName
APPLE

APPLE

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

Re: help with wmic bat file ouput problem

#7 Post by dbenham » 23 Mar 2014 09:52

Sure, but I think I have a better solution:

1) Remove unwanted extra \r. The normal \r\n line terminator is not searchable (unless M mode), but the extra \r before the line terminator is searchable.

Code: Select all

repl \r ""

2) Remove the unwanted "blank" lines that consist of the host name, followed by a comma, then end-of-line.

Code: Select all

repl "^.*?,\r\n" "" m

3) Remove the host name prefix from all remaining lines:

Code: Select all

repl "^.*?," ""

Putting it all together:

Code: Select all

@echo off
(
  wmic nicconfig get DNSHostName /format:csv
  wmic csproduct get name /format:csv
  wmic bios get serialnumber /format:csv
  wmic diskdrive get model /format:csv
  wmic path win32_physicalmedia get SerialNumber /format:csv
)|repl \r ""|repl "^.*?,\r\n" "" m|repl "^.*?," "" >"C:\PC Details\PC_Details.txt"

Note that the last WMIC command using PATH fails on my machine with "Invalid XML content" error.

I get an end result like:

Code: Select all


DNSHostName
value

Name
value

SerialNumber
value

Model
value1
value2
...


Dave Benham

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

Re: help with wmic bat file ouput problem

#8 Post by foxidrive » 23 Mar 2014 10:07

The OP can pick from three scripts now. :)

dbenham wrote:Note that the last WMIC command using PATH fails on my machine with "Invalid XML content" error.


It may be a later WMIC version enhancement.

tom0567uk
Posts: 2
Joined: 23 Mar 2014 04:44

Re: help with wmic bat file ouput problem

#9 Post by tom0567uk » 23 Mar 2014 15:35

guys thank you very much for help, I now have what i need thanks to your inputs.

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

Re: help with wmic bat file ouput problem

#10 Post by Dos_Probie » 27 Mar 2014 06:49

Here is a Working script for XP Pro to Windows 8 (without need for 3rd party tools) that prints a oneliner .txt file to the SystemDrive\PC Details directory per OP request.
Revise as needed..DP 8)

Code: Select all

@echo off
setlocal

MD "%SystemDrive%\PC Details"
Set "SD=%SystemDrive%"
Set "PC=PC_Details.txt"

:: Get DNSHOSTNAME
For /f "tokens=2 delims='='" %%A in ('wmic OS Get csname /value') Do Set Host=%%A

:: Get SYSTEMNAME
For /f "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Model /value') Do Set SysName=%%A

:: Get SERIALNUMBER
For /f "tokens=2 delims='='" %%A in ('wmic Bios Get SerialNumber /value') Do Set SerNumber=%%A

:: Get DRIVEMODEL
For /f "tokens=2 delims='='" %%A in ('wmic diskdrive get model /value') Do Set HDModel=%%A

:: Get DRIVESERIAL
For /F "tokens=2 delims==" %%A in ('WMIC Path Win32_physicalmedia Get SerialNumber /value') Do (
 for %%B in (%%A) Do (set "HDSerial=%%B")
)
cls
echo ============================================================== PC DETAILS ====================================================================== >>%SD%\%PC%
echo ------------------------------------------------------------------------------------------------------------------------------------------------ >>%SD%\%PC%
 echo.>>%SD%\%PC%.
echo DNSHostName:%Host%- Name:%SysName%- SerialNumber:%SerNumber%- Model:%HDModel%- SerialNumber:%HDSerial% >>%SD%\%PC%
 echo.>>%SD%\%PC%
echo ------------------------------------------------------------------------------------------------------------------------------------------------ >>%SD%\%PC%
echo ================================================================= END ========================================================================== >>%SD%\%PC%
move "%SD%\%PC%" "%SD%\PC Details">nul
endlocal
exit
Last edited by Dos_Probie on 29 Mar 2014 09:27, edited 4 times in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: help with wmic bat file ouput problem

#11 Post by Compo » 28 Mar 2014 11:55

tom0567uk wrote:

Code: Select all

@echo off

MD\"PC Details"
wmic nicconfig get DNSHostName >>C:\"PC Details"\PC_Details.txt
wmic csproduct get name/value >>C:\"PC Details"\PC_Details.txt
wmic bios get serialnumber/value >>C:\"PC Details"\PC_Details.txt
wmic diskdrive get model/value >>C:\"PC Details"\PC_Details.txt
wmic path win32_physicalmedia get SerialNumber/value >>C:\"PC Details"\PC_Details.txt


Issue No.1 issue seems to be invisible carrage returns for Hostname

issue No.2 is there a way to have the out so it reads like the following:
Hostname PC Model PC Serial No. HDD Model HDD Serial No.
pc123 dell adfbs samsung 647eje

Here's an idea, (untested):
    PC_Details.cmd - Please do not change the name

Code: Select all

@Echo Off&SetLocal
Set "_V=%~n0"&Set "_H="&Set "_L="
For /f "Tokens=1* Delims==" %%A In (
   'WmiC NICConfig Where "DNSHostName Is Not Null" Get DNSHostName /Value') Do (
   If '%%B Neq ' Call :Sub %%A %%B)
For /f "Tokens=1* Delims==" %%A In (
   'WmiC CSProduct Where "Name Is Not Null" Get Name /Value') Do If '%%B Neq ' (
   Call :Sub %%A %%B)
For /f "Tokens=1* Delims==" %%A In (
   'WmiC BIOS Where "SerialNumber Is Not Null" Get SerialNumber /Value') Do (
   If '%%B Neq ' Call :Sub %%A %%B)
For /f "Tokens=1* Delims==" %%A In (
   'WmiC DiskDrive Where "Model Is Not Null" Get Model /Value') Do (
   If '%%B Neq ' Call :Sub %%A %%B)
For /f "Tokens=1* Delims==" %%A In (
   'WmiC PATH Win32_PhysicalMedia Where "SerialNumber Is Not Null"^
 Get SerialNumber /Value') Do If '%%B Neq ' Call :Sub %%A %%B
If Not Defined _L GoTo :Eof
If Not Exist "%SystemDrive%\%_V:_= %" MD "%SystemDrive%\%_V:_= %"
(Echo=%_H%
   Echo=%_L%)>"%SystemDrive%\%_V:_= %\%_V%.txt
GoTo :Eof
:Sub
For /F "Tokens=1*" %%A In ("%*") Do (If Defined _H (Set _H=%_H%   %%A
      Set _L=%_L% %%B) Else (Set _H=%%A&Set _L=%%B))
It will still output any error messages, but you're unlikely to see them from a double click and I prefer them when run from the console.

Post Reply