File: //var/opt/nydus/ops/customer_local_ops/operating_system/powershell/get_disk_info.ps1
# Ensure Temp directory exists
if (!(Test-Path -Path "C:\Temp")) {
New-Item -ItemType Directory -Path "C:\Temp" | Out-Null
}
# Function to run diskpart and capture output
function Run-DiskpartCommand {
param ($Command)
$scriptPath = "C:\Temp\diskpart_script.txt"
$outputPath = "C:\Temp\diskpart_output.txt"
# Write the command to the script file
$Command | Set-Content -Path $scriptPath -Encoding ASCII
# Run diskpart and capture output
Start-Process -FilePath "diskpart" -ArgumentList "/s $scriptPath" -RedirectStandardOutput $outputPath -NoNewWindow -Wait
# Return the output
Get-Content -Path $outputPath -Encoding ASCII
}
# Get the list of disks
$diskListOutput = Run-DiskpartCommand "list disk"
# Parse disk numbers
$disks = @()
foreach ($line in $diskListOutput) {
if ($line -match "(?i)disk\s+(\d+)") {
$disks += $matches[1]
}
}
# Initialize results array
$diskDetails = @()
# Loop through each disk and get details
foreach ($disk in $disks) {
$detailDiskOutput = Run-DiskpartCommand "select disk $disk`r`ndetail disk"
$params = @{}
$volumes = @()
foreach ($line in $detailDiskOutput) {
if ($line -match "^\s*([^:]+)\s*:\s*(.+)$") {
$params[$matches[1].Trim()] = $matches[2].Trim()
}
elseif ($line -match "Volume\s+(\d+)\s+([A-Z]?)\s+(.{0,11})\s+(NTFS|FAT32|exFAT|ReFS)?\s+(Partition|Removable|Mirror|Simple)?\s+(\d+\s\S+)\s+(Healthy|Failed Rd|No Media|Rebuild|Unusable)\s*(.*)?$") {
$volume = [PSCustomObject]@{
VolumeNumber = $matches[1]
DriveLetter = $matches[2].Trim()
Label = $matches[3].Trim()
FileSystem = $matches[4]
Type = $matches[5]
Size = $matches[6]
Status = $matches[7]
Info = if ($matches[8]) { $matches[8].Trim() } else { "" }
}
# Retrieve volume size details
if ($volume.DriveLetter -ne "") {
$volumeData = Get-Volume -DriveLetter $volume.DriveLetter | Select-Object Size, SizeRemaining
} else {
$volumeData = Get-Volume | Where-Object { $_.FileSystem -eq $volume.FileSystem -and $_.Size -gt 0 } | Select-Object -First 1
}
# Populate size information
if ($volumeData) {
$totalSizeBytes = $volumeData.Size
$freeSpaceBytes = $volumeData.SizeRemaining
$usedSpaceBytes = $totalSizeBytes - $freeSpaceBytes
$volume | Add-Member -MemberType NoteProperty -Name TotalSizeBytes -Value $totalSizeBytes
$volume | Add-Member -MemberType NoteProperty -Name FreeSpaceBytes -Value $freeSpaceBytes
$volume | Add-Member -MemberType NoteProperty -Name UsedSpaceBytes -Value $usedSpaceBytes
$volume | Add-Member -MemberType NoteProperty -Name TotalSizeMB -Value ([math]::Round($totalSizeBytes / 1MB, 2))
$volume | Add-Member -MemberType NoteProperty -Name FreeSpaceMB -Value ([math]::Round($freeSpaceBytes / 1MB, 2))
$volume | Add-Member -MemberType NoteProperty -Name UsedSpaceMB -Value ([math]::Round($usedSpaceBytes / 1MB, 2))
$volume | Add-Member -MemberType NoteProperty -Name TotalSizeGB -Value ([math]::Round($totalSizeBytes / 1GB, 2))
$volume | Add-Member -MemberType NoteProperty -Name FreeSpaceGB -Value ([math]::Round($freeSpaceBytes / 1GB, 2))
$volume | Add-Member -MemberType NoteProperty -Name UsedSpaceGB -Value ([math]::Round($usedSpaceBytes / 1GB, 2))
}
$volumes += $volume
}
}
$diskDetails += [PSCustomObject]@{
DiskNumber = $disk
Parameters = [PSCustomObject]$params
Volumes = $volumes
}
}
# Get the list of volumes
$volumeListOutput = Run-DiskpartCommand "list volume"
# Parse volume details
$volumesDetails = @()
foreach ($line in $volumeListOutput) {
if ($line -match "Volume\s+(\d+)\s+([A-Z]?)\s+(.{0,11})\s+(NTFS|FAT32|exFAT|ReFS)?\s+(Partition|Removable|Mirror|Simple)?\s+(\d+\s\S+)\s+(Healthy|Failed Rd|No Media|Rebuild|Unusable)\s*(.*)?$") {
$volumesDetails += [PSCustomObject]@{
VolumeNumber = $matches[1]
DriveLetter = $matches[2].Trim()
Label = $matches[3].Trim()
FileSystem = $matches[4]
Type = $matches[5]
Size = $matches[6]
Status = $matches[7]
Info = if ($matches[8]) { $matches[8].Trim() } else { "" }
}
}
}
$Partitioninfo = Get-wmiobject -Query "Select * from Win32_volume" | Select-Object DriveLetter, DeviceID
$PartDetails = @()
foreach ($pinfo in $Partitioninfo)
{
$diskID = ($pinfo.DeviceID -match '(?<={)(.*?)(?=})')
$PartDetails += [PSCustomObject]@{
DiskLetter = $pinfo.DriveLetter
DiskID = $Matches[1]
}
}
# Create a container object with named sections
$outputContainer = @{
"DiskSummary" = $diskDetails
"VolumesSummary" = $volumesDetails
"PartitionSummary" = $PartDetails
}
# Convert to JSON and output
$outputContainer | ConvertTo-Json -Depth 5 | Out-Host
# Clean up temporary files
Remove-Item -Path "C:\Temp\diskpart_script.txt" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "C:\Temp\diskpart_output.txt" -Force -ErrorAction SilentlyContinue