adding the wmic bios get serialnumber to script - batch-file

I have the following script
set ip_address_string="IPv4 Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
#(Echo Status,Date,Time,Device,User,IP,SN & Echo Logon,%DATE%,%TIME%,%COMPUTERNAME%,%USERNAME%,%%f,%SerialNumber%))1>%RANDOM%.csv
goto :eof
The script works great but I need to capture the PC serialnumber within my script, for eg
for /F %%a in ('wmic bios get serialnumber') do call :Sub %%a
echo The serial number is %SerialNumber%
:Sub
if not "%*"=="" set SerialNumber=%*
So I can get the following "Logon,14/06/2021,22:03:18.28,LT6880,Testuser,192.168.0.46,B4Q4MN3"
but so far its been unsuccessful, can someone please point me in the correct direction?
Many Thanks
John

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

Defined variable not outputting in batch script

For asset stocktake purposes, I'm trying to create a script in batch that runs through the machine names of computers which are in a local .txt file, getting the last person who logged into the machine, and outputting that to a text file with the format of Machine Name: NAME | Last Login: USER
I've hit a bit of a wall and I can't figure out what's going on. The machine name is being correctly output but the 'last login' part is not. Here is my code:
#echo off
for /f %%a in (Assets.txt) do (
for /f %%b in ('wmic.exe /node:"%%a" computersystem get username') do set lastlogin=%%b
echo Machine Name: %%a Last Login: %lastlogin%)
pause
Any help would be greatly appreciated. Thank you.
Update 2
for /f "delims=" %%a in (Assets.txt) do (
for /f "skip=1 delims=" %%b in (
'WMIC /Node:"%%a" ComputerSystem Get UserName 2^>nul'
) do for %%c in (%%b) do echo Host: %%a Username: %%b >>Usernames.txt)
There really shouldn't be any need to set variables and therefore no need to enable delayed expansion.
You may be able to change your script to something like this:
#Echo Off
For /F "Delims=" %%A In (Assets.txt) Do (
For /F "Skip=1 Delims=" %%B In (
'WMIC /Node:"%%A" ComputerSystem Get UserName 2^>Nul'
) Do For %%C In (%%B) Do Echo Machine Name: %%A Last Login: %%B)
Pause
You may be able to so it like this too:
#Echo Off
For /F "Skip=1 Delims=" %%A In ('
"WMIC /Node:#Assets.txt ComputerSystem Get Name, UserName"
') Do Call :Sub %%A
Pause
Exit /B
:Sub
If Not "%1"=="" Echo Machine Name: %1 Last Login: %2

Get Windows version ,and edition, pass it to variable to use with findstr after

I'm noob it's my first question, but after searching tons of answers here I still can't quite get what I want. So the problem: need to get windows version ,and edition and pass it to variable to run findstr on it afterwards I get the variable with the edition however findstr can't use it to find string in txt file.
what I have done: I'm using this code to get the ver and save it as var
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
set winEdition=%%G
)
echo !winEdition!
endlocal
goto :eof
Output: Microsoft Windows 7 Enterprise
however if I run IF statement or findstr with !winEdition! var I get no result with IF and "string not found" with findstr also if I echo the var to a txt file I get "牣獯景⁴楗摮睯⁳‷湅整灲楲敳†਍" in the txt file so I think it's encoding problem, but I can't find a way to fix it.
More details: the full code suppose to take the var from the code above, search for the string in txt file and return the next line full code:
#echo OFF
setlocal EnableDelayedExpansion
set "winEdition="
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
set winEdition=%%G
)
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"!winEdition!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
rem Search for the lines
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
endlocal
goto :eof
The second part of the code works and tested with string and with var I want to automate it further. Thank you for help I am also open to alternative ways to get the result!
wmic prints some additional "empty" lines (1), which overwrites your value (watch with echo on)
#echo off
setlocal
set "winEdition="
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
if not defined winEdition set winEdition=%%G
)
echo %winEdition%
(1) on screen they appear to be empty, but technically they aren't. They contain a crippeled line break, which causes a lot of headache...
Hi again I found the answer!!! So I just used a different command to find the win version, and edition thanks to #LotPings, it's a bit slower than wmic but it works consistently every time.
Code:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=4-6" %%a in ('"systeminfo | find /i "OS Name""') do (
set "ver=%%a %%b %%c"
)
set "numbers="
for /F "delims=:" %%a in ('findstr /I /N /C:"!ver!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
if "%ver%" equ "Windows 10 Pro" echo YES
endlocal
goto :eof
Thank you all for help, and suggestions!
ps. the serials.txt is just a normal text file created with notepad encoding UTF-8 and the content is just:
Windows 10 Pro
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Windows 7 Pro...
and IF is just another check that it works in original code the problem is not with the second part nor with serials.txt because I couldn't compare the !winEdition! with IF statement.
BTW found the problem with my original solution thx to the one I found above
the problem was wrong token= solution:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=3-5 delims== " %%A in ('wmic os get Caption /value ^| find "Windows" ') do (
set "winVer=%%A %%B %%C"
)
echo !winVer!
set "numbers="
for /F "delims=:" %%a in ('findstr /I /N /C:"!winVer!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
endlocal
goto :eof

Batch - check if variable matches any item in a list

I've got a simple script that grabs a PC's manufacturer and sets a variable:
for /f "usebackq tokens=2 delims==" %%A IN (`wmic csproduct get vendor /value`) DO SET VENDOR=%%A
What I need to do next is to check if my new VENDOR variable matches anything in a list:
Acer
Gateway
Packard
Alienware
ASUS
Dell Inc.
Fujitsu
HP
Compaq
Lenovo
IBM
Samsung
Sony
Toshiba
and then DO COMMAND1 if found, and DOCOMMANDB if not found.
I think that piping the variable into findstr might work, but I only know findstr usage when you're feeding it a text file. I'm really bad at regex so I would need some guidance on that if it's the best option.
How can I accomplish the above?
If you pipe the Vendor to findstr /I /G:Vendorlist.txt and use conditional execution && for success and || for fail :
#Echo off
for /f "tokens=2 delims==" %%A IN (
'wmic csproduct get VENDOR /value'
) DO Set VENDOR=%%A
Echo:%VENDOR%|Findstr /I /G:VENDORList.txt >NUL 2>&1 &&(
Echo found %VENDOR% in List
)||(
Echo %VENDOR% not found in list
)
Slightly different approach. You could also put all the vendors in a list and read the list with a FOR /F.
#echo off
for /f "usebackq tokens=2 delims==" %%A IN (`wmic csproduct get vendor /value`) DO SET VENDOR=%%A
FOR %%G IN ("Acer"
"Gateway"
"Packard"
"Alienware"
"ASUS"
"Dell Inc."
"Fujitsu"
"HP"
"Compaq"
"Lenovo"
"IBM"
"Samsung"
"Sony"
"Toshiba") DO (
IF /I "%vendor%"=="%%~G" GOTO MATCH
)
:NOMATCH
echo Does not match
pause
GOTO :EOF
:MATCH
echo Does match
pause
GOTO :EOF
If you want to read the list where each entry is on its own line.
FOR /F "usebackq delims=" %%G IN ("filelist.txt") DO IF.......

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

Resources