1

I am looping through a list of computers and checking disc space. I want to format this information into an array that I can add as the email body. I want the entire output into one email, I know how to send an individual email of each one.

Here is the code:

$ComputerList = IMPORT-CSV listofcomputers.txt
$tableFragment = ForEach ($Computer in $ComputerList){
$Name = $Computer.Name
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $Name -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$b = ($disk.Size / 1073741824)
$a = ($disk.FreeSpace / 1073741824)
$a = "{0:N1}" -f $a
$b = "{0:N1}" -f $b
if($disk.FreeSpace -lt 10737418240){Write-Host Disk space on $Name is LOW.  Only $a GB remaining. -foregroundcolor "magenta"}
else{Write-Host Free disc space on $Name is $a GB, out of $b GB.}
}

1 Answer 1

2

Change this bit:

if($disk.FreeSpace -lt 10737418240){Write-Host Disk space on $Name is LOW.  Only $a GB remaining. -foregroundcolor "magenta"}
else{Write-Host Free disc space on $Name is $a GB, out of $b GB.}

to this:

if($disk.FreeSpace -lt 10737418240){ $body += "Disk space on $Name is LOW.  Only $a GB remaining.`r`n"}
else{ $body += "Free disc space on $Name is $a GB, out of $b GB.`r`n"}

You don't need an array of strings since the Send-MailMessage command's Body parameter expects a string.

If you want some header info, then at the top of your script declare $body:

$body = "Some header info.`r`n"
Sign up to request clarification or add additional context in comments.

3 Comments

This works, but the rn do not seem to have any effect. The email body is one long continuous string. Do I have to format via HTML to get the line breaks? @Keith Hill
Hmm, I just did a test using Send-MailMessage using "{backtick}r{backtick}n" and it came through on different lines. You shouldn't have to format as HTML.
I was using -BodyAsHTML. When I removed that it worked great. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.