Link to home
Start Free TrialLog in
Avatar of ndalmolin_13
ndalmolin_13Flag 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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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:

User generated image
My question now is how can I get the table in the body of the email to be nice and clean (properly lined up)?
Avatar of oBdA
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all of your help.