Make batch omit lines from a file - batch-file

I'm creating a batch fingerprinter, and I'm using a mix of SystemInfo and IpConfig to get a unique sha256 hash for a computer, the problem is i the systeminfo output there are things like boot time and others that change every time, and i need a way of omitting them before i generate the hash.
my current whole code is:
#echo off
systeminfo >>fingerprint.txt
echo - - - - - - - - - - - - >> fingerprint.txt
ipconfig >>fingerprint.txt
:: Here i want to have the omitter
powershell Get-FileHash fingerprint.txt -Algorithm SHA256 > comphash.txt
pause

Like pretty much all information about your computer, the computer's unique identifier can be found in wmic. In this case, it's the UUID property of the csproduct class, which handles computer system product information from SMBIOS.
wmic has some extra carriage returns at the end of the data so there's a bit of tweaking that needs to be done before you can just run it through a for /f loop like a normal command, but it will look like this:
for /f "delims=" %%A in ('wmic csproduct get UUID /value ^| find "="') do set %%A
This will have the wmic command output its data in the format UUID=00000000-0000-0000-0000-000000000000 and then set the UUID to a variable called %UUID%, using find to look for the = and removing the excess carriage returns.

Found an answer thanks to Squashman, final code in case someone wants to use it is:
#echo off
color 33
systeminfo | find "Domain:" /v | find "System" /v | find "Virtual" /v | find "Available" /v >> fingerprint.txt
echo Your computers unique fingerprint: >> finalhash.txt
powershell Get-FileHash fingerprint.txt -Algorithm SHA256 >> finalhash.txt
del fingerprint.txt
start finalhash.txt
pause

Related

CMD - How do I run a command twice in one line? [duplicate]

This question already has answers here:
Can I search for multiple strings in one "find" command in batch script?
(3 answers)
Closed 4 days ago.
How do I display both the host name and boot time?
The code I'm trying:
systeminfo | find /i "host name" && "boot time"
I know I could use
systeminfo | find /i "host name"
systeminfo | find /i "boot time"
but I really don't want to.
Basically I want the command to display the host name and boot time and ignore all else. How do I do that on one line? I know I'm missing something super simple here.
I have faced a similar problem before, and I may be able to help you out. If I understand your question correctly, you're looking for a way to display both the host name and boot time on one line, using a single prompt. In that case, the following command should do the trick, ignore the curly brackets of course:
{systeminfo | find /i "host name" & systeminfo | find /i "boot time"}
Using the & character will execute the two systeminfo commands sequentially and output the results on the same line.
I wish you the best of luck with your project. Let me know if I can help with anything else.
I would strongly advise against running the slowest utility on Windows, systeminfo.exe, just to get this type of information.
You could, if you wish try to use a less intensive, and quicker utility, net.exe.
You should also try to not to expect utilities to output strings in a specific language.
Example:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "CN=" & For /F "EOL= Delims=" %%G In ('(%SystemRoot%\System32\net.exe
Stats Srv 2^>NUL ^|^| %SystemRoot%\System32\net.exe Stats Work^) ^|
%SystemRoot%\System32\findstr.exe /ELV "."') Do If Not Defined CN (
Set "CN=%%~nxG") Else For %%H In (%%G) Do (SetLocal EnabledelayedExpansion
For %%I In ("!BT!") Do EndLocal & Set "BD=%%~I" & Set "BT=%%H")
If Defined CN Echo %CN% was booted on %BD% at %BT%& Pause

Having trouble storing output of "wmic csproduct get vendor /format:list | findstr/c=" as a variable [duplicate]

This question already has answers here:
Batch WMIC redirecting output and wrapping into variable
(5 answers)
Closed 2 years ago.
Thank you for checking out my question.
I am trying to:
get the output of a wmic query into a variable
Here is some background information for why I need to get this accomplished.
The policy at my job requires that laptops and tablets equipped with webcams have them disabled in the BIOS.
Now that we are teleworking we have a need to enable the webcams in the BIOS.
We have tools that can be used to enable BIOS features while the system is running.
I want to write a script that will psexec into a machine, fetch the vendor information and store it in a variable that can be accessed later in the script once psexec exits.
I can write the rest of my script without issue I think, however I'm having trouble figuring out how to store the results of the following command in a variable:
wmic csproduct get vendor /format:list | findstr/c=
For example, the output of that command on my machine is:
Vendor=Dell Inc.
I want to capture the output and store it in a variable for later use in the script I'll be writing so that I can process vendor specific BIOS manipulation.
I have tried
set sysvend=wmic csproduct get vendor /format:list | findstr/c=
But when I echo %sysvend% to see if it worked I get the following instead of what I wanted:
wmic csproduct get vendor /format:list | findstr/c=
If anyone knows how to do this I would greatly appreciate the help. I am open to other alternatives for determining the vendor.
Even easier than using find or findstr and without the problematic 0x0D0D0A line endings:
#Echo Off & SetLocal EnableExtensions
For /F Tokens^=6Delims^=^" %%G In (
'%__APPDIR__%wbem\WMIC.exe CSProduct Get Vendor /Format:MOF 2^>NUL'
) Do Set "vendor=%%G"
This solution also prevents unwanted trailing characters with the vendor name string, which can be a common occurrence.
There is a known wmic bug, so you need to parse wmic output using two nested loops, e.g. as follows:
FOR /F "tokens=1* delims==" %%s IN ('
wmic csproduct get vendor /value ^| find "="
') DO for /F "tokens=*" %%i in ("%%t") do SET "vendor=%%i"
Here the for loops are
%%s to retrieve the vendor value;
%%i to remove the ending carriage return in the value returned (wmic behaviour): each output line ends with 0x0D0D0A (CR+CR+LF) instead of common 0x0D0A (CR+LF).
See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem
Answer posted to demonstrate the command can be assigned and executed as a variable, Contrary to Neko's answer, as per the OP's question.
#Echo Off
Set Get.Vendor=^>"%TEMP%\Vendor.log" (wmic csproduct get vendor /value ^|
findstr /c:=) ^&^& (For /F "UsebackQ Tokens=2* Delims==^EOL" %%A In ("%TEMP%\Vendor.log") Do For /F "Delims=" %%T In ("%%A") Do Set "Vendor=%%~T") ^&^& Echo.!Vendor!
Setlocal EnableDelayedExpansion
%Get.Vendor%
pause
Use for %%i in (wmic csproduct get vendor /format:list ^| findstr/c=) do (set var=%%~I)
What this does is that the for loop variable %%i becomes the output of the command in the for loop which allows you to set another variable the for loop variable. You need the caret (^) before the | in the for loop since if you didn't, the command would pipe out of the for loop and not execute in the for loop. You could also just surround the command with double quotes (") and you wouldn't need to escape the pipe. As a result of the command, %var% would become equal to the output of your wmic command.
The final command will be:
#echo off
for /f "tokens=* delims==" %%i in (' wmic csproduct get vendor /format:list ^| findstr /c:"=" ') do (set var=%%~i)
echo %var%
rem echoing var to make sure the variable is what you wanted
Output for me:
Vendor=Microsoft Corporation
If you want the output to be Microsoft Corporation specifically, you can add:
set var=%var:~7%
to remove the first 7 characters. You can also change tokens to 2* instead of *. :
for /f "tokens=2* delims==" %%j in (' wmic csproduct get vendor /format:list ^| findstr /c:"=" ') do (set var=%%~j)
Outputs:
Microsoft Corporation
Notes:
In batch-files, unlike powershell, you cannot set a variable as a command and have it become the output which is why the conventional way to set output of a command to a variable is using the for loop. Also, just preference, I would suggest using the /value switch rather than /format:list.
Tested and works for me

Store the UUID and IP of a machine in a file through batch file

I need to generate a text file with data of the UUID and IP of a machine through batch file, something like this:
CAA8A570-86FF-81E4-3398-0071C21A28CE 192.168.0.0
I used these commands but I can't figure out how to put the info in the same file.
wmic csproduct get "UUID" > C:\UUID.txt
ipconfig /all | find /i "phy" > C:\MAC.txt
It is possible to get the IP address and the UUID of a computer through batch file. However, it is unclear what you want to do with them. So, I will show you the way to get them and I will store their value in a variable. Do whatever you want then:
#echo off
rem Script to get the UUID and IP address of this computer.
rem Get IP address:
for /F "tokens=2 delims=(:" %%A IN ('ipconfig /all ^| findstr /c:"IPv4"') do (
for /F "tokens=*" %%B IN ("%%A") do (
rem Set the IP address into the IP_address variable:
set "IP_address=%%B"
)
)
rem Get UUID:
for /F %%A IN ('(wmic csproduct get "UUID"^) ^| findstr /c:"-"') do (
rem Set the UUID in the UUID variable:
set "UUID=%%A"
)
rem Echo the results
echo We have found that this computer has a UUID of %UUID%.
echo We have also found the IP address of this computer. It is: %IP_address%
If you want to redirect them to a file in the format mentioned in your question, use:
(echo %UUID% %IP_address%)>filename.txt
And if you want to append them, use:
(echo %UUID% %IP_address%)>>filename.txt
You may understand better how my code works, if I show you the output of each command processed.
ipconfig /all | findstr /c:"Ipv4":
The output of it, is (for me):
IPv4 Address. . . . . . . . . . . : xxx.xxx.x.x(Preferred)
I am not sure if you are getting these parenthesis.
The for /F loop which processes the command has tokens=2 and delims=(: options.
delims=(: option means not to parse into tokens the characters ( and :. The tokens then, will be (separated by |): IPv4 Address. . . . . . . . . . . | xxx.xxx.x.x|Preferred). I have removed the characters (: as you can see. The token which contains the IP address is the second one, so I select it using:
tokens=2 option.
Because there is one space in its start, I make another loop in this to remove the spaces in the start, using tokens=* option.
Finally, the IP address is set into the IP_address variable and it is ready for use!
(wmic csproduct get "UUID") | findstr /c:"-":
The output of this command is just the UUID (no additional loops here):
F5A91381-529E-11CB-A155-BF406BD05412
So, I just set it to a variable (UUID) and it is ready for use!
For seeing what 'redirect' and what 'append' means, I suggest the following links for reading:
https://www.robvanderwoude.com/redirection.php
https://ss64.com/nt/syntax-redirection.html
Also, I suggest opening a cmd, typing the following commands and reading carefully their output:
for /?
wmic /?
findstr /?
rem /?
set /?
echo /?
which will surely help you understand my code.

batch counting findstr results for /f

I have an apache.log file. Im trying to make a batch file to be able to count the total amount of logins. Basically a number of lines.
My initial idea was to set a variable results=0 and whenever findstr command gets a result i get +1 to variable value and in the end display the value. Dont know if thats the correct way of thinking.
so far i've got the impression for /f command will probably be the key, though i've never used it so many parts are unclear to me.
here is the example of .log file line
67.195.112.96 - - [22/Feb/2010:00:06:03 +0200] "GET /www/kurpiai/dalyviai/?did=118 HTTP/1.0" 200 41119 "-" "Mozilla/5.0 (compatible; Yahoo! Slurp/3.0; http://help.yahoo.com/help/us/ysearch/slurp)"
my attempt so far goes as follows:
findstr ^[1-9] apache.log
for /f %%G in ('findstr ^[1-9] apache.log') do echo result %%G
now i understand that in this case %%G value gets replaced with each findstr result. and with this i get echo of every line that matches findstr and after that every ip adress. why?
i believe maybe somehow i could use the fact that %%G value changes every time to set my own variable. why does %%G get an ip value exactly?
or maybe im wrong and i dont need for /f for this task at all?
I'd say you don't. Just combine your findstr command with the DOS style wc -l:
findstr "^[1-9]" abc.txt | find /v "" /c
The first part selects the lines which match your intentions, and the second part counts (/c) those lines not matching nothing (i.e., all).
edit:
If you need the result in a variable:
for /f %%a in ('findstr /R "^[1-9]" abc.txt ^| find /c /v ""') do set "count=%%a"

batch that return drive letter of USB with specific volume name

I do not have much experience with batch an d need a help with batch script.
Task is, return drive letter as parameter to %disk_letter%
Idea is use this for search:
WMIC LogicalDisk Where VolumeName='MY_USB' Get /Format:list | FIND "Caption="
I have "Caption=G:" as the result. I need that %disk_leter% parameter was equal just "G:"
Need help to finish this script.
Thank you!
On Linux right now but here's what I think you'll need to do. Part 1: save the result of your FIND command to a variable, and 2: take a substring of the variable. The second part is simple, so I'll start with that (assuming that in the first step you named your variable var
#echo %var:~-2%
That's about as far as I'm comfortable in batch, so this next bit is cobbled together:
To store the result of your find as a variable, try amending your code to:
set cmd="WMIC LogicalDisk Where VolumeName='MY_USB' Get /Format:list | FIND "Caption=" "
FOR /F %%i IN (' %cmd% ') DO SET var=%%i
and then (remember above) output it with:
#echo %var:~-2%
The related question from which I am cobbling together the second part is this question so if this doesn't work as expected I would jump over to that one first.
Here goes...
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype
2^>NUL`) do (
if %%l equ 2 (
echo %%i is a USB drive.
)
)

Resources