### Getting all domain-joined Server by Names
 
$Computer = $env:COMPUTERNAME
$SendIt = $Null
$Email= @("diskspace@");
$IPAddy = Invoke-RestMethod -Uri ('http://ipinfo.io/'+(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content) | Select ip
 
### Get only DriveType 3 (Local Disks) foreach Server
 
ForEach ($s in $Computer)
 
{$Report=Get-WmiObject win32_logicaldisk -ComputerName $s -Filter "Drivetype=3" -ErrorAction SilentlyContinue | Where-Object {($_.freespace/$_.size) -le '0.1'}}
$View=($Report.DeviceID -join ",").Replace(":","")
$Space =  Get-WmiObject win32_logicaldisk -ComputerName $s -Filter "Drivetype=3"  | Select-Object -Property DeviceID,VolumeName,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} | Export-Csv C:\temp\space.csv -NoTypeInformation
$Freespace = Get-Content -Path C:\temp\space.csv
### Send Mail if $Report (<=10%) is true
 
If ($Report){

 
$User = "diskspace@"
$File = (Get-Content C:\Temp\pw.txt | ConvertTo-SecureString)

$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, $File

		$To = $Email
		$from = "alert@"
		$EmailSubject = "Server $s storage space has dropped to less than 10 % on $View"
		$smtp = "auth.smtp.1and1.co.uk"

		$DefaultMessage="
			<p>Dear Help,</p>
			<p>There is a hard drive space issue on $Computer IPv4: $IPAddy </p>
			<p>This message only fires off if one or more disks have less than 10% free space left on both $View.</p>
            <p> Current FreeSpace $Freespace</p>
			<p>The Robot Checker .<br><br>
			</p>"

		$MailMessage = @{
				To = $To
				From = $from
				# BCC = $Bcc
				Subject = $EmailSubject
				Body = $DefaultMessage
				priority = "High"
				Smtpserver = $smtp
                Credential = $MyCredential
				ErrorAction = "SilentlyContinue" 
			}
			
		Send-MailMessage @MailMessage -bodyashtml

	}

When it I run the following script, works perfectly but the email appears as such -

Is there an command or where to make it easier to read and in a better format ?

Many thanks!

3 Spice ups

$Freespace is a complex value not a simple int or string/.

Tryu looking at $Freespae to see what you need to pull out of each CSV entry

You exported to CSV which is why it looks that way. What is wrong with the table output fro Get-WmiObject?

I could simplify it by doing this -

$Space =  Get-WmiObject win32_logicaldisk -ComputerName $s -Filter "Drivetype=3"  | Select-Object -Property DeviceID,VolumeName,@{Label="TotalSize";Expression={$_.Size / 1gb -as [int] }},@{Label="FreeSize";Expression={$_.freespace / 1gb -as [int] }}

$Device = $Space.DeviceID  
$Size = $Space.TotalSize 
$FreeSize = $Space.FreeSize

I’ve made the variable less complex, just now need to insert into the email to make it more readable.

You can also get creative with HTML formatting to make it even more readable.

Redid the script with these suggestions and ideas -

$a = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color:black;}
Table{background-color:#DFFFFF;border-collapse: collapse;}
TH{border-width:1px;padding:10px;border-style:solid;border-color:black;}
TD{border-width:1px;padding-left:10px;border-style:solid;border-color:black;}
TD{border-width:1px;padding-right:10px;border-style:solid;border-color:black;}
</style>
"@
$PC = $env:COMPUTERNAME
function Result {
    $PCs = $env:COMPUTERNAME
    $drives = Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -ComputerName $PC | 
    Select SystemName,DeviceID,@{Name="Size(GB)";Expression={"{0:N0} GB" -f ($_.size / 1GB) } },@{Name="FreeSpace(GB)";Expression={ "{0:N0} GB" -f ($_.freespace / 1GB) } } 
foreach ($drive in $drives)
{
[PSCustomObject]@{ 
    "PC Name" = $os.csname 
    "OS Version" = $os.Caption 
    "Device ID" = $drive.DeviceID
    "Size(GB)" = $drive."Size(GB)"
    "FreeSpace(GB)" = $drive."FreeSpace(GB)"

            }
        }
    } 

Result | ConvertTo-HTML -head $a | Out-File C:\temp\Inventory.htm

If (Result){

    $User = "diskspace@"
    $File = (Get-Content C:\Temp\pw.txt | ConvertTo-SecureString)
    $MyCredential = New-Object -TypeName System.Management.Automation.PSCredential `
    -ArgumentList $User, $File
    
            $To = "diskspace@"
            $from = "alert@"
            $EmailSubject = "Storage is low on $PC"
            $smtp = "auth.smtp.1and1.co.uk"
            $body = get-content C:\temp\Inventory.htm
            $DefaultMessage="
                <p>Dear Help,</p>
                <p>There is a hard drive space issue on $PC IPv4: $IPAddy </p>
                <p> $body </p>
                <p>The Robot Checker .<br><br>
                </p>"
    
            $MailMessage = @{
                    To = $To
                    From = $from
                    # BCC = $Bcc
                    Subject = $EmailSubject
                    Body = $DefaultMessage
                    priority = "High"
                    Smtpserver = $smtp
                    Credential = $MyCredential
                    ErrorAction = "SilentlyContinue" 
                }
                
            Send-MailMessage @MailMessage -bodyashtml
    
        }