Powershell will not allow a str var in the email.body

$TS = Get-Date -Format “hh:mm”

Write-Host “($TS)—Email Completion Message—” -foregroundcolor “White”

#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)

####You Can Add An Attachment Pathway In The Next Line
#$Attachment = “C:\Users\Healthcare.ROC.Repor\DXC Production\Reporting Site - Documents\AWSConnect\qryfltConnect_LastDay.xlsx”

#add properties as desired
$Mail.To = “J.P@GainwellTechnologies.com
$Mail.Subject = “DB Server Disk Health, Size”

####Add Attachment With Next Line
#$Mail.Attachments.Add($Attachment)
$results2 = $results + $Threshold
$Mail.Body = $results2 # <------- error is here, why does this method not allow a str var ???
#send message
$Mail.Send()
#quit and cleanup
#$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null

$TS = Get-Date -Format “hh:mm”

Write-Host “($TS)—Emailing Completion Message Complete—” -foregroundcolor “White”

OUTPUT :The object does not support this method.
At C:\ROCTeam\PowerShell\DailyDiskThresholdAlert\DiskThreshold.ps1:48 char:1

  • $Mail.Body = $results2
  •   + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Please do not post images of code or error messages as it is almost unreadable. Instead post it as text formatted as code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

You can go back, edit your existing post and fix it - you don’t have to create a new post. :point_up_2:t4:

Why not use PowerShell native e-mail support?

Everyone here gives up their free time to help answer queries. Please help us by taking a few minutes to format your posts properly.
Thanks in advance.

What is $results and what is $Threshold?

they are just strings from 2 different outputs

I believe I’m doing that although going through the outlook app

That’s at very least an unreliable advisor. :wink: No, you don’t.

How do you populate them?

they are populated by hostlocal, get-volumn, etc. The problem is this method only works by passing a literal string, it seems. How do you pass variables to it. Theres no explanation in the error.

I don’t know what hostlocal is and Get-Volume does not return strings by default. So again how do you populate them? I’d prefer to actually see the code you’re using instead of you describing it. :wink:

And please, when you post code format it as code.

$name = hostname
write output " the DB Server"
$results=Get-Volume | ft
write output @results
#JPutz assuming 60% is needed from the statement that any sql server database < 500 gigs needs 50 gigs to run properly
$Threshold =“60%”
$Computername = $env:COMPUTERNAME
$Free = Get-WmiObject Win32_LogicalDisk -ComputerName $Computername |
Where-Object { $.DriveType -eq “3” } | Select-Object SystemName,VolumeName,DeviceID,
@{ Name = “Size (GB)” ; Expression = { “{0:N1}” -f( $
.Size / 1gb) } },
@{ Name = “Free Space (GB)” ; Expression = {"{0:N1}" -f( $.Freespace / 1gb ) } },
@{ Name = “Percent” ; Expression = { “{0:P0}” -f( $
.FreeSpace / $_.Size ) } }

           if ($threshold -ge $_.Percent) {
          
          write-host  "Above Threshold" -ForegroundColor Green > $Threshold
         <# }
          Elseif($Threshold -lt $_.PercentFree)

          {Write-host "Same" -ForegroundColor Yellow#>

           }else{
          Write-Host "Lower Than Threshold" -ForegroundColor Red  > $Threshold
           }

#########################################################################Completion Email

$TS = Get-Date -Format “hh:mm”

Write-Host “($TS)—Email Completion Message—” -foregroundcolor “White”

#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)

####You Can Add An Attachment Pathway In The Next Line
#$Attachment = “C:\Users\Healthcare.ROC.Repor\DXC Production\Reporting Site - Documents\AWSConnect\qryfltConnect_LastDay.xlsx”

#add properties as desired
$Mail.To = “Joseph.Putz@GainwellTechnologies.com
$Mail.Subject = “DB Server Disk Health, Size”

####Add Attachment With Next Line
#$Mail.Attachments.Add($Attachment)
$results2 = $results + $Threshold
$Mail.Body = $results2
#send message
$Mail.Send()
#quit and cleanup
#$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null

$TS = Get-Date -Format “hh:mm”

Write-Host “($TS)—Emailing Completion Message Complete—” -foregroundcolor “White”

I am no powershell user, but my department uses it. I really don’t see why powershell is so good. You can’t debug. The point here is, WHY IS THIS SO DIFFICULT ?

I will ask this only once more: Please format your code as code. The description how to do is in my first answer.

there is nothing under Codeformatting_1 or Codeformatting_101. send a link

You like to ignore the obvious, don’t you? :wink: Just feel welcomed and click on the link “Welcome to the new PowerShell Forums”. :point_up_2:t4:

So the non-answer, is that you need to use the powershell library to pass this variable. The above code is the only thing running for our department for email output.

No, the non-answer is that you keep making it hard for the people willing to help you by stubbornly refusing to format your code as code. That’s why I don’t have any desire at all to help you any further.

1 Like