列出物理驱动器空间 [英] List physical drive space

查看:69
本文介绍了列出物理驱动器空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约有200台服务器,我需要获取磁盘空间&逻辑驱动器空间详细信息(可用空间,已用空间和总空间).

I have around 200 servers and I need to get the disk space & logical drive space details (free space, used space & total space).

这是我的PowerShell查询.

Here is my PowerShell query.

$infoObjects = New-Object PSObject
foreach ($machine in $servers) {
  $counts = Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive" -ComputerName $machine
  $total_disk =  @($counts).Count
  $i = 0
  $total_disk = $total_disk -1

  for (; $i -le $total_disk; $i++) {
    $a = $i
    $a  = Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive WHERE DeviceID='\\\\.\\PHYSICALDRIVE$i'" -ComputerName $machine

    $b = $i
    $b = [math]::round($a.size /1GB)

    Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Physical_Disk $i" -Value $b
  }
  $infoObject | Export-Csv -Path Inventory.csv -Append -Force -NoTypeInformation 
}

这给了我想要的输出,但是如果某些serverd的磁盘多于一个磁盘或逻辑驱动器多于输出,则该输出将卡在第一台服务器的驱动器数量中.它没有在其他服务器的其余驱动器的CSV文件中提供输出.

It is giving me desired output but if some of serverd have more than one disk or have more logical drive than the output is stuck with that number of drives of first server. It is not giving me output in the CSV file of rest of the drives of other servers.

这是我在说什么的例子.

Here is the example about what I am saying.

ServerName Physical_Disk 0 Physical_Disk 1 Physical_Disk 2 Physical_Disk 3
Server1 100 20  40
Server2 85
Server3 60  450 200 420
Server4 60
Server5 60                  
Server10    55  20  40

如果看来我无法解释问题.让我再试一次.

If it seems like I am not able to explain the problem. Let me try again.

  • 第一台服务器在我的输出文件(CSV)中有2个物理驱动器.
  • 第二台服务器还具有2个物理驱动器,它们也都在CSV文件中.
  • 但是第三台服务器有2个以上的驱动器,并且输出中仅显示2个驱动器.

推荐答案

我想指出,在某些地方可能发生错误 .此答案的目的是解决未知数量的标头.我建议您就地运行此程序,以在尝试集成此程序之前查看它试图向您显示的内容.

I would like to point that there is several places where error could occur. The purpose of this answer is to address the unknown number of headers. I would recommend that you run this in place to see what it is trying to show you before you attempt to integrate this.

# Gather all wmi drives query at once
$alldisksInfo = Get-WmiObject –query "SELECT * FROM Win32_DiskDrive" -ComputerName $servers -ErrorAction SilentlyContinue | Group-Object __Server

# Figure out the maximum number of disks
$MaximumDrives = $alldisksInfo | Measure-Object -Property Count -Maximum | Select-Object -ExpandProperty Maximum

# Build the objects, making empty properties for the drives that dont exist for each server where need be. 
$servers | ForEach-Object{
    # Clean the hashtable
    $infoObject = [ordered]@{}

    # Populate Server
    $infoObject.Server = $_    

    # Add other simple properties here
    $infoObject.PhysicalMemory = (Get-WmiObject Win32_PhysicalMemory -ComputerName $infoObject.Server | Measure-Object Capacity -Sum).Sum/1gb

    # Add the disks information from the $diskInfo Array
    $serverDisksWMI = $alldisksInfo | Where-Object{$_.Name -eq $infoObject.Server} | Select-Object -ExpandProperty Group

    for($diskIndex =0; $diskIndex -lt $MaximumDrives;$diskIndex++){
        $infoObject."PhysicalDisk$diskIndex" = [Math]::Round(($serverDisksWMI | Where-Object{($_.DeviceID -replace "^\D*") -eq $diskIndex} | Select -Expand Size)/1GB)
    }

    # Create the custom object now.
    New-Object -TypeName psobject -Property $infoObject
} # | Export-Csv ....

由于它使用管道,因此可以在此末尾轻松添加export-CSV.

Since this uses the pipeline you can easily add export-CSV on the end of this.

这篇关于列出物理驱动器空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆