troubleshooting Question

Generating a single table using Powershell

Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America asked on
PowershellOperating Systems
5 Comments3 Solutions114 ViewsLast Modified:
Hello Powershell Experts,

I need some help generating a table to display results of a Powershell script.  The script basically checks our servers for drive space.  The code of the script is as follows:

Get-ADComputer -Filter {OperatingSystem -like '*Windows Server*'} | Select name | sort name | ForEach-Object {
    $PingTest =Test-Connection -ComputerName $_.name -Count 1 -Quiet 
        if ($PingTest -eq "True") {
            Get-WmiObject win32_logicaldisk -ComputerName $_.name -Filter "DriveType=3" -ErrorAction SilentlyContinue | `
                Select-Object `
                    @{n="Server";e={$_.SystemName}},`
                    @{n="Drive Letter";e={$_.DeviceID}},`
                    @{n="Volume Name";e={$_.VolumeName}},`
                    @{n="Total Size (GB)";e={"{0:n2}" -f($_.size/1GB)}},`
                    @{n="Free Space (GB)";e={"{0:n2}" -f($_.FreeSpace/1GB)}},`
                    @{n="Used Space (GB)";e={"{0:n2}" -f(([Int64]$_.size/1gb) - ([Int64]$_.FreeSpace/1gb))}},`
                    @{n="Percentage Used";e={"{0:P2}" -f((1-[Int64]$_.FreeSpace/[Int64]$_.Size))}} 

        }

}

This returns the desired information in a list format.  If I add the format-table commandlet at the end, it almost gives me what I want.  Below is the code and the results:

Get-ADComputer -Filter {OperatingSystem -like '*Windows Server*'} | Select name | sort name | ForEach-Object {
    $PingTest =Test-Connection -ComputerName $_.name -Count 1 -Quiet 
        if ($PingTest -eq "True") {
            Get-WmiObject win32_logicaldisk -ComputerName $_.name -Filter "DriveType=3" -ErrorAction SilentlyContinue | `
                Select-Object `
                    @{n="Server";e={$_.SystemName}},`
                    @{n="Drive Letter";e={$_.DeviceID}},`
                    @{n="Volume Name";e={$_.VolumeName}},`
                    @{n="Total Size (GB)";e={"{0:n2}" -f($_.size/1GB)}},`
                    @{n="Free Space (GB)";e={"{0:n2}" -f($_.FreeSpace/1GB)}},`
                    @{n="Used Space (GB)";e={"{0:n2}" -f(([Int64]$_.size/1gb) - ([Int64]$_.FreeSpace/1gb))}},`
                    @{n="Percentage Used";e={"{0:P2}" -f((1-[Int64]$_.FreeSpace/[Int64]$_.Size))}} | Format-Table

        }

}

Results:
Server    Drive Letter Volume Name Total Size (GB) Free Space (GB) Used Space (GB) Percentage Used
------    ------------ ----------- --------------- --------------- --------------- ---------------
Server1   C:                       59.90           14.44           45.46           75.90 %        

Server    Drive Letter Volume Name Total Size (GB) Free Space (GB) Used Space (GB) Percentage Used
------    ------------ ----------- --------------- --------------- --------------- ---------------
WEBSERVER C:           OS          120.00          29.86           90.14           75.11 %        
WEBSERVER E:           DATA        713.58          598.74          114.85          16.09 %        


I would like the results to only have one set of column headers.  How would I do this?

Server          Drive Letter    Volume Name Total Size (GB) Free Space (GB) Used Space (GB) Percentage Used
------              ------------          ----------- --------------- --------------- --------------- ---------------
Server1         C:                                    59.90           14.44           45.46           75.90 %        
WEBSERVER C:                    OS          120.00          29.86           90.14           75.11 %        
WEBSERVER E:                     ATA        713.58          598.74        114.85          16.09 %        


Any help would be greatly appreciated.

Thanks,
Nick
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 3 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros