WMIC Returns an odd serial number

I have a strange problem. I am trying to return the serial number of a hard drive from WMIC to a variable in a batch file; line that gives me a variable:

for /f "skip=2 tokens=2 delims=," %%a in ('wmic diskdrive get serialnumber /FORMAT:CSV') do (set harddisk=%%a) 

This assigns what wmic sees as the serial number for the hard disk for the variable, the problem is that the serial number it returns is slightly disabled. In one example, it returns:

3ZVT01H4

But the serial number on the label:

Z3TV104H

It seems to take the actual serial number and reverse all two characters; Am I typing something wrong or is this a known (but not documented) problem?

Is there a way that I can take a variable, split it into two parts of characters, reverse the order of all these characters, and then put this in a variable?

+5
source share
3 answers

- It seems that it takes the actual serial number and reverses every two characters; Am I typing something wrong or is this a known (but not documented) problem?

The best I can understand is apparently a mistake . My characters also pair up, and it's not limited to wmic. Same result if I SELECT SerialNumber FROM Win32_DiskDrive from winmgmts:\\localhost\root\cimv2 in WSH.

β€œIs there a way I can take a variable, split it into two parts of characters, reorder all of these characters, and then put it in a variable?”

If you have PowerShell installed, you can do this with regexp replace . Here's the script package that invokes the PowerShell command to demonstrate:

 @echo off setlocal for /f "tokens=*" %%I in ('wmic diskdrive get serialnumber ^| findstr "[0-9]"') do ( echo reversed: %%I for /f "tokens=*" %%x in ('powershell -command "'%%I' -replace '(.)(.)','$2$1'"') do ( set "serial=%%x" ) ) echo corrected: %serial% goto :EOF 

I guess this qualifies as a duck punch . :)

In fact, if you just want to see the serial number without setting a variable, you can do this with a single-line PowerShell file:

 powershell "gwmi win32_diskdrive | %{ $_.serialnumber -replace '(.)(.)','$2$1' }" 
+4
source

All you need is some loops and basic substring operations.

I arbitrarily assume that the number cannot exceed 100 characters.

 @echo off set "disk=3ZVT01H4" echo Before: %disk% call :swapChars disk echo After: %disk% exit /b :swapChars var setlocal enableDelayedExpansion set "rtn=" for /l %%A in (0 2 100) do ( set /a "B=%%A+1" for %%B in (!B!) do ( if "!%~1:~%%B,1!" equ "" goto :break set "rtn=!rtn!!%~1:~%%B,1!!%~1:~%%A,1!" ) ) :break endlocal & set "%~1=%rtn%" exit /b 
+4
source

After upgrading the Windows 7 operating system to Windows 10, I had no problems if the serial number of the WD drive was displayed correctly on the hard drive when using: wmic diskdrive get model,name,SerialNumber,size So, Microsoft decided to change how the serial numbers of the physical disk is displayed.

Some useful background information. If someone runs a low-level ATA command to get information from the disk, every two characters of the model name and serial number are stored in the reverse order! This may be due to what is called a "little endian", or just how the ATA decided that they should be stored.

0
source

Source: https://habr.com/ru/post/1208363/


All Articles