We help IT Professionals succeed at work.

Generating a single table using Powershell

109 Views
1 Endorsement
Last Modified: 2019-03-21
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))}} 

        }

}

Open in new window


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

        }

}

Open in new window


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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I changed up the code as suggested and added an email function.  I almost got what I need.  Here is the code:
$Date = Get-Date
$Body = get-ADComputer -Filter {OperatingSystem -like '*Windows Server*'} | where {$_.name -ne "Edge"} | 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 -AutoSize 

$SendMailArguments = @{
    SmtpServer = "SMTP1.server.com"
    Port = 25
    To = "serveradmin@mydomain.com"
    From = "ServerAdmin@mydomain.gov"
    Subject = "$($Date) Drive Space report"
    Body = $Body | Out-String
}

Open in new window


Here is the report:

Emailed Output
My question now is how can I get the table in the body of the email to be nice and clean (properly lined up)?
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Then you need to create HTML and send the email as such.
$Date = Get-Date
$diskReport = Get-ADComputer -Filter {OperatingSystem -like '*Windows Server*'} | Where-Object {$_.name -ne "Edge"} | Select-Object -Property Name | Sort-Object -Property Name | ForEach-Object {
	If (Test-Connection -ComputerName $_.name -Count 1 -Quiet) {
		Get-WmiObject Win32_LogicalDisk -ComputerName $_.Name -Filter "DriveType=3" -ErrorAction SilentlyContinue |
			Select-Object -Property `
				@{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))}} 
	}
}

$SendMailArguments = @{
	SmtpServer = "SMTP1.server.com"
	Port = 25
	To = "serveradmin@mydomain.com"
	From = "ServerAdmin@mydomain.gov"
	Subject = "$($Date) Drive Space report"
	BodyAsHtml = $true
	Body = -join ($diskReport | ConvertTo-Html)
}

Open in new window

Brent ChallisPrincipal: IT
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for all of your help.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.