积极答复者
请教一下如何用Powershell查询磁盘使用率%

问题
答案
-
你好 谷先生,
首先很高兴你能够解决了你的问题。
由于wmi命令运行速度更快所以我推荐了您使用wmi命令,当然get-psdrive也可以满足你的需求。
第二个我推荐给你脚本,你可以尝试通过模块化的方法加入到机器中,然后在以后的使用中只需要运行“get-diskfree -format”就可以实现你的需求了。
使用get-psdrive确实是无法直接获得分区total size属性的,你需要将used和free相加。
如果你的问题已经被解决,请尝试标记为答案,因为这会帮助到社区中其他的人。
最好的祝福,
Lee
Just do it.
- 已标记为答案 谷青松 2019年9月15日 8:51
全部回复
-
你好,
谢谢你的提问。
你可以使用以下命令来完成你所需要的。
$pc = "localhost" $disks = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2" -computername $pc $results = foreach ($disk in $disks) { if ($disk.Size -gt 0) { $size = [math]::round($disk.Size/1GB, 0) $free = [math]::round($disk.FreeSpace/1GB, 0) [PSCustomObject]@{ Drive = $disk.Name Name = $disk.VolumeName "Total Disk Size" = $size "Free Disk Size" = "{0:N0} ({1:P0})" -f $free, ($free/$size) } } } # Sample outputs $results | Out-GridView
第一行$pc=后面可以换成你所需要查询的计算机的名称,如果是本地计算机你可以使用上述例子中的localhost,如果是远程计算机请使用远程计算机的名称。
最好的祝福,
Lee
Just do it.
-
此外,你在github收藏过一个分享的powershell function
在使用前请先加载以下命令:
function Get-DiskFree { [CmdletBinding()] param ( [Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias('hostname')] [Alias('cn')] [string[]]$ComputerName = $env:COMPUTERNAME, [Parameter(Position=1, Mandatory=$false)] [Alias('runas')] [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty, [Parameter(Position=2)] [switch]$Format ) BEGIN { function Format-HumanReadable { param ($size) switch ($size) { {$_ -ge 1PB}{"{0:#.#'P'}" -f ($size / 1PB); break} {$_ -ge 1TB}{"{0:#.#'T'}" -f ($size / 1TB); break} {$_ -ge 1GB}{"{0:#.#'G'}" -f ($size / 1GB); break} {$_ -ge 1MB}{"{0:#.#'M'}" -f ($size / 1MB); break} {$_ -ge 1KB}{"{0:#'K'}" -f ($size / 1KB); break} default {"{0}" -f ($size) + "B"} } } $wmiq = 'SELECT * FROM Win32_LogicalDisk WHERE Size != Null AND DriveType >= 2' } PROCESS { foreach ($computer in $ComputerName) { try { if ($computer -eq $env:COMPUTERNAME) { $disks = Get-WmiObject -Query $wmiq ` -ComputerName $computer -ErrorAction Stop } else { $disks = Get-WmiObject -Query $wmiq ` -ComputerName $computer -Credential $Credential ` -ErrorAction Stop } if ($Format) { # Create array for $disk objects and then populate $diskarray = @() $disks | ForEach-Object { $diskarray += $_ } $diskarray | Select-Object @{n='Name';e={$_.SystemName}}, @{n='Vol';e={$_.DeviceID}}, @{n='Size';e={Format-HumanReadable $_.Size}}, @{n='Used';e={Format-HumanReadable ` (($_.Size)-($_.FreeSpace))}}, @{n='Avail';e={Format-HumanReadable $_.FreeSpace}}, @{n='Use%';e={[int](((($_.Size)-($_.FreeSpace))` /($_.Size) * 100))}}, @{n='FS';e={$_.FileSystem}}, @{n='Type';e={$_.Description}} } else { foreach ($disk in $disks) { $diskprops = @{'Volume'=$disk.DeviceID; 'Size'=$disk.Size; 'Used'=($disk.Size - $disk.FreeSpace); 'Available'=$disk.FreeSpace; 'FileSystem'=$disk.FileSystem; 'Type'=$disk.Description 'Computer'=$disk.SystemName;} # Create custom PS object and apply type $diskobj = New-Object -TypeName PSObject ` -Property $diskprops $diskobj.PSObject.TypeNames.Insert(0,'BinaryNature.DiskFree') Write-Output $diskobj } } } catch { # Check for common DCOM errors and display "friendly" output switch ($_) { { $_.Exception.ErrorCode -eq 0x800706ba } ` { $err = 'Unavailable (Host Offline or Firewall)'; break; } { $_.CategoryInfo.Reason -eq 'UnauthorizedAccessException' } ` { $err = 'Access denied (Check User Permissions)'; break; } default { $err = $_.Exception.Message } } Write-Warning "$computer - $err" } } } END {} }
然后就可以使用get-diskfree这条命令了:
get-diskfree -format
最好的祝福,
Lee
Just do it.
-
探讨一下我这边Powershell命令
PS C:\Windows\system32> Get-WmiObject -Class Win32_LogicalDisk -Filter 'DeviceID = "C:"'| select DeviceID,@{name='Free%'
;Expression = {[int]($_.Freespace*100/$_.Size)}}DeviceID Free%
-------- -----
C: 77这一整条命令查询C盘输出得到磁盘空间可用率,但是字符太长必须少于84字符。我想测试一下用Get-PSDrive命令查询C盘但是这命令没有Size总磁盘大小所以怎么用成了问题。
能否帮助我一起研究一下?
谷青松
- 已编辑 谷青松 2019年9月12日 14:02 修改
-
用这个Powershell脚本命令:get-psdrive c | select @{Name="Free%"; Expression={$_.Free*100/($_.Used+$_.Free)}}
PS C:\Windows\system32> get-psdrive c | select @{Name="Free%"; Expression={$_.Free*100/($_.Used+$_.Free)}}
Free%
-----
77.3255632908801这个字符在84字符内应该可以了,要求只查询C盘输出磁盘空间可用率
谷青松
- 已编辑 谷青松 2019年9月12日 14:31 修改
-
你好 谷先生,
首先很高兴你能够解决了你的问题。
由于wmi命令运行速度更快所以我推荐了您使用wmi命令,当然get-psdrive也可以满足你的需求。
第二个我推荐给你脚本,你可以尝试通过模块化的方法加入到机器中,然后在以后的使用中只需要运行“get-diskfree -format”就可以实现你的需求了。
使用get-psdrive确实是无法直接获得分区total size属性的,你需要将used和free相加。
如果你的问题已经被解决,请尝试标记为答案,因为这会帮助到社区中其他的人。
最好的祝福,
Lee
Just do it.
- 已标记为答案 谷青松 2019年9月15日 8:51