Avatar of ndalmolin_13
ndalmolin_13
Flag for United States of America asked on

Generating a single table using Powershell

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
PowershellOperating Systems

Avatar of undefined
Last Comment
ndalmolin_13

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ndalmolin_13

ASKER
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)?
oBdA

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

SOLUTION
Brent Challis

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ndalmolin_13

ASKER
Thanks for all of your help.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23