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

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

[复制链接]
发表于 2023-3-2 13:11:55 | 显示全部楼层 |阅读模式
本帖最后由 newswan 于 2023-4-17 13:54 编辑
  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.         #( ([math]::Ceiling($num)).tostring() + " " + $unit[$i])
  11.         #[string]::Format("{0:0.0}", $num)
  12. }

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


  31. function get-DiskPart() {
  32.         $diskPhy = @(Get-PhysicalDisk)
  33.         $disk = @( get-disk | sort-object { $_.DiskNumber } )
  34.         $disk | add-member -NotePropertyName "MediaType" -NotePropertyValue ""
  35.         foreach ( $x in $diskPhy ) {
  36.                 $disk | Where-Object { if ($_.UniqueId -eq $x.UniqueId) {$_.MediaType = $x.MediaType} }
  37.         }
  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-diskpart() {
  53.         $disk | Format-Table $ftdDisk,$ftdBus,$ftdMedia,$ftdStyle,$ftToTalSize,$ftAllocatedSize,$ftdName
  54.         $part | Format-Table $ftpDisk,$ftpPart,$ftpLetter,FSType,$ftToTalSize,$ftpPartType,FSLabel        #,$ftpIsActive,$ftOffset
  55. }

  56. init
  57. $disk,$part = get-DiskPart
  58. list-diskpart

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-17 01:12 , Processed in 0.016398 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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