[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[系统相关] [已解决]引导方式-分区表类型-空间占用-接口协议

本帖最后由 ygqiang 于 2025-2-13 22:05 编辑

[已解决]引导方式-分区表类型-空间占用-接口协议
请直接看6楼代码

磁盘分区容量-类型分析,gpt类型判断为mbr,如何修改?
win10系统,磁盘0为mbr,磁盘1为gpt。
bat文件编码保存为ANSI,运行代码后,分区表类型均显示mbr
  1. @echo off
  2. echo ——————————————————————————————————————— >nul 2>nul
  3. if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  4. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
  5.     more +%%i "%~f0" > c:\diskinfo.vbs
  6. )
  7. ping 127.0.0.1 -n 2 >nul 2>nul
  8. cls
  9. cscript /nologo c:\diskinfo.vbs
  10. ping 127.0.0.1 -n 2 >nul 2>nul
  11. CScript /NoLogo c:\diskinfo.vbs>c:\diskinfo.txt
  12. ping 127.0.0.1 -n 2 >nul 2>nul
  13. cls
  14. Type c:\diskinfo.txt
  15. ping 127.0.0.1 -n 15 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  16. echo ——————————————————————————————————————— >nul 2>nul
  17. if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  18. set "vbs=c:\diskinfo.vbs"
  19. set "log=c:\diskinfo.log"
  20. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (more +%%i "%~f0" > "%vbs%")
  21. CScript //NoLogo "%vbs%" > "%log%"
  22. cls
  23. type "%log%"
  24. ping 127.0.0.1 -n 5 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul) & exit/b
  25. goto :eof
  26. echo ——————————————————————————————————————— >nul 2>nul
  27. :DynamicScript
  28. Main
  29. Sub Main()
  30.   Dim objWMI, colDisks
  31.   Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  32.   Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
  33.   ' 获取硬盘最大序号
  34.   nIndexMax = 0
  35.   For Each objDisk in colDisks
  36.     If Cint(objDisk.Index) > nIndexMax Then nIndexMax = Cint(objDisk.Index)
  37.   Next
  38.   ' 按序号取信息
  39.   For i = 0 To nIndexMax
  40.     For Each objDisk in colDisks
  41.       ' 只取该序号信息
  42.       If objDisk.Index = i Then
  43.         s = s & "硬盘" & objDisk.Index & ":" & vbTab
  44.         s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  45.         s = s & "型号:" & objDisk.Caption & vbTab
  46.         s = s & "分区表:" & IsGPT(objDisk.Index) & vbCrLf
  47.         strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  48.         Set colPartitions = objWMI.ExecQuery _
  49.             ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  50.             & " where AssocClass=Win32_DiskDriveToDiskPartition")
  51.         For Each objPartition in colPartitions
  52.           strPartId = objPartition.DeviceID
  53.           Set colLogicalDisks = objWMI.ExecQuery _
  54.               ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId _
  55.               & """}  where AssocClass=Win32_LogicalDiskToPartition")
  56.           For Each objLogicalDisk in colLogicalDisks
  57.             size = objLogicalDisk.Size
  58.             free = objLogicalDisk.Freespace
  59.             used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  60.             s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  61.             s = s & "大小:" & GetSize(size) & vbTab
  62.             s = s & "剩余:" & GetSize(free) & vbTab
  63.             s = s & "使用率:" & used & vbCrLf
  64.           Next
  65.         Next
  66.         s = s & vbCrLf
  67.       End If
  68.     Next
  69.   Next
  70.   WScript.Echo s
  71. End Sub
  72. ' 格式化
  73. Function GetSize(intSize)
  74.   If intSize/1024/1024 > 1024 Then
  75.     GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  76.   Else
  77.     GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  78.   End If
  79. End Function
  80. ' 获取指定硬盘的分区表类型(GPT/MBR)
  81. Function IsGPT(ByVal nDiskIndex)
  82.   IsGPT = "MBR"
  83.   Dim wso, sLogFile, sText
  84.   Set wso = CreateObject("WScript.Shell")
  85.   sLogFile = wso.ExpandenVironmentStrings("%temp%\diskpart.log")
  86.   wso.Run "cmd /c ""chcp 437 & cls & (echo list disk | diskpart | find /i ""Disk " & nDiskIndex & """) >""" & sLogFile & """ "" ", 0, False
  87.   Call TxtFile(sLogFile, 1, -2, sText)
  88.   If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
  89. End Function
  90. ' 对文本指定编码进行读写操作2
  91. 'nRW: 1只读, 2只写, 8追加 'nCharset: -2(系统), -1(Unicode), 0(ASCII)
  92. Sub TxtFile(ByVal FileName, ByVal nRW, ByVal nCharset, ByRef sText)
  93.   Dim fso :  Set fso = CreateObject("Scripting.filesystemobject")
  94.   If sText <> "" And (nRW = 2 Or nRW = 8) Then
  95.     fso.OpenTextFile(FileName, nRW, True, nCharset).Write sText
  96.   ElseIf fso.FileExists(FileName) And nRW = 1 Then
  97.     If fso.GetFile(FileName).Size > 0 Then _
  98.       sText = fso.OpenTextFile(FileName, nRW, False, nCharset).ReadAll
  99.   End If
  100. End Sub
  101. ' 以管理员身份运行
  102. Sub RunAsAdmin()
  103.   Dim objItems, objItem, strVer, nVer
  104.   Set objItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  105.   For Each objItem In objItems
  106.     strVer = objItem.Version
  107.   Next
  108.   nVer = Split(strVer, ".")(0) & Split(strVer, ".")(1)
  109.   If nVer >= 60 Then
  110.     Dim oShell, oArg, strArgs
  111.     Set oShell = CreateObject("Shell.Application")
  112.     If Not WScript.Arguments.Named.Exists("ADMIN") Then
  113.       For Each oArg In WScript.Arguments
  114.         strArgs = strArgs & " """ & oArg & """"
  115.       Next
  116.       strArgs = strArgs & " /ADMIN:1"
  117.       Call oShell.ShellExecute("WScript.exe", """" & WScript.ScriptFullName & """" & strArgs, "", "runas", 1)
  118.       Set oShell = Nothing
  119.       WScript.Quit(0)
  120.     End If
  121.     Set oShell = Nothing
  122.   End If
  123. End Sub
复制代码

  1. powershell –NoProfile –ExecutionPolicy Bypass "Get-Disk"
复制代码
bat文件里面,以上命令可以显示正常的GPT和MBR

TOP

TOP

本帖最后由 aloha20200628 于 2025-2-13 11:45 编辑

回复 2# ygqiang

用 powershell/vbs/jscript/wmic/... 等方法均可获取硬盘数据,比较而言,走 powershell 之道要慢一些,但要获取硬盘分区类型 MBR/GPT 数据还是 powershell 句式最简洁,以下给一个示例脚本,先完成自启提权窗口再来获取 MBR/GPT 数据,代码存为 test.bat 运行...
  1. @echo off
  2. %~1 powershell "start -file '%~f0' -arg '::' -verb runas" &exit/b
  3. echo,...正在获取本地和外接硬盘数据...
  4. powershell "get-disk|%%{''+$_.number+' '+[math]::round($_.size/1tb,1)+'TB '+$_.partitionStyle+' '+$_.FriendlyName}"|sort
  5. pause&exit/b
复制代码

TOP

回复 4# aloha20200628


    谢谢。
  1. @echo off & setlocal enabledelayedexpansion
  2. echo 代码开始
  3. cls
  4. echo ------------系统引导方式:
  5. powershell –NoProfile –ExecutionPolicy Bypass "$env:Firmware_Type"
  6. echo.
  7. echo.
  8. echo ------------硬盘格式::
  9. powershell –NoProfile –ExecutionPolicy Bypass "Get-Disk"
  10. pause >nul
  11. exit
复制代码

TOP

本帖最后由 ygqiang 于 2025-2-13 22:11 编辑

已解决,接近完美的代码。引导方式-分区表类型-空间占用-接口协议
  1. @echo off & setlocal enabledelayedexpansion
  2. echo 代码开始
  3. cls
  4. echo ——————————————————————————————————————— >nul 2>nul
  5. if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  6. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
  7.     more +%%i "%~f0" > c:\diskinfo.vbs
  8. )
  9. ping 127.0.0.1 -n 2 >nul 2>nul
  10. cls
  11. echo ------------系统引导方式:
  12. powershell –NoProfile –ExecutionPolicy Bypass "$env:Firmware_Type"
  13. echo.
  14. echo ------------硬盘分区表类型::
  15. powershell "get-disk|%%{''+'     '+$_.number+' '+'    '+$_.partitionStyle+'   '+$_.FriendlyName}"|sort
  16. echo.
  17. echo ------------硬盘空间占用::
  18. cscript /nologo c:\diskinfo.vbs
  19. echo ------------硬盘接口协议::
  20. echo.           协议   机械/固态    接口
  21. set "_NVMe=NVMe====SSD    ::::::m.2"
  22. set "_USB=USB"
  23. set "_SATA=SATA====HDD/SSD::::::SATA-MSATA-m.2"
  24. for /f "tokens=2,4,5" %%a in ('"echo list disk|diskpart|findstr /irc:"磁盘  *[0-9]""') do (
  25.      for /f "tokens=3" %%i in ('"(echo sel disk %%a & echo detail disk & echo list part)|diskpart|findstr /irc:"类型  *:""') do (
  26.           if defined _%%i echo       %%a:   !_%%i!           %%b %%c
  27.      )
  28. )
  29. pause >nul
  30. if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  31. exit
  32. CScript /NoLogo c:\diskinfo.vbs>c:\diskinfo.txt
  33. ping 127.0.0.1 -n 2 >nul 2>nul
  34. cls
  35. Type c:\diskinfo.txt
  36. echo ——————————————————————————————————————— >nul 2>nul
  37. if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  38. set "vbs=c:\diskinfo.vbs"
  39. set "log=c:\diskinfo.log"
  40. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (more +%%i "%~f0" > "%vbs%")
  41. CScript //NoLogo "%vbs%" > "%log%"
  42. cls
  43. type "%log%"
  44. ping 127.0.0.1 -n 5 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul) & exit/b
  45. goto :eof
  46. echo ——————————————————————————————————————— >nul 2>nul
  47. :DynamicScript
  48. Main
  49. Sub Main()
  50.   Dim objWMI, colDisks
  51.   Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  52.   Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
  53.   ' 获取硬盘最大序号
  54.   nIndexMax = 0
  55.   For Each objDisk in colDisks
  56.     If Cint(objDisk.Index) > nIndexMax Then nIndexMax = Cint(objDisk.Index)
  57.   Next
  58.   ' 按序号取信息
  59.   For i = 0 To nIndexMax
  60.     For Each objDisk in colDisks
  61.       ' 只取该序号信息
  62.       If objDisk.Index = i Then
  63.         s = s & "硬盘" & objDisk.Index & ":" & vbTab
  64.         s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  65.         s = s & "型号:" & objDisk.Caption &  vbCrLf
  66.         's = s & "型号:" & objDisk.Caption & vbTab
  67.         's = s & "分区表:" & IsGPT(objDisk.Index) & vbCrLf
  68.         strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  69.         Set colPartitions = objWMI.ExecQuery _
  70.             ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  71.             & " where AssocClass=Win32_DiskDriveToDiskPartition")
  72.         For Each objPartition in colPartitions
  73.           strPartId = objPartition.DeviceID
  74.           Set colLogicalDisks = objWMI.ExecQuery _
  75.               ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId _
  76.               & """}  where AssocClass=Win32_LogicalDiskToPartition")
  77.           For Each objLogicalDisk in colLogicalDisks
  78.             size = objLogicalDisk.Size
  79.             free = objLogicalDisk.Freespace
  80.             used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  81.             s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  82.             s = s & "大小:" & GetSize(size) & vbTab
  83.             s = s & "剩余:" & GetSize(free) & vbTab
  84.             s = s & "使用率:" & used & vbCrLf
  85.           Next
  86.         Next
  87.         s = s & vbCrLf
  88.       End If
  89.     Next
  90.   Next
  91.   WScript.Echo s
  92. End Sub
  93. ' 格式化
  94. Function GetSize(intSize)
  95.   If intSize/1024/1024 > 1024 Then
  96.     GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  97.   Else
  98.     GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  99.   End If
  100. End Function
  101. ' 获取指定硬盘的分区表类型(GPT/MBR)
  102. Function IsGPT(ByVal nDiskIndex)
  103.   IsGPT = "MBR"
  104.   Dim wso, sLogFile, sText
  105.   Set wso = CreateObject("WScript.Shell")
  106.   sLogFile = wso.ExpandenVironmentStrings("%temp%\diskpart.log")
  107.   wso.Run "cmd /c ""chcp 437 & cls & (echo list disk | diskpart | find /i ""Disk " & nDiskIndex & """) >""" & sLogFile & """ "" ", 0, False
  108.   Call TxtFile(sLogFile, 1, -2, sText)
  109.   If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
  110. End Function
  111. ' 对文本指定编码进行读写操作2
  112. 'nRW: 1只读, 2只写, 8追加 'nCharset: -2(系统), -1(Unicode), 0(ASCII)
  113. Sub TxtFile(ByVal FileName, ByVal nRW, ByVal nCharset, ByRef sText)
  114.   Dim fso :  Set fso = CreateObject("Scripting.filesystemobject")
  115.   If sText <> "" And (nRW = 2 Or nRW = 8) Then
  116.     fso.OpenTextFile(FileName, nRW, True, nCharset).Write sText
  117.   ElseIf fso.FileExists(FileName) And nRW = 1 Then
  118.     If fso.GetFile(FileName).Size > 0 Then _
  119.       sText = fso.OpenTextFile(FileName, nRW, False, nCharset).ReadAll
  120.   End If
  121. End Sub
  122. ' 以管理员身份运行
  123. Sub RunAsAdmin()
  124.   Dim objItems, objItem, strVer, nVer
  125.   Set objItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  126.   For Each objItem In objItems
  127.     strVer = objItem.Version
  128.   Next
  129.   nVer = Split(strVer, ".")(0) & Split(strVer, ".")(1)
  130.   If nVer >= 60 Then
  131.     Dim oShell, oArg, strArgs
  132.     Set oShell = CreateObject("Shell.Application")
  133.     If Not WScript.Arguments.Named.Exists("ADMIN") Then
  134.       For Each oArg In WScript.Arguments
  135.         strArgs = strArgs & " """ & oArg & """"
  136.       Next
  137.       strArgs = strArgs & " /ADMIN:1"
  138.       Call oShell.ShellExecute("WScript.exe", """" & WScript.ScriptFullName & """" & strArgs, "", "runas", 1)
  139.       Set oShell = Nothing
  140.       WScript.Quit(0)
  141.     End If
  142.     Set oShell = Nothing
  143.   End If
  144. End Sub
复制代码

TOP

返回列表