当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell:检查可用磁盘空间和磁盘使用情况

作者:精品下载站 日期:2024-12-14 23:03:50 浏览:16 分类:玩电脑

PowerShell:检查可用磁盘空间和磁盘使用情况


在本文中,我们将向您展示如何使用 PowerShell 检查本地或远程 Windows 主机上的可用磁盘空间和磁盘使用情况。另外,请考虑如果超出可用空间阈值,如何通过弹出通知或电子邮件通知管理员。

如何使用 WMI 和 PowerShell 检查 Windows 上的驱动器可用空间?

您可以使用 Win32_ologicalDisk WMI 类获取有关 Windows 中逻辑驱动器的信息。

下面的命令将显示有关计算机上逻辑驱动器的所有信息:

Get-WmiObject -Class Win32_LogicalDisk

如果您使用的是较新的 PowerShell Core 7.x,请注意,此 PowerShell 版本不支持 WMI(因为 PowerShell Core 基于 .Net Core)。如果您尝试运行 Get-WmiObject 命令,您将看到以下错误:

The term 'Get-WmiObject' is not recognized as a name of a cmdlet, function, script file, or executable program

。使用 CIM 代替 WMI,例如:

Get-CimInstance win32_logicaldisk

[玩转系统] PowerShell:检查可用磁盘空间和磁盘使用情况

FreeSpace 属性包含每个驱动器上剩余的可用空间量(以字节为单位)。为了更方便,您可以将其转换为 GB,并以 % 形式显示每个逻辑磁盘上的可用空间量(作为可用空间与磁盘总大小的比率)。您可以使用以下 PowerShell 脚本:

Get-WmiObject -Class Win32_LogicalDisk |
Select-Object -Property DeviceID, VolumeName, @{Label='FreeSpace (Gb)'; expression={($_.FreeSpace/1GB).ToString('F2')}},
@{Label='Total (Gb)'; expression={($_.Size/1GB).ToString('F2')}},
@{label='FreePercent'; expression={[Math]::Round(($_.freespace / $_.size) * 100, 2)}}|ft

[玩转系统] PowerShell:检查可用磁盘空间和磁盘使用情况

该脚本显示逻辑驱动器列表、其大小和可用空间百分比。

要在 PowerShell Core 中使用此脚本,只需替换

Get-WmiObject

Get-CimInstance

如果您不想简单地显示有关磁盘上可用空间的信息,而是在可用空间少于指定阈值时采取一些操作(发送电子邮件或显示弹出消息),则可以使用下面的 PowerShell 脚本:

$percentWarning = 20
$percentCritcal = 5
$ListDisk = Get-WmiObject -Class Win32_LogicalDisk
Foreach($Disk in $ListDisk){
if ($Disk.size -ne $NULL) {
$DiskFreeSpace = ($Disk.freespace/1GB).ToString('F2')
$DiskFreeSpacePercent = [Math]::Round(($Disk.freespace/$Disk.size) * 100, 2)
if($DiskFreeSpacePercent -lt $percentWarning){
$Message= "Warning!"
if($DiskFreeSpacePercent -lt $percentCritcal){
$Message= "Alert!"
}
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("Disk $($Disk.DeviceID) has only $DiskFreeSpace GB of free space left",0,$Message,48)
}
}
}

[玩转系统] PowerShell:检查可用磁盘空间和磁盘使用情况

该脚本设置磁盘上剩余可用空间的阈值 - 5% 和 20%。如果任何磁盘上的可用空间量低于指定值,则会显示模式信息窗口。您可以将其显示为弹出通知或立即运行磁盘清理工具(

cleanmgr.exe

)。

如果您想向管理员发送电子邮件解决问题,您可以使用 Send-MailMessage cmdlet 通过 SMTP 服务器(可能是 Exchange 主机或任何其他 SMTP 服务,甚至内置的 Windows Server SMTP 角色也可以)发送电子邮件:

Send-MailMessage -To “[email protected]” -From “$env:[email protected]” -Subject “Insufficient disk space on server $env:computername” -Body “Disk $($Disk.DeviceID) has only $DiskFreeSpace GB left” -Credential (Get-Credential) -SmtpServer smtp.a-d.site -Port 587

在此示例中,您需要输入凭据才能以交互方式连接到 SMTP 主机。您可以将 SMTP 主机配置为接受来自受信任主机的消息而无需进行身份验证,或者使用文件中保存的凭据配置 SMTP 身份验证(无法使用托管服务帐户发送电子邮件)。

您可以使用任务计划程序定期运行 PowerShell 脚本,也可以将其配置为 Windows 服务。如果此 Windows 主机上没有足够的可用空间,管理员将收到通知。

通过 PowerShell 从远程 Windows 主机获取可用磁盘空间

Invoke-Command cmdlet 可用于运行 PS 脚本来检查远程计算机上的剩余可用空间。

Invoke-Command -ComputerName srv01,srv02,srv03 -FilePath "C:\PS\checkfreespace.ps1"

如果您要检查可用空间量的服务器位于您的域中,您可以使用 Get-ADComputer cmdlet 从 Active Directory 获取它们的列表,并对每个主机运行脚本:

$computers = (Get-ADComputer -Filter 'operatingsystem -like "*Windows Server*" -and enabled -eq "true"').Name
Invoke-Command -ComputerName $computers -FilePath "C:\PS\checkfreespace.ps1" -ErrorAction SilentlyContinue

您还可以使用 RemoteWMI 从远程计算机获取 WMI 数据:

Get-WmiObject -Class Win32_logicalDisk -ComputerName srv01,srv02,srv03

本指南介绍了监控磁盘空间的最简单的自制解决方案。如果你有很多主机需要监控,最好使用功能齐全的监控系统(如Zabbix、Icinga、PRTG、Nagios等)。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯