List physical drive space

我的梦境 提交于 2019-12-02 07:38:55

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 ....

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

Export-Csv assumes that all objects in your list are uniform, i.e. that they all have the same properties, just with different values. It takes the properties of the first element to determine what to export. Either make sure that all objects have the same properties, or use a different output method, for instance putting all disk information in an array per host and write that to the output file, e.g. like this:

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'
}

BTW, you don't need multiple WMI queries, since the first one already returns all disks including their sizes.

tamil

I Have below script:
And I am looking for help to convert the output to Excel format

$Pather = Get-Content C:\Servers.txt
foreach($Path in $Pather)
{
  (Get-Acl $Path).access | ft $path,IdentityReference,FileSystemRights,AccessControlType,IsInherited -auto
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!