PowerShell 技能连载 - 检查(本地和远程的)硬盘容量

WMI 可以提供硬盘的容量和剩余空间。PowerShell 会用返回这样的友好信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PS C:\> # local
PS C:\> Get-HardDriveSize

DriveLetter Free(GB) Size(GB) Percent
----------- -------- -------- -------
C: 823,7 942,3 87,4


PS C:\> # remote
PS C:\> Get-HardDriveSize -ComputerName server2 -Credential server2\Tobias

DriveLetter Free(GB) Size(GB) Percent
----------- -------- -------- -------
C: 87,3 436,9 20
D: 5,3 25 21,3

以下是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function Get-HardDriveSize
{
param
(
$ComputerName,

$Credential
)

# get calculated properties:
$prop1 = @{
Name = 'DriveLetter'
Expression = { $_.DeviceID }
}

$prop2 = @{
Name = 'Free(GB)'
Expression = { [Math]::Round(($_.FreeSpace / 1GB),1) }
}

$prop3 = @{
Name = 'Size(GB)'
Expression = { [Math]::Round(($_.Size / 1GB),1) }
}

$prop4 = @{
Name = 'Percent'
Expression = { [Math]::Round(($_.Freespace * 100 / $_.Size),1) }
}

# get all hard drives
Get-CimInstance -ClassName Win32_LogicalDisk @PSBoundParameters -Filter "DriveType=3" |
Select-Object -Property $prop1, $prop2, $prop3, $prop4
}

PowerShell 技能连载 - 检查(本地和远程的)硬盘容量

http://blog.vichamp.com/2016/09/07/checking-hard-drive-size-local-and-remote/

作者

吴波

发布于

2016-09-07

更新于

2022-07-06

许可协议

评论