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

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
::***********************************************

Related

Batch Script to Search for Softwares by Vendor to a CSV file

I want a batch script to search for software's on a computer by a specific vendor and save the results to a CSV file.... I am tried it with the following script, but it always returns NULL values, not able to figure out the issue.
#echo off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
:Sub
Set "serial=%*"
Set "LogFile=%serial%.CSV"
If Exist "%LogFile%" Del "%LogFile%"
wmic /Append:"%LogFile%" product where "Name like '%Microsoft%'" get Name, Version /Format:CSV
Start "" "%LogFile%"
exit
NB : % is needed to be used as wildcard
In this case you should add another percent character % to %Microsfot% to escape the wilcard of WMIC in your batch and become like %%Microsoft%%
#echo off
Title Batch Script to Search for Softwares by Vendor to a CSV file
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
:Sub
echo(
echo( Please wait a while ... Searching for installed softwares ....
Set "serial=%*"
Set "LogFile=%serial%.CSV"
If Exist "%LogFile%" Del "%LogFile%"
wmic /Append:"%LogFile%" product where "Name like '%%Microsoft%%'" get Name, Version /Format:CSV
Start "" "%LogFile%"
Exit

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%"

wmic batch file for network scan

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"

Get cpu utilization percentage using batch script

I have executed the below command in command prompt.
Input:
C:\>#for /f "skip=1" %p in ('wmic cpu get loadpercentage') do #echo %p%
8%
%
I have written the code to get only 8% in batch script like below,
call :Harddisk
call :CPU
Echo ^</TABLE^> ^</BODY^> ^</HTML^> >> %opfile%
pause
exit /b
:CPU
set wmih=#for /f skip=1 %p in ('wmic cpu get loadpercentage') do #echo %p% |Find /c /v ":"
ECHO wmih
for /f "tokens=1,2,3,4,5" %%a in ('"%wmih%"') do (
Echo ^<TR^> ^<TD^> >> %opfile%
Echo CPU Utilization: %%a >> %opfile%
Echo ^</TD^> ^</TR^> >> %opfile%
)
exit /b
I'm not getting proper output. Please help me to sort it out.
Thanks!!!
for /f "tokens=2 delims==" %%i in ('wmic cpu get loadpercentage /value ^|find "="') do echo set p=%%i
set p=%p:~0,-1%
echo %p%
Just to keep your structure
:CPU
set "wmih=#for /f "usebackq skip^^^=2 tokens^^^=2 delims^^^=^^^," %%p in (`wmic cpu get loadpercentage^^^,version /format^^^:csv`) do #echo(%%p%%"
ECHO %wmih%
for /f "tokens=1,2,3,4,5" %%a in ('"%wmih%"') do (
>>%opfile% Echo ^<TR^> ^<TD^>
>>%opfile% Echo CPU Utilization: %%a
>>%opfile% Echo ^</TD^> ^</TR^>
)
To be able to store the command inside a variable and then pass it to the for command, that will spawn another cmd instance to execute the command, you need a lot of scape characters just to handle the command execution.
It can be made easier
:CPU
for /f "skip=2 tokens=2 delims=," %%a in (
'wmic cpu get loadpercentage^,version /format^:csv'
) do (
>>%opfile% Echo ^<TR^> ^<TD^>
>>%opfile% Echo CPU Utilization: %%a%%
>>%opfile% Echo ^</TD^> ^</TR^>
)
In both cases, to remove the aditional CR character at the end of the retrieved value, the query request the output in csv format with an aditional field, so, the needed data is not at the end of the line and the value will not include the ending CR

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