Network Zero
asked on
powershell wont send email + I want to add a screenshot every 30 seconds saved to a folder
I'm trying to get this code to send out an email and also take a snap shoot - I haven't figured out the screenshot part the code works but wont send an email - I tried fixing the smtp but I still get nothing.
I also wanted to see if I can add a screenshot every 30 seconds to the code.
powershell.exe -noexit -command ". C:\Users\example\Documents \Online.ps 1; start-monitor PC,Server1"
function Start-Monitor {
#Requires -Version 2.0
[CmdletBinding()]
Param
(
[Parameter(Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropert yName=$tru e)]
[String[]]$ComputerName = $env:COMPUTERNAME,
# Switch to Enable Email Notifications on First Down
[Switch]$notifyonServerDow n,
# Switch to Enable Email Notifications on Server Online
[Switch]$notifyonServerBac kOnline,
# Switch to Enable Email Notifications on MaxOutageCount
[Switch]$notifyonMaxOutage Count,
# Switch to Enable all notifications
[Switch]$notifyAll,
# specify the time you want email notifications resent for hosts that are down
$EmailTimeOut = 30,
# specify the time you want to cycle through your host lists.
$SleepTimeOut = 30,
# specify the maximum hosts that can be down before the script is aborted
$MaxOutageCount = 100,
# specify who gets notified
$tonotification = @("email@gmail.com"),
# specify where the notifications come from
$fromnotification = ("email@gmail.com"),
# specify the SMTP server
$smtpserver = "smtp-relay.gmail.com",
# reset the lists of hosts prior to looping
$OutageHosts = @()
)#End Param
# Use end block, to ensure all computers are read in at once, even by pipeline
end {
if ($notifyAll)
{
$notifyonMaxOutageCount,$n otifyonSer verBackOnl ine,$notif yonServerD own = $True,$True,$True
}
Write-Verbose -Message "computername: $computername"
Write-Verbose -Message "notifyonMaxOutageCount: $notifyonMaxOutageCount"
Write-Verbose -Message "notifyonServerBackOnline: $notifyonServerBackOnline"
Write-Verbose -Message "notifyonServerDown: $notifyonServerDown"
# Allow
if ( $Input )
{
Write-Verbose -Message "Input: $Input"
$ComputerName = $Input
}
# start looping here
Do{
$available = @()
$notavailable = @()
Write-Host (Get-Date)
# Read the File with the Hosts every cycle, this way to can add/remove hosts
# from the list without touching the script/scheduled task,
# also hash/comment (#) out any hosts that are going for maintenance or are down.
$ComputerName | Where-Object {!($_ -match "#")} |
#"test1","test2" | Where-Object {!($_ -match "#")} |
ForEach-Object {
if(Test-Connection -ComputerName $_ -Count 1 -ea silentlycontinue)
{
# if the Host is available then write it to the screen
write-host "Available host ---> "$_ -BackgroundColor Green -ForegroundColor White
[String[]]$available += $_
# if the Host was out and is now backonline, remove it from the OutageHosts list
if ($OutageHosts -ne $Null)
{
if ($OutageHosts.ContainsKey( $_))
{
$OutageHosts.Remove($_)
$Now = Get-date
if ($notifyonServerBackOnline )
{
$Body = "$_ is back online at $Now"
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification `
-Subject "Host $_ is up" -SmtpServer $smtpserver
}
}
}
}
else
{
# If the host is unavailable, give a warning to screen
write-host "Unavailable host ------------> "$_ -BackgroundColor Magenta -ForegroundColor White
if(!(Test-Connection -ComputerName $_ -Count 2 -ea silentlycontinue))
{
# If the host is still unavailable for 4 full pings, write error and send email
write-host "Unavailable host ------------> "$_ -BackgroundColor Magenta -ForegroundColor White
[Array]$notavailable += $_
if ($OutageHosts -ne $Null)
{
if (!$OutageHosts.ContainsKey ($_))
{
# First time down add to the list and send email
Write-Host "$_ Is not in the OutageHosts list, first time down"
$OutageHosts.Add($_,(get-d ate))
$Now = Get-date
if ($notifyonServerDown)
{
$Body = "$_ has not responded for 5 pings at $Now"
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification `
-Subject "Host $_ is down" -SmtpServer $smtpserver
}
}
else
{
# If the host is in the list do nothing for 1 hour and then remove from the list.
Write-Host "$_ Is in the OutageHosts list"
if (((Get-Date) - $OutageHosts.Item($_)).Tot alMinutes -gt $EmailTimeOut)
{$OutageHosts.Remove($_)}
}
}
else
{
# First time down create the list and send email
Write-Host "Adding $_ to OutageHosts."
$OutageHosts = @{$_=(get-date)}
$Now = Get-date
if ($notifyonServerDown)
{
$Body = "$_ has not responded for 5 pings at $Now"
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification `
-Subject "Host $_ is down" -SmtpServer $smtpserver
}
}
}
}
}
# Report to screen the details
Write-Host "Available count:"$available.count
Write-Host "Not available count:"$notavailable.count
if ($OutageHosts)
{
Write-Host "Not available hosts:"
$OutageHosts
}
Write-Host ""
Write-Host "Sleeping $SleepTimeOut seconds"
Start-Sleep -Seconds $SleepTimeOut
if ($OutageHosts.Count -gt $MaxOutageCount)
{
# If there are more than a certain number of host down in an hour abort the script.
$Exit = $True
$body = $OutageHosts | Out-String
if ($notifyonMaxOutageCount)
{
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification `
-Subject "More than $MaxOutageCount Hosts down, monitoring aborted" -SmtpServer $smtpServer
}
}
}
while ($Exit -ne $True)
}#End
}#Start-Monitor
I also wanted to see if I can add a screenshot every 30 seconds to the code.
powershell.exe -noexit -command ". C:\Users\example\Documents
function Start-Monitor {
#Requires -Version 2.0
[CmdletBinding()]
Param
(
[Parameter(Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropert
[String[]]$ComputerName = $env:COMPUTERNAME,
# Switch to Enable Email Notifications on First Down
[Switch]$notifyonServerDow
# Switch to Enable Email Notifications on Server Online
[Switch]$notifyonServerBac
# Switch to Enable Email Notifications on MaxOutageCount
[Switch]$notifyonMaxOutage
# Switch to Enable all notifications
[Switch]$notifyAll,
# specify the time you want email notifications resent for hosts that are down
$EmailTimeOut = 30,
# specify the time you want to cycle through your host lists.
$SleepTimeOut = 30,
# specify the maximum hosts that can be down before the script is aborted
$MaxOutageCount = 100,
# specify who gets notified
$tonotification = @("email@gmail.com"),
# specify where the notifications come from
$fromnotification = ("email@gmail.com"),
# specify the SMTP server
$smtpserver = "smtp-relay.gmail.com",
# reset the lists of hosts prior to looping
$OutageHosts = @()
)#End Param
# Use end block, to ensure all computers are read in at once, even by pipeline
end {
if ($notifyAll)
{
$notifyonMaxOutageCount,$n
}
Write-Verbose -Message "computername: $computername"
Write-Verbose -Message "notifyonMaxOutageCount: $notifyonMaxOutageCount"
Write-Verbose -Message "notifyonServerBackOnline:
Write-Verbose -Message "notifyonServerDown: $notifyonServerDown"
# Allow
if ( $Input )
{
Write-Verbose -Message "Input: $Input"
$ComputerName = $Input
}
# start looping here
Do{
$available = @()
$notavailable = @()
Write-Host (Get-Date)
# Read the File with the Hosts every cycle, this way to can add/remove hosts
# from the list without touching the script/scheduled task,
# also hash/comment (#) out any hosts that are going for maintenance or are down.
$ComputerName | Where-Object {!($_ -match "#")} |
#"test1","test2" | Where-Object {!($_ -match "#")} |
ForEach-Object {
if(Test-Connection -ComputerName $_ -Count 1 -ea silentlycontinue)
{
# if the Host is available then write it to the screen
write-host "Available host ---> "$_ -BackgroundColor Green -ForegroundColor White
[String[]]$available += $_
# if the Host was out and is now backonline, remove it from the OutageHosts list
if ($OutageHosts -ne $Null)
{
if ($OutageHosts.ContainsKey(
{
$OutageHosts.Remove($_)
$Now = Get-date
if ($notifyonServerBackOnline
{
$Body = "$_ is back online at $Now"
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification `
-Subject "Host $_ is up" -SmtpServer $smtpserver
}
}
}
}
else
{
# If the host is unavailable, give a warning to screen
write-host "Unavailable host ------------> "$_ -BackgroundColor Magenta -ForegroundColor White
if(!(Test-Connection -ComputerName $_ -Count 2 -ea silentlycontinue))
{
# If the host is still unavailable for 4 full pings, write error and send email
write-host "Unavailable host ------------> "$_ -BackgroundColor Magenta -ForegroundColor White
[Array]$notavailable += $_
if ($OutageHosts -ne $Null)
{
if (!$OutageHosts.ContainsKey
{
# First time down add to the list and send email
Write-Host "$_ Is not in the OutageHosts list, first time down"
$OutageHosts.Add($_,(get-d
$Now = Get-date
if ($notifyonServerDown)
{
$Body = "$_ has not responded for 5 pings at $Now"
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification `
-Subject "Host $_ is down" -SmtpServer $smtpserver
}
}
else
{
# If the host is in the list do nothing for 1 hour and then remove from the list.
Write-Host "$_ Is in the OutageHosts list"
if (((Get-Date) - $OutageHosts.Item($_)).Tot
{$OutageHosts.Remove($_)}
}
}
else
{
# First time down create the list and send email
Write-Host "Adding $_ to OutageHosts."
$OutageHosts = @{$_=(get-date)}
$Now = Get-date
if ($notifyonServerDown)
{
$Body = "$_ has not responded for 5 pings at $Now"
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification `
-Subject "Host $_ is down" -SmtpServer $smtpserver
}
}
}
}
}
# Report to screen the details
Write-Host "Available count:"$available.count
Write-Host "Not available count:"$notavailable.count
if ($OutageHosts)
{
Write-Host "Not available hosts:"
$OutageHosts
}
Write-Host ""
Write-Host "Sleeping $SleepTimeOut seconds"
Start-Sleep -Seconds $SleepTimeOut
if ($OutageHosts.Count -gt $MaxOutageCount)
{
# If there are more than a certain number of host down in an hour abort the script.
$Exit = $True
$body = $OutageHosts | Out-String
if ($notifyonMaxOutageCount)
{
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification `
-Subject "More than $MaxOutageCount Hosts down, monitoring aborted" -SmtpServer $smtpServer
}
}
}
while ($Exit -ne $True)
}#End
}#Start-Monitor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Below I have this code that works, how do I integrate this code to my current code and take a screenshot.
I put the output of the code below it gave me an error
########################## ########## ########## ########## ########## ########## ##$From = "YourEmail@gmail.com"$To = "email@gmail.com"$Cc = "email@gmail.com"$Attachme nt = "C:\1.txt"$Subject = "test"$Body = "This is a test"$SMTPServer = "smtp.gmail.com"$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment
########################## ########## ########## ########## ########## ########## ##
The error I get is below:
Send-MailMessage : Unable to connect to the remote server
At line:1 char:1
+ Send-MailMessage -from "email@gmail.com" -to "email@gmail.com" -s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClien t:SmtpClie nt) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.Po werShell.C ommands.Se ndMailMess age
I put the output of the code below it gave me an error
##########################
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment
##########################
The error I get is below:
Send-MailMessage : Unable to connect to the remote server
At line:1 char:1
+ Send-MailMessage -from "email@gmail.com" -to "email@gmail.com" -s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClien
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.Po
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The problem is this code was written with powershell 2.0 and it does not take in consideration the port also Gmail requires authentication - if I was doing this from domain I wouldn't have any issues.
# specify who gets notified
$tonotification = @("email@gmail.com"),
# specify where the notifications come from
$fromnotification = ("email@gmail.com"),
# specify the SMTP server
$smtpserver = "smtp-relay.gmail.com",
# reset the lists of hosts prior to looping
$OutageHosts = @()
)#End Param
- This is the part with the issue.
# specify who gets notified
$tonotification = @("email@gmail.com"),
# specify where the notifications come from
$fromnotification = ("email@gmail.com"),
# specify the SMTP server
$smtpserver = "smtp-relay.gmail.com",
# reset the lists of hosts prior to looping
$OutageHosts = @()
)#End Param
- This is the part with the issue.
Can you look into the SMTP logs and see if there is any helpful info?
Dan