Computer Hope

Microsoft => Microsoft DOS => Topic started by: Abo-Zead on December 29, 2020, 06:34:32 PM

Title: Collect all network PCs Serial Numbers inside text file by using batch?
Post by: Abo-Zead on December 29, 2020, 06:34:32 PM
Hi all,

I want to get all PCs "serial numbers" over the network by using batch as I know this command "wmic /node:%hostname% bios get serialnumber /value"  works properly separately but I want to but this command to work under for loop and for loop takes the hostnames from external text file then export the result to another text file arranged as follows " Hostname: Serial number"
Can anyone help, please?
Title: Re: Collect all network PCs Serial Numbers inside text file by using batch?
Post by: Hackoo on December 30, 2020, 06:03:20 AM
Your request is like this thread : Reading text file line by line and do some operations - Windows Batch File (https://www.computerhope.com/forum/index.php/topic,161111.0.html)
You just need to add some tweaks when you call the Sub :Action

Get-Serial-Numbers-Hosts.bat
Code: [Select]
@echo off
Title Collect all network PCs Serial Numbers inside text file by using batch
set "InputFile=%~dp0Hosts.txt"
set "OutPutFile=%~dp0serial_numbers.txt"
If Exist "%OutPutFile%" Del "%OutPutFile%"

If Not Exist "%InputFile%" (
color 0C & echo "%InputFile%" does not exist. Please check it first !
Timeout /T 8 /NoBreak>nul & Exit
)

set /a count=0
SETLOCAL enabledelayedexpansion
for /F "tokens=* delims=" %%a in ('Type "%InputFile%"') do (
Set /a count+=1
Set "Host[!count!]=%%~a"
)

For /L %%i in (1,1,%Count%) Do (
Call :Action !Host[%%i]!
Call :Action !Host[%%i]!>>"%OutPutFile%"
)
If Exist "%OutPutFile%" Start "" "%OutPutFile%" & Exit
::--------------------------------------------------------------------------------------
:Action
for /f "skip=1 delims=" %%a in ('wmic /node:"%1" bios get serialnumber /value') do (
for /f "tokens=2 delims==" %%b in ("%%a") do (
echo %1 : %%b
)
)
exit /b
::--------------------------------------------------------------------------------------
Title: Re: Collect all network PCs Serial Numbers inside text file by using batch?
Post by: Abo-Zead on December 31, 2020, 09:53:07 AM
Thank you, bro, for your help and I tried to test it on my PC and it works, and also I'll test your batch in the real environment the next week Then feedback to you.
but I think it will work perfectly as usual from you as you are a prof in this field.
, so Could you help me with this case  https://www.computerhope.com/forum/index.php/topic,179675.msg1006525.html#msg1006525