批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[原创代码] PowerShell脚本get- 得到磁盘和分区信息

用powershell 得到磁盘和分区信息,请看看,有什么改进么
  1. Function Format-FileSize() {
  2.     Param ( [Double]$size )
  3.     [string[]] $a = "B","K","M","G","T"
  4.     for ($i = $a.GetlowerBound(0) ; $i -le $a.GetUpperBound(0) -and $size -ge 1024 ; $i++) {
  5.         $size = $size / 1024
  6.     }
  7.     return ( [string]::Format("{0:0.0}", $size) + " " + $a[$i])
  8. }
  9. function get-DiskPart() {
  10.     [Object[]] $diskPhy = Get-PhysicalDisk
  11.     [Object[]] $disk = get-disk
  12.     $disk | add-member -NotePropertyName "MediaType" -NotePropertyValue ""
  13.     for ($i = 0 ; $i -le $disk.GetUpperBound(0) ; $i++){
  14.         $disk[$i].mediatype = ($diskPhy | Where-Object {$_.deviceid -eq $i} ).mediatype
  15.     }
  16.     [Object[]] $vol = Get-Volume
  17.     [Object[]] $part = Get-Partition
  18.     $part | add-member -NotePropertyName "FS" -NotePropertyValue ""
  19.     for ($i = 0 ; $i -le $part.GetUpperBound(0) ; $i++) {
  20.         $part[$i].FS = ( Get-Volume -Partition $part[$i] ).FileSystemType
  21.     }
  22.     return $disk,$part
  23. }
  24. function list-diskpart() {
  25.     $ftsize = @{label="Size" ; expression={(format-filesize($_.Size))} ; alignment ="right" ; width = 30 }
  26.     $ftOffset = @{label="Offset" ; expression={(format-filesize($_.Offset))} ; alignment ="right" ; width = 30 }
  27.     $ftdDisk = @{label="Disk" ; expression={$_.Number}}
  28.     $ftdName = @{label="Name" ; expression={$_.FriendlyName}}
  29.     $ftdBus = @{label="Bus" ; expression={$_.BusType}}
  30.     $ftdMedia = @{label="Media" ; expression={$_.MediaType}}
  31.     $ftdStyle = @{label="Style" ; expression={$_.PartitionStyle}}
  32.     $ftpDisk = @{label="Disk" ; expression={$_.DiskNumber}}
  33.     $ftpPart = @{label="Part" ; expression={$_.PartitionNumber}}
  34.     $ftpLetter = @{label="Ltr" ; expression={$_.DriveLetter}}
  35.     $ftpIsActive = @{label="Active" ; expression={ switch ($_.IsActive) { True {"A"} } } ; alignment ="center"}
  36.     $ftpIsBoot = @{label="Boot" ; expression={ switch ($_.IsBoot) { True {"B"} } }}
  37.     $ftpAB = @{label="A-B" ; expression={ switch ($_.IsActive) { True {"Active"} } ; switch ($_.IsBoot) { True {"Boot"} } }}
  38.     $disk | Format-Table $ftdDisk,$ftdName,$ftdBus,$ftdMedia,$ftsize,$ftdStyle
  39.     $part | Format-Table $ftpDisk,$ftpPart,$ftpLetter,$ftpIsActive,$ftsize,FS
  40. }
  41. $disk,$part = get-DiskPart
  42. list-diskpart
复制代码

本帖最后由 newswan 于 2021-7-8 19:45 编辑

输出内容为
  1. Disk Name                 Bus  Media    Size Style
  2. ---- ----                 ---  -----    ---- -----
  3.    0 Pioneer APS-SE20-256 NVMe SSD   238.5 G GPT
  4. Disk Part Ltr Active    Size FS
  5. ---- ---- --- ------    ---- --
  6.    0    1            260.0 M
  7.    0    2             16.0 M
  8.    0    3   C        100.0 G NTFS
  9.    0    4   D        138.2 G NTFS
复制代码
运行时间2秒

TOP

function init()
{
    $script:ftsize = @{label="Size" ; expression={(format-filesize($_.Size))} ; alignment ="right" ; width = 30 }
    $script:ftOffset = @{label="Offset" ; expression={(format-filesize($_.Offset))} ; alignment ="right" ; width = 30 }
    $script:ftdDisk = @{label="Disk" ; expression={$_.Number}}
    $script:ftdName = @{label="Name" ; expression={$_.FriendlyName}}
    $script:ftdBus = @{label="Bus" ; expression={$_.BusType}}
    $script:ftdMedia = @{label="Media" ; expression={$_.MediaType}}
    $script:ftdStyle = @{label="Style" ; expression={$_.PartitionStyle}}
    $script:ftpDisk = @{label="Disk" ; expression={$_.DiskNumber}}
    $script:ftpPart = @{label="Part" ; expression={$_.PartitionNumber}}
    $script:ftpLetter = @{label="Ltr" ; expression={$_.DriveLetter}}
    $script:ftpIsActive = @{label="Active" ; expression={ switch ($_.IsActive) { True {"A"} } } ; alignment ="center"}
    $script:ftpIsBoot = @{label="Boot" ; expression={ switch ($_.IsBoot) { True {"B"} } }}
    $script:ftpAB = @{label="A-B" ; expression={ switch ($_.IsActive) { True {"Active"} } ; switch ($_.IsBoot) { True {"Boot"} } }}
}

Function Format-FileSize() {
    Param ( [Double]$size )
    [string[]] $a = "B","K","M","G","T"
    for ($i = $a.GetlowerBound(0) ; $i -le $a.GetUpperBound(0) -and $size -ge 1024 ; $i++) {
        $size = $size / 1024
    }
    return ( [string]::Format("{0:0.0}", $size) + " " + $a[$i])
}

function get-DiskPart() {
    [Object[]] $diskPhy = Get-PhysicalDisk
    [Object[]] $disk = get-disk
    $disk | add-member -NotePropertyName "MediaType" -NotePropertyValue ""
    for ($i = 0 ; $i -le $disk.GetUpperBound(0) ; $i++){
        $disk[$i].mediatype = ($diskPhy | Where-Object {$_.deviceid -eq $i} ).mediatype
    }
    [Object[]] $vol = Get-Volume
    [Object[]] $part = Get-Partition
    $part | add-member -NotePropertyName "FS" -NotePropertyValue ""
    for ($i = 0 ; $i -le $part.GetUpperBound(0) ; $i++) {
        $part[$i].FS = ( Get-Volume -Partition $part[$i] ).FileSystemType
    }
    return $disk,$part
}

function list-diskpart() {
    $disk | Format-Table $ftdDisk,$ftdName,$ftdBus,$ftdMedia,$ftsize,$ftdStyle
    $part | Format-Table $ftpDisk,$ftpPart,$ftpLetter,$ftpIsActive,$ftsize,FS
}

init
$disk,$part = get-DiskPart
list-diskpart

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

$part | where-object { ($_.DiskNumber -eq $seldisk -and $_.PartitionNumber -eq $selpart) -or $_.DriveLetter -eq $selvol } |
    Format-Table $ftpDisk,$ftpPart,$ftpLetter,$ftpIsActive,$ftsize,FS

TOP

返回列表