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

[原创代码] PowerShell获取磁盘和分区信息

本帖最后由 newswan 于 2023-4-17 13:54 编辑
  1. Function zFormat-sizereadable() {
  2. param(
  3. [int64]$num
  4. )
  5. $unit =@("B","K","M","G","T")
  6. $i = 0
  7. while ($num -gt 1024 ) { $i++; $num = $num /1024 }
  8. return "{0} {1}" -f $num , $unit[$i]
  9. #( ([math]::Ceiling($num)).tostring() + " " + $unit[$i])
  10. #[string]::Format("{0:0.0}", $num)
  11. }
  12. function init()
  13. {
  14. $script:ftToTalSize = @{label="ToTalSize" ; expression={(zFormat-sizereadable($_.Size))} ; alignment = "right" }
  15. $script:ftAllocatedSize = @{label="Allocated" ; expression={zFormat-sizereadable($_.AllocatedSize)} ; alignment = "right" }
  16. $script:ftOffset = @{label="Offset" ; expression={ zFormat-sizereadable($_.Offset) } ; alignment = "right"}
  17. $script:ftdDisk = @{label="Disk" ; expression={$_.Number}}
  18. $script:ftdName = @{label="Name" ; expression={$_.FriendlyName}}
  19. $script:ftdBus = @{label="Bus" ; expression={$_.BusType}}
  20. $script:ftdMedia = @{label="Media" ; expression={$_.MediaType}}
  21. $script:ftdStyle = @{label="Style" ; expression={$_.PartitionStyle}}
  22. $script:ftpDisk = @{label="Disk" ; expression={$_.DiskNumber}}
  23. $script:ftpPart = @{label="Part" ; expression={$_.PartitionNumber}}
  24. $script:ftpPartType = @{label="PartType" ; expression={$_.Type}}
  25. $script:ftpLetter = @{label="Ltr" ; expression={$L = ($_.DriveLetter).tostring();if ($L -match "[B-Z]") {$L} else {"-"}} ; alignment = "right" }
  26. $script:ftpIsActive = @{label="Active" ; expression={ switch ($_.IsActive) { True {"A"} } } ; alignment ="center"}
  27. $script:ftpIsBoot = @{label="Boot" ; expression={ switch ($_.IsBoot) { True {"B"} } }}
  28. $script:ftpAB = @{label="A-B" ; expression={ switch ($_.IsActive) { True {"Active"} } ; switch ($_.IsBoot) { True {"Boot"} } }}
  29. }
  30. function get-DiskPart() {
  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-volume)
  38. $part = @( Get-Partition | sort-object { $_.DiskNumber , $_.PartitionNumber } )
  39. $part | add-member -NotePropertyName "FSType" -NotePropertyValue ""
  40. $part | add-member -NotePropertyName "FSLabel" -NotePropertyValue ""
  41. $part | foreach-object {
  42.         $pAccessPaths = $_.AccessPaths
  43. $v =  $vol | Where-Object { $pAccessPaths -contains $_.Path }
  44.         if ( $v ) {
  45.     $_.FSType = $v.FileSystem
  46.     $_.FSLabel = $v.FileSystemLabel
  47.         }
  48. }
  49. return $disk,$part
  50. }
  51. function list-diskpart() {
  52. $disk | Format-Table $ftdDisk,$ftdBus,$ftdMedia,$ftdStyle,$ftToTalSize,$ftAllocatedSize,$ftdName
  53. $part | Format-Table $ftpDisk,$ftpPart,$ftpLetter,FSType,$ftToTalSize,$ftpPartType,FSLabel #,$ftpIsActive,$ftOffset
  54. }
  55. init
  56. $disk,$part = get-DiskPart
  57. list-diskpart
复制代码

返回列表