列出物理驱动器 space

List physical drive space

我有大约 200 台服务器,我需要获取磁盘 space 和逻辑驱动器 space 详细信息(免费 space,已用 space 和总计 space).

这是我的 PowerShell 查询。

$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 文件中给我输出。

这是我所说的例子。

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

如果你觉得我无法解释这个问题。让我再试一次。

Export-Csv 假定列表中的所有对象都是统一的,即它们都具有相同的属性,只是具有不同的值。它采用第一个元素的属性来确定要导出的内容。要么确保所有对象都具有相同的属性,要么使用不同的输出方法,例如将所有磁盘信息放入每个主机的数组中并将其写入输出文件,例如像这样:

foreach ($machine in $servers) {
  $disks = @($machine)
  $disks += Get-WmiObject -Computer $machine -Class Win32_DiskDrive |
            ForEach-Object { [Math]::Round($_.Size/1GB) }
  $disks -join ',' | Add-Content 'C:\path\to\output.csv'
}

顺便说一句,您不需要多个 WMI 查询,因为第一个查询已经 returns 所有磁盘,包括它们的大小。

我想指出,有几个地方可能会 发生错误。此答案的目的是解决 headers 的未知数量。我建议您 运行 在尝试集成它之前先看看它试图向您展示什么。

# 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

我有以下脚本:
我正在寻求帮助将输出转换为 Excel 格式

$Pather = Get-Content C:\Servers.txt
foreach($Path in $Pather)
{
  (Get-Acl $Path).access | ft $path,IdentityReference,FileSystemRights,AccessControlType,IsInherited -auto
}