I want a script that uses these options in CMD:
wmic bios get serialnumber
Then ask for a Vendor ie. HP or Dell.
Then the output should be something like vendor-serialnumber from wmic, then add a - in the serialnumber, like this:
YHT3-1234
the "-" should always be before the last 4 digits in the serialnumber,
so the final output would be something like :
HP-YHT3-1234
Here is another way to get it with PowerShell. Unless your organization has banned it, PowerShell is available on all supported Windows systems.
FOR /F "delims=" %A IN ('powershell -NoLogo -NoProfile -Command ^
"$x=Get-CimInstance CIM_BIOSElement;"
"'{0}-{1}-{2}' -f"
"#($x.Manufacturer, $x.SerialNumber.Substring(0,$x.SerialNumber.Length - 4),"
"$x.SerialNumber.Substring($x.SerialNumber.Length - 4))"') DO (SET "RESULT=%A")
ECHO RESULT is %RESULT%
Remember to double the PERCENT SIGN characters if this goes into a .bat/.cmd script.
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"$x=Get-CimInstance CIM_BIOSElement;"
"'{0}-{1}-{2}' -f"
"#($x.Manufacturer, $x.SerialNumber.Substring(0,$x.SerialNumber.Length - 4),"
"$x.SerialNumber.Substring($x.SerialNumber.Length - 4))"') DO (SET "RESULT=%%A")
ECHO RESULT is %RESULT%
Here's a quick example to show you how to introduce the hyphen/dash into your serial number, together with some additional work to auto detect the Manufacturer, and shorten to an identifier as necessary.
#Echo Off
SetLocal EnableExtensions
Set "Vendor="
Set "Serial="
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe BIOS Where "Manufacturer Like '%%Hewlett-Packard%%' Or Manufacturer Like '%%Dell%%' And SerialNumber Is Not NULL" Get Manufacturer^,SerialNumber /Format:MOF 2^>NUL') Do If Not Defined Vendor (Set "Vendor=%%G") Else Set "Serial=%%G"
If Not Defined Vendor GoTo :EOF
If "%Vendor:Dell=%" == "%Vendor%" (Set "Serial=HP-%Serial:~,-4%-%Serial:~-4%") Else Set "Serial=Dell-%Serial:~,-4%-%Serial:~-4%"
Echo %Serial%
Pause
Obviously if there are a lot of Manufacturers then additional work would need to be done.
As an unnecessary courtesy, here is the same methodology, just this time affording end user the opportunity to break the code, or corrupt the result:
#Echo Off
SetLocal EnableExtensions
Set "Serial="
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe BIOS Where "SerialNumber Is Not NULL" Get SerialNumber /Format:MOF 2^>NUL') Do Set "Serial=%%G"
If Not Defined Serial GoTo :EOF
Set /P "Vendor=Type the name of the Computer Manufacturer, and press ENTER>"
Echo %Vendor%-%Serial:~,-4%-%Serial:~-4%
Pause
Based upon a very limited test of one HP notebook and the comments by KJ below, the following idea may also be useful:
#Echo Off
SetLocal EnableExtensions
Set "Serial="
Set "Vendor="
For /F Tokens^=2^,4^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
CSProduct Assoc:List 2^>NUL') Do Set "Serial=%%G" & For /F %%I In ('
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile
"-Join '%%H'.ToUpper()[0..3]"') Do Set "Vendor=%%I"
If Not Defined Serial GoTo :EOF
Echo %Vendor%-%Serial:~,-4%-%Serial:~-4%
Pause
This uses PowerShell just to return up to the first four characters in upper case from the string returned as the Name value from the CSProduct alias of WMIC. So Compaq would return COMP, Dell as DELL, Lenovo as LENO Samsung as SAMS etc. This would then be prefixed to the serial number, (split as before), which in my test was returned as the ComputerSystemProduct IdentifyingNumber.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
Please help me to parse something to a CSV. I already know how to do it using Powershell, but I need to use a batch file.
I need to get output like this:
datetime;hostname;username1;username2;
where username1 username2 etc are the user names from the local administrators group.
I made this:
#echo off
echo %DATE%
echo %TIME%
set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetimef%
for /f "tokens=* skip=6" %%a in ('net localgroup Administrators') do (
echo %%a >> "c:\temp\%datetimef%.csv")
pause
However the output file name is:
2021_ 1_Fr__11_42_18.csv
And the date returned is:
Administrator
Usr1
The command completed successfully
How can I remove The command completed successfully from the resulting output file?
It is easy enough to do this in a .bat batch-file run by cmd. If you are on a supported Windows system, PowerShell will be available.
powershell.exe -NoLogo -NoProfile -Command ^
$DatetimeF = Get-Date -Format 'yyyy_MM_ddThh_mm_ss'; ^
$Users = (Get-LocalGroupMember -Name Administrators).Name; ^
$DatetimeF + ';' + $Env:COMPUTERNAME + ';' + ($Users -join ';') ^| ^
Out-File -FilePath ($DatetimeF + '.csv')
I suppose you want something like this?
#echo off
setlocal enabledelayedexpansion
set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
set "var=%datetimef%"
for /f "skip=6delims=" %%a in ('net localgroup Administrators ^| findstr /v "successfully"') do set "var=!var!;%%a"
(echo %var%)>>"c:\temp\%datetimef%.csv"
The main problem with the output you have reported, is that it appears to have assumed the returned output of your %DATE% and %TIME% variables. Those values can be configured differently for the locale, machine and/or logged in user, and in your case does not match the %DATE% assumption of dd#MM#yyyy, (where # represents the item sepatators). Yours is clearly either ddd d#M#yyyy, ddd dd#MM#yyyy, ddd M#d#yyyy. or ddd MM#dd#yyyy
There aremany questions and answer already on this site which show methods of getting the date and time using a none locale or otherwise configured format, the most useful, non PowerShell method of doing that uses the built-in WMIC.exe utility.
Just for something a little bit different, i.e. not using net.exe at all, and instead using WMIC.exe:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "{="
Set "}="
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Get CSName^,LocalDateTime /Format:MOF 2^>NUL') Do If Not Defined { (Set "{=%%G") Else (SetLocal EnableDelayedExpansion
Set "}=%%G"
For %%H In ("!}:~,4!_!}:~4,2!_!}:~6,2!__!}:~8,2!_!}:~10,2!_!}:~12,2!;!{!") Do (EndLocal
Set "{=%%~H"))
For /F "Tokens=1,* Delims==" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Group Where "Name='Administrators'" Assoc:List /ResultClass:Win32_UserAccount 2^>NUL ^| %SystemRoot%\System32\findstr.exe "^Name="') Do For /F "Tokens=*" %%I In ("%%H") Do If Not Defined } (Set "}=%%I") Else (SetLocal EnableDelayedExpansion
For %%J In ("!}!") Do (Endlocal
Set "}=%%~J;%%I"))
Set "CSVBaseName=%{:;="&:"%"
(Echo %{%;%}%;) 1>"C:\Temp\%CSVBaseName%.csv"
If you wanted to still use net.exe, i.e. 'a little bit different' was not what you wanted, then the following should perform the same task:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "{="
Set "}="
For /F Delims^=^ EOL^= %%G In ('%SystemRoot%\System32\net.exe LocalGroup
Administrators 2^>NUL') Do (If Not Defined { (Set "{=%%G") Else (
SetLocal EnableDelayedExpansion
For /F Delims^= %%H In ("!{!") Do (EndLocal
Set "{=%%H;%%G"
Set "}=%%H"))
Set /P "=%%G" 0<NUL | %SystemRoot%\System32\findstr.exe "^\-\-*$" 1>NUL && (
Set "{="))
If Not Defined } GoTo :EOF
For /F Delims^=^ EOL^= %%G In ('%SystemRoot%\System32\HOSTNAME.EXE'
) Do Set "{=%%G;%}%"
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Get
LocalDateTime /Format:MOF 2^>NUL') Do Set "}=%%~nG"
Set "}=%}:~,4%_%}:~4,2%_%}:~6,2%__%}:~8,2%_%}:~10,2%_%}:~-2%"
(Echo %}%;%{%;) 1>"C:\Temp\%}%.csv"
The differences with this being, that it does not assume a specific number of lines to skip, it does not expect the end user language to be English, it uses the reported HostName, as opposed to the ComputerName, (which may not necessarily match), and once again does not rely on the end users computer configuration for defining the date and time string.
thank you all! finally i solved with all your help and came to this:
#echo off
setlocal enabledelayedexpansion
rem set mydate=%:/date:/=%
rem set mytime=%time::=%
rem set mytimestamp=%mydate: =_%_%mytime:.=_%
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
set mytimestamp=%mydate%_%mytime%
for /f "skip=6delims=" %%a in ('net localgroup Administrators ^| findstr /v "successfully"') do (
set param=!param!%%a;)
echo %mytimestamp%;%computername%;!param! >> "c:\temp\%mydate%.csv"
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
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%"
This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed 2 years ago.
I'm trying to write a script but I'm really bad at writing the batch scripts. I'm trying to create a script that recognizes local hard drives and puts the letter of those hard drives in a variable, and after that I use that variable in another for loop to decrypt the hard drive if it's encrypted.
Example:
:check
for /f "tokens=2 delims==" %%d in ('wmic logicaldisk where "drivetype=3" get name /format:value') do (
set vvv=%%d
for /f "tokens=1,*" %%A in ('manage-bde -status %vvv% ^| findstr Conversion') do set var1=%%B
Rem Try to find if value is Encrypted or not
echo %vb1%|find "Encrypted" >nul
if errorlevel 1 ( goto :check) else ( goto :decrypt_c))
#Echo Off
:check
for /f "tokens=2 delims==" %%d in ('wmic logicaldisk where "drivetype=3" get name /format:value') do (
echo %%d
manage-bde -off %%d )
pause
That code works fine until echo %%d, when I try to put it into manage-bde command does not work...When I have for example 3 hard drives, I will do loop and turn off BitLocker for all partitions. Thanks!
Perhaps you'd be better off using the Win32_EncryptableVolume Class.
This example will create variables, e.g. %EncryptedDriveLetter[1]%, %EncryptedDriveLetter[2]%, %EncryptedDriveLetter[3]% etc., with the respective content of e.g. C:, E:, F:
#Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^== %%G In ('2^> NUL Set EncryptedDriveLetter[')Do Set "%%G="
For /F Delims^= %%G In ('^""%SystemRoot%\System32\wbem\WMIC.exe" ^
/NameSpace:\\root\CIMv2\Security\MicrosoftVolumeEncryption Path ^
Win32_EncryptableVolume Where ^
"ConversionStatus!='0' And EncryptionMethod!='0' And VolumeType<'2'" ^
Get DriveLetter 2^> NUL ^| "%SystemRoot%\System32\find.exe" ":"^"'
)Do (Set /A i+=1 & SetLocal EnableDelayedExpansion
For %%H In (!i!) Do EndLocal & Set "EncryptedDriveLetter[%%H]=%%G")
Set EncryptedDriveLetter[ & Pause
Notes: This must be run 'As administrator', and the last line is included just to provide some visual output. You would of course, replace that, with the rest of your script.
Just to be sure that you understand why I provided this methodology; if you were simply wanting to decrpt the encrypted drives, you don't need a for loop, or variables or manage-bde. You would just change the Get method to Call and use Decrypt.
For example:
#"%SystemRoot%\System32\wbem\WMIC.exe" /NameSpace:\\root\CIMv2\Security\MicrosoftVolumeEncryption Path Win32_EncryptableVolume Where "ConversionStatus!='0' And EncryptionMethod=!'0' And VolumeType<'2'" Call Decrypt
Just to mention, if the protection status prior to decryption was 1, i.e. Protection On, upon successful completion the protection status will be changed to 0, i.e. Protection Off.
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.