标题: [原创代码] PowerShell脚本get- 得到磁盘和分区信息 [打印本页]
作者: newswan 时间: 2021-7-8 19:33 标题: PowerShell脚本get- 得到磁盘和分区信息
用powershell 得到磁盘和分区信息,请看看,有什么改进么- 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() {
- $ftsize = @{label="Size" ; expression={(format-filesize($_.Size))} ; alignment ="right" ; width = 30 }
- $ftOffset = @{label="Offset" ; expression={(format-filesize($_.Offset))} ; alignment ="right" ; width = 30 }
- $ftdDisk = @{label="Disk" ; expression={$_.Number}}
- $ftdName = @{label="Name" ; expression={$_.FriendlyName}}
- $ftdBus = @{label="Bus" ; expression={$_.BusType}}
- $ftdMedia = @{label="Media" ; expression={$_.MediaType}}
- $ftdStyle = @{label="Style" ; expression={$_.PartitionStyle}}
- $ftpDisk = @{label="Disk" ; expression={$_.DiskNumber}}
- $ftpPart = @{label="Part" ; expression={$_.PartitionNumber}}
- $ftpLetter = @{label="Ltr" ; expression={$_.DriveLetter}}
- $ftpIsActive = @{label="Active" ; expression={ switch ($_.IsActive) { True {"A"} } } ; alignment ="center"}
- $ftpIsBoot = @{label="Boot" ; expression={ switch ($_.IsBoot) { True {"B"} } }}
- $ftpAB = @{label="A-B" ; expression={ switch ($_.IsActive) { True {"Active"} } ; switch ($_.IsBoot) { True {"Boot"} } }}
-
- $disk | Format-Table $ftdDisk,$ftdName,$ftdBus,$ftdMedia,$ftsize,$ftdStyle
- $part | Format-Table $ftpDisk,$ftpPart,$ftpLetter,$ftpIsActive,$ftsize,FS
- }
-
- $disk,$part = get-DiskPart
- list-diskpart
复制代码
作者: newswan 时间: 2021-7-8 19:35
本帖最后由 newswan 于 2021-7-8 19:45 编辑
输出内容为- Disk Name Bus Media Size Style
- ---- ---- --- ----- ---- -----
- 0 Pioneer APS-SE20-256 NVMe SSD 238.5 G GPT
-
-
- Disk Part Ltr Active Size FS
- ---- ---- --- ------ ---- --
- 0 1 260.0 M
- 0 2 16.0 M
- 0 3 C 100.0 G NTFS
- 0 4 D 138.2 G NTFS
复制代码
运行时间2秒
作者: newswan 时间: 2021-7-12 22:19
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
作者: newswan 时间: 2021-8-7 20:20
- function init()
- {
- $script:ftToTalSize = @{label="ToTalSize" ; expression={(zFormat-HrSize($_.Size))} ; alignment = "right" }
- $script:ftAllocatedSize = @{label="Allocated" ; expression={(zFormat-HrSize($_.AllocatedSize))} ; alignment = "right" }
- $script:ftOffset = @{label="Offset" ; expression={ [string]::format("{0:N0}",$_.Offset) } ; alignment = "right" ; Width = 20 }
- $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:ftpPartType = @{label="PartType" ; expression={$_.Type}}
- $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 zFormat-HrSize() {
- Param ( [Double]$size )
- $a = "BKMGT"
- for ($i = 0 ; $i -lt $a.length -and $size -ge 1024 ; $i++) {
- $size = $size / 1024
- }
- return ( [string]::Format("{0:0.0}", $size) + " " + $a.Substring($i,1) )
- }
-
- function get-DiskPart() {
- [Object[]] $diskPhy = Get-PhysicalDisk
- [Object[]] $disk = get-disk | sort-object { $_.DiskNumber }
- $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 | sort-object { $_.DiskNumber , $_.PartitionNumber }
- $part | add-member -NotePropertyName "FSType" -NotePropertyValue ""
- $part | add-member -NotePropertyName "FSLabel" -NotePropertyValue ""
- for ($i = 0 ; $i -le $part.GetUpperBound(0) ; $i++) {
- $part[$i].FSType = ( Get-Volume -Partition $part[$i] ).FileSystemType
- $part[$i].FSLabel = ( Get-Volume -Partition $part[$i] ).FileSystemLabel
- }
- return $disk,$part
- }
-
- function list-diskpart() {
- $disk | Format-Table $ftdDisk,$ftdName,$ftdBus,$ftdMedia,$ftdStyle,$ftToTalSize,$ftAllocatedSize
- $partStr = $part | Format-Table $ftpDisk,$ftpPart,$ftpLetter,$ftpIsActive,FSType,FSLabel,$ftToTalSize,$ftOffset,$ftpPartType | out-string
- $partStr = $partStr -replace "`r","" -replace "`n+$","`n" -split "`n"
- for ( $i = 0 ; $i -le $partstr.GetUpperBound(0) ; $i++)
- {
- if ( $partstr[$i-1] -match "^\s+(\d+)" ) { $n1 = $matches[1] }
- if ( $partstr[$i] -match "^\s+(\d+)" ) { $n2 = $matches[1] }
- if ( [int]$n1 + 1 -eq [int]$n2 )
- {
- $partstr[2]
- }
- $partstr[$i]
- }
- }
-
- 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,$ftToTalSize,FSType,FSLabel
复制代码
作者: newswan 时间: 2021-8-7 21:03
本帖最后由 newswan 于 2021-8-7 21:11 编辑
输出样式- Disk Name Bus Media Style ToTalSize Allocated
- ---- ---- --- ----- ----- --------- ---------
- 0 Pioneer APS-SE20-256 NVMe SSD GPT 238.5 G 238.5 G
- 1 Msft Virtual Disk File Backed Virtual SSD MBR 10.0 G 5.0 G
- 2 Msft Virtual Disk File Backed Virtual SSD GPT 10.0 G 10.0 G
- 3 ST500DM002-1ER14C SATA HDD MBR 465.8 G 465.8 G
-
-
-
- Disk Part Ltr Active FSType FSLabel ToTalSize Offset PartType
- ---- ---- --- ------ ------ ------- --------- ------ --------
- 0 1 260.0 M 1,048,576 System
- 0 2 16.0 M 273,678,336 Reserved
- 0 3 C NTFS 100.0 G 290,455,552 Basic
- 0 4 D NTFS 138.2 G 107,664,637,952 Basic
- ---- ---- --- ------ ------ ------- --------- ------ --------
- 1 0 9.0 G 1,074,790,400 XINT13 Extended
- 1 1 Unknown 1023.0 M 1,048,576 Logical
- 1 2 Unknown 4.0 G 1,075,838,976 Logical
- ---- ---- --- ------ ------ ------- --------- ------ --------
- 2 1 16.0 M 17,408 Reserved
- 2 2 W NTFS New Volume 10.0 G 16,777,216 Basic
- ---- ---- --- ------ ------ ------- --------- ------ --------
- 3 1 F A NTFS 60.0 G 1,048,576 IFS
- 3 2 G NTFS 100.0 G 64,425,558,016 IFS
- 3 3 H NTFS 305.8 G 171,799,740,416 IFS
复制代码
作者: newswan 时间: 2021-8-7 21:10
mbr 分区情况下,分区类型检测有问题
未格式化, primary 显示成 Logical
格式化,都显示成IFS
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |