标题: [原创代码] PowerShell获取磁盘和分区信息 [打印本页]
作者: newswan 时间: 2023-3-2 13:11 标题: PowerShell获取磁盘和分区信息
本帖最后由 newswan 于 2023-4-17 13:54 编辑
- Function zFormat-sizereadable() {
- param(
- [int64]$num
- )
-
- $unit =@("B","K","M","G","T")
- $i = 0
- while ($num -gt 1024 ) { $i++; $num = $num /1024 }
- return "{0} {1}" -f $num , $unit[$i]
- #( ([math]::Ceiling($num)).tostring() + " " + $unit[$i])
- #[string]::Format("{0:0.0}", $num)
- }
-
- function init()
- {
- $script:ftToTalSize = @{label="ToTalSize" ; expression={(zFormat-sizereadable($_.Size))} ; alignment = "right" }
- $script:ftAllocatedSize = @{label="Allocated" ; expression={zFormat-sizereadable($_.AllocatedSize)} ; alignment = "right" }
- $script:ftOffset = @{label="Offset" ; expression={ zFormat-sizereadable($_.Offset) } ; alignment = "right"}
- $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={$L = ($_.DriveLetter).tostring();if ($L -match "[B-Z]") {$L} else {"-"}} ; alignment = "right" }
- $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 get-DiskPart() {
- $diskPhy = @(Get-PhysicalDisk)
- $disk = @( get-disk | sort-object { $_.DiskNumber } )
- $disk | add-member -NotePropertyName "MediaType" -NotePropertyValue ""
- foreach ( $x in $diskPhy ) {
- $disk | Where-Object { if ($_.UniqueId -eq $x.UniqueId) {$_.MediaType = $x.MediaType} }
- }
- $vol =@(get-volume)
- $part = @( Get-Partition | sort-object { $_.DiskNumber , $_.PartitionNumber } )
- $part | add-member -NotePropertyName "FSType" -NotePropertyValue ""
- $part | add-member -NotePropertyName "FSLabel" -NotePropertyValue ""
-
- $part | foreach-object {
- $pAccessPaths = $_.AccessPaths
- $v = $vol | Where-Object { $pAccessPaths -contains $_.Path }
- if ( $v ) {
- $_.FSType = $v.FileSystem
- $_.FSLabel = $v.FileSystemLabel
- }
- }
- return $disk,$part
- }
-
- function list-diskpart() {
- $disk | Format-Table $ftdDisk,$ftdBus,$ftdMedia,$ftdStyle,$ftToTalSize,$ftAllocatedSize,$ftdName
- $part | Format-Table $ftpDisk,$ftpPart,$ftpLetter,FSType,$ftToTalSize,$ftpPartType,FSLabel #,$ftpIsActive,$ftOffset
- }
-
- init
- $disk,$part = get-DiskPart
- list-diskpart
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |