之前在國外站點看到一個很有趣的偏GUI風格式的圖式,覺得挺不錯
閑暇之餘句決定自己用PowerShell做一個。
代碼如下:
#region Get-DiskInfo
Function Get-DiskInfo
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
[Parameter(Mandatory=$false,Position=0,ValueFromPipeline=$true)]
[Alias("CN")][String[]]$ComputerName=$Env:COMPUTERNAME,
[Parameter(Mandatory=$false,Position=0,HelpMessage="a")]
[Alias("GI")][Switch]$GraphInfo,
[Parameter(Mandatory=$false,Position=0,HelpMessage="a")]
[Alias("RI")][Switch]$RawInfo
)
$Width = $Host.UI.RawUI.WindowSize.Width
foreach($cn in $ComputerName)
{ Trap
{
Write-Host "Do not connect to this computer $ComputerName"
}
if($PSCmdlet.ShouldProcess("get the disk information"))
{
if($GraphInfo)
{Get-ColorDiskInfo}
}
if($PSCmdlet.ShouldProcess("get the disk information"))
{
if($RawInfo)
{Get-RawDiskInfo}
}
}
}
#endregion
#region Get-RawDiskInfo
Function Get-RawDiskInfo
{
# $PSCmdlet.WriteVerbose("Get the information of disk drive as raw mode.")
Write-Host "=================== Raw Data ==================="
$diskInfo|Select-Object @{Name = "Computer Name";Expression={$cn}} `
,@{Name = "Drive Name";Expression={$_.DeviceID}} `
,@{Name = "Volume Name";Expression={if(($_.VolumeName) -le 0){$_.DeviceID}else{$_.VolumeName}}} `
,@{Name = "Free Space";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) + " GB"}} `
,@{Name = "Used Space";Expression={"{0:N2}" -f (($_.Size - $_.FreeSpace)/1GB) + " GB"}} `
,@{Name = "Total Disk Size";Expression={"{0:N2}" -f ($_.Size/1GB) + " GB"}} `
| Format-Table -AutoSize
}
#endregion
#region Get-ColorDiskInfo
Function Get-ColorDiskInfo
{
$PSCmdlet.WriteVerbose("Get the information of disk drive as color mode.")
$Threshold = 40
Write-Host "==================== Graph ===================="
Write-Host " " -BackgroundColor Red -NoNewline
Write-Host " Used Space | " -NoNewline
Write-Host " " -BackgroundColor Green -NoNewline
Write-Host " Free Space"
Write-Host
foreach($disk in $diskInfo)
{
$DiskDrive = $disk.DeviceID
$FreeDiskSize = $disk.FreeSpace/$disk.Size
$UsedDiskSize = ($disk.Size-$disk.FreeSpace)/$disk.Size
#manipulate strings
$FreeDiskPercent = "{0:P2}" -f $FreeDiskSize
$FreeDiskSpace = "{0:N2}" -f $($disk.FreeSpace/1GB)
$TotalDiskSize = "{0:N2}" -f $($disk.Size/1GB)
[int]$UsedDiskSpace = "{0:N2}" -f (($disk.Size - $disk.FreeSpace)/1GB)
Write-Host "[$cn] " -NoNewline
Write-Host "$DiskDrive " -NoNewline
Write-Host (" " *($UsedDiskSize*$Threshold)) -BackgroundColor Red -NoNewline
Write-Host (" " *($FreeDiskSize*$Threshold)) -BackgroundColor Green -NoNewline
Write-Host " $FreeDiskPercent Free"
Write-Host
}
}
#endregion
運作效果如下 :)