using powershell to check the hard disk information

本文介绍了如何利用PowerShell编写脚本来查看硬盘信息。通过提供的代码示例,展示了使用PowerShell实现这一功能的过程及其运行效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前在国外站点看到一个很有趣的偏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

 

运行效果如下 :)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值