Skip to content

Instantly share code, notes, and snippets.

@nikyodo85
Last active October 31, 2018 22:10
Show Gist options
  • Save nikyodo85/023024ec4b938a8cd6c518d575b290c4 to your computer and use it in GitHub Desktop.
Save nikyodo85/023024ec4b938a8cd6c518d575b290c4 to your computer and use it in GitHub Desktop.
Created this to extract information from diskpart. As of this writing, it supports the following disk part commands: "list disk", "list partition", "list volume", "shrink querymax", "detail disk"
function Parse-SizeUnit {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)][string]$SizeUnit
)
$SizeUnit = $SizeUnit.Trim()
$Size = 0;
$Unit = 'B';
if ($SizeUnit -match '\d+\s+\w*B$') {
$SizeUnitSplit = $SizeUnit -split '\s+'
$Size = [int]$SizeUnitSplit[0]
$Unit = $SizeUnitSplit[1]
} elseif ($SizeUnit -match '\d+\w*B$') {
$SizeUnit -match '\d+'
$Size = [int]$Matches[0]
$SizeUnit -match '\D+\w*B$'
$Unit = $Matches[0]
}
if ($Unit -eq 'TB') {
$Size * 1TB
} elseif ($Unit -eq 'GB') {
$Size * 1GB
} elseif ($Unit -eq 'MB') {
$Size * 1MB
} elseif ($Unit -eq 'KB') {
$Size * 1KB
} elseif ($Unit -eq 'B') {
$Size
} else { 0 }
}
function List-Disk {
$DiskArray = 'list disk' | diskpart | Where-Object { $_ -match 'Disk .+' }
$HeaderRow = $DiskArray | Where-Object { $_ -match 'Disk ### .*' } | Select -First 1
$DiskNumberIndex = $HeaderRow.IndexOf('###')
$StatusIndex = $HeaderRow.IndexOf('Status')
$SizeIndex = $HeaderRow.IndexOf('Size')
$FreeIndex = $HeaderRow.IndexOf('Free')
$DynamicIndex = $HeaderRow.IndexOf('Dyn')
$GptIndex = $HeaderRow.IndexOf('Gpt')
$DiskArray | Select -Skip 1 | ForEach-Object {
New-Object PSObject -Property @{
Number = [int]$_.SubString($DiskNumberIndex, 3).Trim();
Status = $_.SubString($StatusIndex, 13).Trim();
Size = Parse-SizeUnit $_.SubString($SizeIndex, 7).Trim();
Free = Parse-SizeUnit $_.SubString($FreeIndex, 7).Trim();
IsDynamic = $_.SubString($DynamicIndex, 3).Trim() -eq '*';
IsGpt = $_.SubString($GptIndex).Trim() -eq '*';
PartitionStyle = If ($_.SubString($GptIndex).Trim() -eq '*') {'GPT'} Else {'MBR'};
}
}
}
function List-Partition {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)][int]$DiskNumber
)
$PartitionArray = "select disk $($DiskNumber)", "list partition" | diskpart | Where-Object { $_ -match 'Partition .+' }
$HeaderRow = $PartitionArray | Where-Object { $_ -match 'Partition ### .*' } | Select -First 1
$PartitionNumberIndex = $HeaderRow.IndexOf('###')
$TypeIndex = $HeaderRow.IndexOf('Type')
$SizeIndex = $HeaderRow.IndexOf('Size')
$OffsetIndex = $HeaderRow.IndexOf('Offset')
$PartitionArray | Select -Skip 1 | ForEach-Object {
New-Object PSObject -Property @{
Number = [int]$_.SubString($PartitionNumberIndex, 3).Trim();
Type = $_.SubString($TypeIndex, 16).Trim();
Size = Parse-SizeUnit $_.SubString($SizeIndex, 7).Trim();
Offset = Parse-SizeUnit $_.SubString($OffsetIndex).Trim();
}
}
}
function List-Volume {
$VolumeArray = 'list volume' | diskpart | Where-Object { $_ -match 'Volume .+' }
$HeaderRow = $VolumeArray | Where-Object { $_ -match 'Volume ### .*' } | Select -First 1
$VolumeNumberIndex = $HeaderRow.IndexOf('###')
$LetterIndex = $HeaderRow.IndexOf('Ltr')
$LabelIndex = $HeaderRow.IndexOf('Label')
$FileSystemIndex = $HeaderRow.IndexOf('Fs')
$TypeIndex = $HeaderRow.IndexOf('Type')
$SizeIndex = $HeaderRow.IndexOf('Size')
$StatusIndex = $HeaderRow.IndexOf('Status')
$InfoIndex = $HeaderRow.IndexOf('Info')
$VolumeArray | Select -Skip 1 | ForEach-Object {
New-Object PSObject -Property @{
Number = [int]$_.SubString($VolumeNumberIndex, 3).Trim();
Letter = $_.SubString($LetterIndex, 3).Trim();
Label = $_.SubString($StatusIndex, 11).Trim();
FileSystem = $_.SubString($FileSystemIndex, 5).Trim();
Type = $_.SubString($TypeIndex, 10).Trim();
Size = Parse-SizeUnit $_.SubString($SizeIndex, 7).Trim();
Status = $_.SubString($StatusIndex, 9).Trim();
Info = $_.SubString($InfoIndex).Trim();
}
}
}
function Get-ShrinkQueryMax {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)][string]$DriveLetter
)
"select volume $($DriveLetter)", "shrink querymax" | diskpart |
Where-Object { $_ -match 'The maximum number of reclaimable bytes is:.+' } |
ForEach-Object { $_ -split ':' } | Select-Object -Skip 1 | ForEach-Object {
Parse-SizeUnit $_.Trim()
}
}
function Get-DetailDisk {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)][int]$DiskNumber
)
$DiskDetails = "select disk $($DiskNumber)", "detail disk" | diskpart
$Disk = New-Object PSObject -Property @{
DiskId = '';
Type = '';
Status = '';
Path = 0;
Target = 0;
LunId = 0;
LocationPath = '';
CurrentReadOnlyState = '';
ReadOnly = '';
BootDisk = '';
PagefileDisk = '';
HibernationFileDisk = '';
CrashdumpDisk = '';
ClusteredDisk = '';
Volumes = $null;
}
$DiskDetails | ForEach-Object {
if ($_ -match 'Disk ID:') {
$DiskIdParts = $_ -split ':'
$Disk.DiskId = $DiskIdParts[1].Trim()
} elseif ($_ -match 'Type\s+:') {
$TypeParts = $_ -split ':'
$Disk.Type = $TypeParts[1].Trim()
} elseif ($_ -match 'Status\s+:') {
$StatusParts = $_ -split ':'
$Disk.Status = $StatusParts[1].Trim()
} elseif ($_ -match 'Path\s+:') {
$PathParts = $_ -split ':'
$Disk.Path = $PathParts[1].Trim()
} elseif ($_ -match 'Target\s+:') {
$TargetParts = $_ -split ':'
$Disk.Target = $TargetParts[1].Trim()
} elseif ($_ -match 'LUN ID\s+:') {
$LunIdParts = $_ -split ':'
$Disk.LunId = $LunIdParts[1].Trim()
} elseif ($_ -match 'Location Path\s+:') {
$LocPathParts = $_ -split ':'
$Disk.LocationPath = $LocPathParts[1].Trim()
} elseif ($_ -match 'Current Read-only State\s+:') {
$CurrROStateParts = $_ -split ':'
$Disk.CurrentReadOnlyState = $CurrROStateParts[1].Trim()
} elseif ($_ -match 'Read-only\s+:') {
$ReadOnlyParts = $_ -split ':'
$Disk.ReadOnly = $ReadOnlyParts[1].Trim()
} elseif ($_ -match 'Boot Disk\s+:') {
$BootDiskParts = $_ -split ':'
$Disk.BootDisk = $BootDiskParts[1].Trim()
} elseif ($_ -match 'Pagefile Disk\s+:') {
$PagefileDiskParts = $_ -split ':'
$Disk.PagefileDisk = $PagefileDiskParts[1].Trim()
} elseif ($_ -match 'Hibernation File Disk\s+:') {
$HibFileDiskParts = $_ -split ':'
$Disk.HibernationFileDisk = $HibFileDiskParts[1].Trim()
} elseif ($_ -match 'Crashdump Disk\s+:') {
$CrashdumpDiskParts = $_ -split ':'
$Disk.CrashdumpDisk = $CrashdumpDiskParts[1].Trim()
} elseif ($_ -match 'Clustered Disk\s+:') {
$ClusteredDiskParts = $_ -split ':'
$Disk.ClusteredDisk = $ClusteredDiskParts[1].Trim()
}
}
$VolumeArray = $DiskDetails | Where-Object { $_ -match 'Volume .+' }
$HeaderRow = $VolumeArray | Where-Object { $_ -match 'Volume ### .*' } | Select -First 1
$VolumeNumberIndex = $HeaderRow.IndexOf('###')
$LetterIndex = $HeaderRow.IndexOf('Ltr')
$LabelIndex = $HeaderRow.IndexOf('Label')
$FileSystemIndex = $HeaderRow.IndexOf('Fs')
$TypeIndex = $HeaderRow.IndexOf('Type')
$SizeIndex = $HeaderRow.IndexOf('Size')
$StatusIndex = $HeaderRow.IndexOf('Status')
$InfoIndex = $HeaderRow.IndexOf('Info')
$Disk.Volumes = $VolumeArray | Select -Skip 1 | ForEach-Object {
New-Object PSObject -Property @{
Number = [int]$_.SubString($VolumeNumberIndex, 3).Trim();
Letter = $_.SubString($LetterIndex, 3).Trim();
Label = $_.SubString($StatusIndex, 11).Trim();
FileSystem = $_.SubString($FileSystemIndex, 5).Trim();
Type = $_.SubString($TypeIndex, 10).Trim();
Size = Parse-SizeUnit $_.SubString($SizeIndex, 7).Trim();
Status = $_.SubString($StatusIndex, 9).Trim();
Info = $_.SubString($InfoIndex).Trim();
}
}
$Disk
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment