|
本帖最后由 batche 于 2023-6-30 14:49 编辑
用wimc获取磁盘序列号,使用diskpart获取磁盘序ID
- @echo off
- %~1mshta vbscript:CreateObject("Shell.Application").ShellExecute("%~s0",":",,"runas",1)(window.close)&&exit
- for /f "skip=1 tokens=2-3" %%a in ('wmic diskdrive get BytesPerSector^,Index^,SerialNumber') do (
- echo,磁盘%%a 序列号:%%b
- (echo,select disk %%a
- echo,detail disk
- ) | diskpart | find /i "ID:"
- )
- pause
复制代码 补充一下:Win7通过wimc获取到的序列号是每两个字节经过小端排序的hex字符串,需要转码,我写了一个适用于win7的转码脚本- @echo off
- for /f "skip=1 tokens=2-3" %%a in ('wmic diskdrive get BytesPerSector^,Index^,SerialNumber') do (
- echo,磁盘%%a 序列号:
- powershell -c "$hexBytes=[regex]::Matches(\"%%b\",\".{4}\");$reversedHexBytes=foreach($hexByte in $hexBytes){$swappedByte=$hexByte.Value.Substring(2,2)+$hexByte.Value.Substring(0,2);$swappedByte -split '(..)'|?{$_}|%%{[System.Convert]::ToInt16($_,16)}};$filteredHexBytes=$reversedHexBytes|?{$_ -ne 32};$result=[System.Text.Encoding]::Default.GetString($filteredHexBytes);$result" 2>nul
- )
- pause
复制代码
|
|