Cannot find an overload

I am trying to figure out why I am getting this error:
New-Object : Cannot find an overload for “MailMessage” and the argument count: “4”.
5th line from the end…

`
$smtpServer = “smptservername"
$smtpFrom = “test@test.org”
$smtpTo = “me@test.org”
$DiskResults = @()

destination hosts to check

[Array] $machines = “servername", “localhost”

foreach ($computername in $machines)
{
$emailflag = 0;
Write-Host $computerName
$objDisks = Get-WmiObject -Computername $computerName -Class win32_logicaldisk | Where-Object { $_.DriveType -eq 3 }
ForEach( $disk in $objDisks )
{
$ThisVolume = "" | select ServerName,Volume,Capacity,FreeSpace,PercentFree
$ThisVolume.ServerName = $computerName
$ThisVolume.Volume = $disk.DeviceID
$ThisVolume.Capacity = $([Math]::Round($disk.Size/1073741824,2))
$ThisVolume.FreeSpace = $([Math]::Round($disk.FreeSpace/1073741824,2))
$ThisVolume.PercentFree = $ThisVolume.FreeSpace/$ThisVolume.Capacity*100
if ($ThisVolume.PercentFree -gt 10.0)
{
$emailflag = 1
}
$DiskResults += $ThisVolume
}
if ($emailflag -eq 1)
{
$subject = “Attention: Disk space is running low on ” + $computerName + “”;
$body = $ThisVolume

    # Send e-mail

    $smtpClient = New-Object Net.Mail.SmtpClient($smtpServer);
    $emailFrom  = New-Object Net.Mail.MailAddress $smtpFrom, $smtpFrom;
    $emailTo    = New-Object Net.Mail.MailAddress $smtpTo, $smtpTo;
    $mailMsg    = New-Object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $body);
    $smtpClient.Send($mailMsg)

}
}
$DiskResults | ft
`


Thanks for your help!

The 4-argument constructor for MailMessage expects 4 Strings. PowerShell is usually pretty good about converting data for you, but maybe it’s having trouble here. Try this change:

$body = $ThisVolume | Out-String

I agree with David. For some reason, the constructor just does not like the way the object built. Using his approach above will work to resolve that error you are getting.

Just out of curiosity, why aren’t you using the Send-MailMessage cmdlet instead of building out your own email command?
Using splatting and Send-MailMessage would simplify your code a little more.
Side note: Consider the use of the -Filter parameter with your WMI query as well.

`
$mailParams = @{
    To = “me@test.org”
    From = “test@test.org”
    SMTPServer = $smtpServer
}
$DiskResults = @()
# destination hosts to check
[Array] $machines = “servername", “localhost”
foreach ($computername in $machines)
{
 $emailflag = 0;
 Write-Host $computerName
  $objDisks = Get-WmiObject -Computername $computerName -Class win32_logicaldisk -Filter "DriveType = '3'"
  ForEach( $disk in $objDisks )
  {
    $ThisVolume = "" | select ServerName,Volume,Capacity,FreeSpace,PercentFree
    $ThisVolume.ServerName = $computerName
    $ThisVolume.Volume = $disk.DeviceID
    $ThisVolume.Capacity = $([Math]::Round($disk.Size/1073741824,2))
    $ThisVolume.FreeSpace = $([Math]::Round($disk.FreeSpace/1073741824,2))
    $ThisVolume.PercentFree = $ThisVolume.FreeSpace/$ThisVolume.Capacity*100
    if ($ThisVolume.PercentFree -gt 10.0)
    {
        $emailflag = 1
    }
    $DiskResults += $ThisVolume
  }
  if ($emailflag -eq 1)
  {
        $body = $ThisVolume
        $emailParams.Subject = “Attention: Disk space is running low on $computerName"
        $emailParams.Body = ($ThisVolume | Out-String)
        Send-MailMessage @emailParams
  }
}
`

Definitely use a filter against remote machines - it reduces the amount of data returned from the remote server

Also use 1Gb instead of 1073741824

Hello All,

Hope everyone is doing well.

I’m getting the following error message while executing the powershell script …Requesting you please help me to resolve this issue.
Script here is :

Function writeServiceInfo
{
param($fileName,$machinename,$dbname,$tbname,$ixname,$dbfrg,$dbpc)
Add-Content $filename “”
Add-Content $filename “$machineName”
Add-Content $filename “$dbname”
Add-Content $filename “$tbname”
Add-Content $filename “$ixname”
$frg = [Math]::Round($dbfrg,2)
If ($frg -gt “30”)

Error message

Cannot find an overload for “Round” and the argument count: “2”.
Cannot find an overload for “Round” and the argument count: “2”.
At line:108 char:2

  • $frg = [Math]::Round($dbfrg,2)
  • CategoryInfo : NotSpecified: (:slight_smile: , ParentContainsErrorRecordException
  • FullyQualifiedErrorId : MethodCountCouldNotFindBest

Thanks in advance for your quick help!!

please do not recycle old posts.