wmic batch file for network scan - batch-file

I want to get the computer name, IP address and version (windows 10) by using a batch file that scans the network.
I have the following:
Set compInfo= "wmic /NODE:192.168.1.%%s OS GET Version,Caption"
Set ipAdd= "wmic /NODE:192.168.1.%%s NICCONFIG WHERE IPEnabled=true GET
IPAddress,DNSHostName"
(FOR /L %%s IN (1,1,254) DO (echo %compInfo%,%ipAdd% >>
C:\Users\xxxx\Desktop\NsLooKup\TestEcho.txt))
I'm trying to have the results in the same Line and possibly have it in CSV format with /Format:
It's not working for me, is it because the variables are outside the for loop?
EDIT:
Note: run command inside for loop
(FOR /L %%s IN (1,1,254) DO wmic /node:192.168.1.%%s OS GET Version,Caption /format:htable & wmic /node:192.168.1.%%s NICCONFIG WHERE IPEnabled=true GET IPAddress,DNSHostName /format:htable) > results.html
When I do this I get the TWO lines as a result instead of ONE. if i can make this as one line for every ip address it would work great :)

#echo off
setlocal enabledelayedexpansion
(
FOR /L %%s IN (1,1,254) DO (
set "compInfo=N/A"
set "IpAdd=N/A"
for /f "delims=" %%a in ('wmic /NODE:192.168.1.%%s os get version^,caption /all^|find "."') do set compInfo=%%a
for /f "delims=" %%a in ('wmic /NODE:192.168.1.%%s NICCONFIG WHERE "IPEnabled=true" GET IPAddress^,DNSHostName /all^|find "."') do set IpAdd=%%a
echo !compInfo:~0,-1!,!IpAdd:~0,-1!
)
)>"C:\Users\xxxx\Desktop\NsLooKup\TestEcho.txt"

Related

Batch file to collect UUID and Serial number

I have been using this batch file to collect the Serial number and UUID number and output to a CSV and now it no longer works.
#echo off
set outputfile="Y:\HP\UUDI.csv"
for /f "delims== tokens=2" %%i in ('wmic csproduct Get "UUID" /value') do SET CSPRODUCT=%%i
for /f "delims== tokens=2" %%i in ('wmic bios get serialnumber /value') do SET SERIAL=%%i
echo UUID,Serial,>>%outputfile%
echo %CSPRODUCT%,%SERIAL%,>>%outputfile%
If someone can look at this file and help me understand what went wrong I would appreciate it
I don't understand what did you mean by "No Longer Works" ? Please be more explicit when you ask a question !
here is a test and tell me if this works or not on your side and i will edit this aswer according to your response !
#echo off
set "outputfile=%~dp0UUDI.csv"
#for /f %%i in (
'wmic csproduct Get "UUID" /value ^& wmic bios get serialnumber /value'
) do (
#for /f %%j in ("%%i") do set "%%j" & echo "%%j"
)
echo UUID,SerialNumber>"%outputfile%"
echo %UUID%,%SERIALNumber%>>"%outputfile%"
If exist "%outputfile%" Start "" "%outputfile%" & Exit
The only reason I can see for your provided code to change its behavior, is that which was commented already by Mofi. That is, you've somehow caused the location of WMIC.exe to have been removed from the %Path% environment.
I have decided to provide an alternative method of achieving your goal using your chosen command utility WMIC.exe, and using its full path, to prevent such a reliance in future.
The WMIC command is traditionally one of the slower ones, so this method invokes it only once. All you should need to do is Echo your commands, currently on lines 12and 14, each separated as in line 13. If any of your commands requires to Get more than one property, you should separate those with caret escaped commas, e.g. Get Property1^,Property2. The results, (subject to line/environment length limitations), will then be saved to variables, %Title%, and %Record%, which can later be output to a file outside of the loop. Note: all commands should use /Value, or the more correct, /Format:List.
Example, (don't forget to adjust your output file path on line 4 as needed):
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "outputfile=Y:\HP\UUDI.csv"
Set "WMIC=%SystemRoot%\System32\wbem\WMIC.exe"
Set "FIND=%SystemRoot%\System32\find.exe"
Set "Title="
Set "Record="
For /F "Tokens=1,* Delims==" %%G In ('
(
Echo CSProduct Get UUID /Value
^&
Echo BIOS Get SerialNumber /Value
^)
^| %WMIC% ^| %FIND% "="
') Do (If Not Defined Title (Set "Title=%%G") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Title!") Do (EndLocal
Set "Title=%%I,%%G"))
If Not Defined Record (Set "Record=%%H") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Record!") Do (EndLocal
Set "Record=%%I,%%H")))
If Defined Title ( Echo %Title%
Echo %Record%) 1>"%outputfile%"

Remotely get serial number and model from list file and export it to arranged excel

I saw a nice script Here - this code get the Serial Number and Model from list of Remote Computers and export it to csv file.
The only problem is, that the export results have many separated lines instead of ONE line each computer.
I mean, i want to get this:
But i get that:
I also checked Here, but cant get it to work as described.
Here the Batch Code:
#echo off
del list.csv 2>nul
for /f %%i in (pclist.txt) do (
for /f "tokens=2 delims==" %%M in ('wmic /node:"%%i" csproduct get name /value') do for %%m in (%%M) do (
for /f "tokens=2 delims==" %%S in ('wmic /node:"%%i" bios get serialnumber /value') do for %%s in (%%S) do (
>>list.csv echo %%i, %%s, %%m
)
)
)
type list.csv
pause
How do i make the results to export to one line (display SN and model) to each computer?
You can have a go at this.
#echo off
break>list.csv
for /f "delims=" %%i in (pclist.txt) do call :method %%~i
type list.csv
goto :eof
:method
set "host_name=%~1"
for /f "tokens=2* delims==" %%N in ('wmic /node:"%~1" csproduct get name /value') do set "name=%%N"
for /f "tokens=2* delims==" %%S in ('wmic /node:"%~1" bios get serialnumber /value') do set "serial=%%S"
(echo %host_name%,%serial%,%name%)>>list.csv
We just get each of the values once, seeing as we do not incorporate for loops in for loops. Though we could have done this without setting variables, in this instance it makes it a little more readable.

Batch file get result into runtime variable

The following code is ment to get Chrome current version into a runtime variable. What am I missing here?
for /f %%i in ('wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get Version /value') do set VAR=%%i
echo %VAR%
pause
The result :
C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe - Invalid alias
verb.
Quoting gets a little different when inside a FOR command. Need the nested FOR to get rid of the empty lines.
#echo off
for /f "delims=" %%G in ('wmic datafile where "name='C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'" get Version /value ') do (
FOR /F "tokens=2 delims==" %%H in ("%%~G") do set var=%%H
)
echo %var%
pause

How to save a logfile with the serial number of a computer?

So this is what I have so far in my batch file. What I'm looking to do, is take the file that is created from the last line, 'serialnumber.txt' and rename it to the actual serial number of the computer. Maybe using the 'wmic bios get serialnumber' in some way?
wmic csproduct get identifyingnumber > %~d0\serial2.txt
%WINDIR%\SYSTEM32\msinfo32.exe /report %~d0\info2.txt
copy /b %~d0\serial2.txt + %~d0\info2.txt %~d0\serialnumber.txt
Related to the comment of SomethingDark you are looking for something like that :
How to save a logfile with the serial number of a computer ?
#echo off
Call :GetSerialNumber
echo %SerialNumber% & pause
%WINDIR%\SYSTEM32\msinfo32.exe /report "%~d0\%SerialNumber%.txt"
Start "" "%~d0\%SerialNumber%.txt"
Exit
::***********************************************
:GetSerialNumber
for /f "tokens=2 delims==" %%a in ('
wmic csproduct get identifyingnumber /value
') do for /f "delims=" %%b in ("%%a") do (
Set "SerialNumber=%%b"
)
exit /b
::***********************************************

How to get serial number from bios and compare it with a set of other serial number using Windows batch command?

I have a list of 100+ serial number.
What I want to do is: checking if the serial number of the computer I'm working on is in the list.
Is it possible to do this in Windows batch script ?
no need to iterate through every line in the file:
#echo off
for /F "tokens=2 delims==" %%s in ('wmic bios get serialnumber /value') do (
findstr "%%s" serials.txt
)
if %errorlevel%==0 ( echo Serialnumber found ) else ( echo Serialnumber not listed )
This command-line show the BIOS SerialNumber in my computer (Windows 8):
for /F "skip=1 tokens=3" %s in ('wmic bios list BRIEF') do echo %s
You may test it and adjust it until get what you want. For example:
#echo off
for /F "skip=1 tokens=3" %%s in ('wmic bios list BRIEF') do set serial=%%s
for /F "delims=" %%s in (serialList.txt) do if "%%s" equ "%serial%" goto found
echo Not found
goto :EOF
:found
echo OK
EDIT: I modified the program in order to include the serial numbers in the Batch file; I also used the modification in wmic parameters suggested by Stephan:
#echo off
for /F "tokens=2 delims==" %%s in ('wmic bios get serialnumber /value') do set serial=%%s
for %%s in (serial0 serial1 serial2 serial3 serial4 serial5 serial6 serial7 serial8 serial9
ser90 ser91 ser92 ser93 ser94 ser95 ser96 ser97 ser98 ser99 ser100 ser101) do (
if "%%s" equ "%serial%" goto found
)
echo Not found
goto :EOF
:found
echo OK
No need for a FOR loop if you use your list as the search strings and WMIC BIOS output as the target. I believe the serial number is always 10 characters, so no /I option is needed. But if the serial number length can vary, then the /I option is required due to a FINDSTR bug: Why doesn't this FINDSTR example with multiple literal search strings find a match?
wmic bios get serialNumber|findstr /blg:list.txt&&echo number found||echo number not listed
You can suppress the output of the serial number to the screen by redirecting the FINDSTR output to nul using >nul.

Resources