Hi
I found this great script , and trying to pipe the results into the body of email.
However after spending many hours, i cannot figure it out!
here's the script
$Subnet = "192.168.0"
$MinAddress = 60
$MaxAddress = 62
# step through each address from MinAddress to MaxAddress
$emailtext=for($Address=$MinAddress;$Address -le $MaxAddress;$Address++){
# make a text string for the current IP address to be tested
$TestAddress = $Subnet+"."+[convert]::ToString($Address)
# do the ping(s), don't display red text if the ping fails (erroraction)
$Result = Test-Connection -ComputerName $TestAddress -Count 2 -ErrorAction SilentlyContinue
if($Result -eq $null){
Write-Host "No reply from $TestAddress" -NoNewline
# have to use try/catch as the GetHostByAddress errors in a nasty way if it doesn't find anything
try{
# do the reverse DNS query
$HostName = [System.Net.Dns]::GetHostByAddress($TestAddress).HostName
if($HostName -ne $null){
# found something in DNS, display it
Write-Host " ($HostName)"
}
} catch {
# didn't find anything (GetHostByAddress generated an error), just do a CRLF
write-host ""
}
}
}
Send-MailMessage -SmtpServer localhost -Subject "ping sweep" -From usrv01@domain.com -To john@domain.com -body ($emailtext | Out-String)
I get this error
Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again
.
At C:\test.ps1:28 char:111
+ Send-MailMessage -SmtpServer localhost -Subject "ping sweep" -From usrv01@domain.com -To john@domain.com -body <<<< ($emailtext | Out-String)
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage