Search code examples
batch-filescriptingdelimiterwmic

Invalid Verb on WMIC get variable


I am trying to get just the primary hard disk serial number to a variable to be echoed later.

I have used this and it works:

for /f "tokens=2 delims== " %%k in ('WMIC path win32_physicalmedia get serialnumber /value') do (for /f "delims=" %%l in ("%%k") do set "SerialNumber=%%l")

However, it only does the last serial number and I want to get the primary.

If I used this command,

"wmic path win32_physicalmedia where tag='\\\\.\\PHYSICALDRIVE0' get serialnumber /value" 

then I get only the desired serial number.

Problem is when I try to modify the working code I get an error when running the bat:

Invalid Verb.

Trying to get to work:

for /f "tokens=2 delims== " %%k in ('wmic path win32_physicalmedia where tag='\\\\.\\PHYSICALDRIVE0' get serialnumber /value') do (for /f "delims=" %%l in ("%%k") do set SerialNumber=%%l)

Errors:

Invalid Verb

If I run just:

wmic path win32_physicalmedia where tag='\\\\.\\PHYSICALDRIVE0' get serialnumber /value in CMD

I get the following output:

SerialNumber=     WD-WCC2E1XJADRT

So I know the command works.

It doesn't even matter if the spaces are in, I just want whats after the =. I am doing this in WINPE with WMIC support, so Powershell isn't an option.

Total script

@echo OFF
setlocal
for %%v in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%v:\drives.txt set w=%%v

for /f "tokens=2 delims== " %%k in ('wmic path win32_physicalmedia where tag='\\\\.\\PHYSICALDRIVE0' get serialnumber /value)') do (for /f "delims=" %%l in ("%%k") do set SerialNumber=%%l)

for /F "tokens=2" %%i in ('date /t') do set "Date=%%i"
echo %SerialNumber%,%Date% >> %w%:\drives.txt

Solution

  • If you modify your command to this:

    WMIC Path Win32_PhysicalMedia Where "Tag='\\\\.\\PHYSICALDRIVE0'" Get SerialNumber /Value
    

    The = will be protected.

    Try it like this:

    @Echo Off
    For /F "Tokens=1*Delims==" %%A in ('
        WMIC Path Win32_PhysicalMedia Where "Tag='\\\\.\\PHYSICALDRIVE0'" Get SerialNumber /Value 2^>Nul
    ') Do For %%C In (%%B) Do Set "SerialNumber=%%C"
    Echo [%SerialNumber%] & Pause
    

    I've added the last line just to show any returned variable and value.