简体   繁体   English

PowerShell 2.0磁盘空间检查

[英]PowerShell 2.0 Disk Space check

Below is the code for checking the disk space: 下面是检查磁盘空间的代码:

$disks = Get-WmiObject -ComputerName WDSMS01 -Namespace root\cimv2 -Query "Select * from Win32_LogicalDisk where DriveType=3"

$disks.DeviceID
$dsql01 = @()

foreach($db in $disks)
{
    $dsql = New-Object PSObject
    $dsql | Add-Member -MemberType NoteProperty -Name "Drive Name" -value $db.DeviceID
    $dsql | Add-Member -MemberType NoteProperty -Name "Total Space(GB)" -value ($db.Size/1GB)
    $dsql | Add-Member -MemberType NoteProperty -Name "Free Space(GB)" -value ($db.FreeSpace/1GB)
    $dsql01 += $dsql
}

$dsql01

Is it possible to limit the decimals by 2 digits? 是否可以将小数位数限制为2位? Also, add another column that computes free space in % format. 此外,添加另一列以%格式计算可用空间。

Thanks. 谢谢。

This depends a bit on what exactly you need. 这取决于您到底需要什么。 You can use [Math]::Round to round off everything after two digits: 您可以使用[Math]::Round舍入两位数后的所有内容:

[Math]::Round($db.Size/1GB, 2)

but that can leave you with numbers that have less than two digits if they are followed by zeroes, eg 14.6 which doesn't look as nice in a table. 但这会给您留下少于两位数的数字(如果后面跟有零),例如14.6在表中看起来不太好。

The other option is to format the numbers: 另一种选择是格式化数字:

($db.Size/1GB).ToString('0.00')

which will always use the same number of decimal digits, but results in a string, so performing further calculations can be hindered by that. 它将始终使用相同数量的十进制数字, 但会产生一个字符串,因此可能会妨碍执行进一步的计算。

As for your second question, that's actually trivial, just add another Add-Member call: 至于第二个问题,这实际上很简单,只需添加另一个Add-Member调用即可:

$dsql | Add-Member NoteProperty 'Free Space (%)' (100*$db.FreeSpace/$db.Size)

Also you can (in PowerShell v2) put all those in a single New-Object call: 您还可以(在PowerShell v2中)将所有这些都放在一个New-Object调用中:

$dsql = New-Object PSObject -Property @{
    'Drive Name' = $db.DeviceID
    'Total Space (GB)' = $db.Size / 1GB
    'Free Space (GB)' = $db.FreeSpace / 1GB
    'Free Space (%)' = 100 * $db.FreeSpace / $db.Size
}

And while we're at it, foreach loops are so not PowerShell-y. 而且, foreachforeach循环并不是PowerShell-y。 Let's rewrite that with a pipeline: 让我们用管道重写它:

$disks = Get-WmiObject -ComputerName WDSMS01 -Namespace root\cimv2 -Query "Select * from Win32_LogicalDisk where DriveType=3"
$disks.DeviceID
$dsql01 = $disks | ForEach-Object {
    New-Object PSObject -Property @{
        'Drive Name' = $_.DeviceID
        'Total Space (GB)' = $_.Size / 1GB
        'Free Space (GB)' = $_.FreeSpace / 1GB
        'Free Space (%)' = 100 * $_.FreeSpace / $_.Size
    }
}

use formatter example: 使用格式化程序示例:

PS>"{0:N2}" -f (11531055554 /1GB)
10,74

to get percent : 获得百分比:

 $pfree=$dsql.free *100 /$dsql.total
 $dsql| Add-Member -MemberType NoteProperty -Name "pFree" -value $pfree

尝试这个:

-value ("{0:0.00}" -f ($db.Size/1GB))

-value ("{0:0.00}" -f ($db.FreeSpace/1GB))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM