找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 8509|回复: 3

[系统增强] powershell 获取 磁盘信息

[复制链接]
发表于 2025-2-12 10:57:48 | 显示全部楼层 |阅读模式
  1. Function zFormat_sizereadable() {
  2.         param(
  3.                 [int64]$num
  4.         )
  5.        
  6.         $unit =@("B","K","M","G","T")
  7.         $i = 0
  8.         while ($num -gt 1024 ) { $i++; $num = $num /1024 }
  9.         return "{0} {1}" -f $num , $unit[$i]
  10. }

  11. function Init-Formatting()
  12. {
  13.         $script:ftToTalSize = @{label="ToTalSize" ; expression={(zFormat_sizereadable($_.Size))} ; alignment = "right" }
  14.         $script:ftAllocatedSize = @{label="Allocated" ; expression={zFormat_sizereadable($_.AllocatedSize)} ; alignment = "right" }
  15.         $script:ftOffset = @{label="Offset" ; expression={ zFormat_sizereadable($_.Offset) } ; alignment = "right"}
  16.         $script:ftdDisk = @{label="Disk" ; expression={$_.Number}}
  17.         $script:ftdName = @{label="Name" ; expression={$_.FriendlyName}}
  18.         $script:ftdBus = @{label="Bus" ; expression={$_.BusType}}
  19.         $script:ftdMedia = @{label="Media" ; expression={$_.MediaType}}
  20.         $script:ftdStyle = @{label="Style" ; expression={$_.PartitionStyle}}
  21.         $script:ftpDisk = @{label="Disk" ; expression={$_.DiskNumber}}
  22.         $script:ftpPart = @{label="Part" ; expression={$_.PartitionNumber}}
  23.         $script:ftpPartType = @{label="PartType" ; expression={$_.Type}}
  24.         $script:ftpLetter = @{label="Ltr" ; expression={$L = ($_.DriveLetter).tostring();if ($L -match "[B-Z]") {$L} else {"-"}} ; alignment = "right" }
  25.         $script:ftpIsActive = @{label="Active" ; expression={ switch ($_.IsActive) { True {"A"} } } ; alignment ="center"}
  26.         $script:ftpIsBoot = @{label="Boot" ; expression={ switch ($_.IsBoot) { True {"B"} } }}
  27.         $script:ftpAB = @{label="A-B" ; expression={ switch ($_.IsActive) { True {"Active"} } ; switch ($_.IsBoot) { True {"Boot"} } }}
  28.         $script:ftFSLabel = @{label="FSLabel" ; expression={"'" + $_.FSLabel + "'"} ; alignment = "left" }
  29. }


  30. function Get-DiskPartitions() {
  31.         $diskPhy = @(Get-PhysicalDisk)
  32.         $disk = @( get-disk | sort-object { $_.DiskNumber } )
  33.         $disk | add-member -NotePropertyName "MediaType" -NotePropertyValue ""
  34.         foreach ( $x in $diskPhy ) {
  35.                 $disk | Where-Object { if ($_.UniqueId -eq $x.UniqueId) {$_.MediaType = $x.MediaType} }
  36.         }
  37. #        $vol = @(Get-WmiObject win32_volume)
  38.         $vol =@(get-volume)
  39.         $part = @( Get-Partition | sort-object { $_.DiskNumber , $_.PartitionNumber } )
  40.         $part | add-member -NotePropertyName "FSType" -NotePropertyValue ""
  41.         $part | add-member -NotePropertyName "FSLabel" -NotePropertyValue ""

  42.         $part | foreach-object {
  43.                 $pAccessPaths = $_.AccessPaths
  44.                 $v =  $vol | Where-Object { $pAccessPaths -contains $_.Path }
  45.                 if ( $v ) {
  46.                         $_.FSType = $v.FileSystem
  47.                         $_.FSLabel = $v.FileSystemLabel
  48.                 }
  49.         }
  50.         return $disk,$part
  51. }

  52. function List-DiskPartitions() {
  53.         $disk | Format-Table $ftdDisk,$ftdBus,$ftdMedia,$ftdStyle,$ftToTalSize,$ftAllocatedSize,$ftdName
  54.         $part | Format-Table $ftpDisk,$ftpPart,$ftpLetter,FSType,$ftToTalSize,$ftpPartType, $ftFSLabel        #,$ftpIsActive,$ftOffset
  55. }

  56. Init-Formatting
  57. $disk,$part = Get-DiskPartitions
  58. List-DiskPartitions
  59. return

  60. $dpv = read-host "select disk,part or vol"
  61. $dpv = $dpv.trim()
  62. switch -Regex ($dpv)
  63. {
  64.         '^([c-z])$' { $selvol = $matches[1] ; break }
  65.         '(\d+)\D+(\d+)' { $seldisk = $matches[1] ; $selpart = $matches[2] ; break }
  66. }

  67. $part | where-object { ($_.DiskNumber -eq $seldisk -and $_.PartitionNumber -eq $selpart) -or ($_.DriveLetter -eq $selvol) } |
  68.         Format-Table $ftpDisk,$ftpPart,$ftpLetter,$ftpIsActive,$ftToTalSize,FSType,ftFSLabel
复制代码
发表于 2025-2-12 14:04:45 | 显示全部楼层
没有
Get-PhysicalDisk
get-disk
命令的系统用不了
发表于 2025-2-12 17:16:24 | 显示全部楼层
bat批处理里面,如何调用代码并显示磁盘信息?谢谢
 楼主| 发表于 2025-2-13 00:53:45 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 18:54 , Processed in 0.018017 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表