0

In my PowerShell code I am sending an email out if an unexpected folder is found in a certain directory when a command is executed. This works already, but I need to know is how to queue that email if the laptop is not connected to a network. Once the laptop has network connectivity then send the email out.

if ($item.Attributes -eq "Directory")
 {
        if ($ip=get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1})
        {
        Send-MailMessage  -SmtpServer $SmtpServer -From "$Admin" -To "$Admin" -Subject "Unexpected folder in $path folder on ""$env:computername""" -Body "There is an unexpected folder in the $path Folder on ""$env:computername"". Check on this ASAP."
        continue
        }
                else
                {
                #Need to queue this message until we are connected to a network again
                Send-MailMessage  -SmtpServer $SmtpServer -From "$Admin" -To "$Admin" -Subject "Unexpected folder in $path folder on ""$env:computername""" -Body "There is an unexpected folder in the $path Folder on ""$env:computername"". Check on this ASAP."
                continue
                }
 }

Thank you in advance

mr_evans2u
  • 139
  • 1
  • 5
  • 15
  • A `while` loop perhaps that will check for connectivity or a `try/catch` to write the details to a file and when the script run again check for a file? – Matt Apr 10 '15 at 19:12
  • What OS are you running? – mjolinor Apr 10 '15 at 20:27
  • I am new to powershell but,if someone here can translate this vbscript to powershell it will be nicer for you ;) in the same principle but in vbscript ==> http://stackoverflow.com/questions/28865002/loop-a-function?answertab=active#tab-top – Hackoo Apr 10 '15 at 22:40
  • I can't really do a while or sleep option in that block since there are other functionality that is required to be done at that time, which is why I'm doing the continue so the other functionality can be completed. – mr_evans2u Apr 13 '15 at 16:19

2 Answers2

1

If you need a simple delay, use while loop

   ...
    else
    {
         while (!(Test-Connection -ComputerName $SmtpServer -Quiet)) 
         {
             Start-Sleep 30
         }
         Send-MailMessage  -SmtpServer $SmtpServer ...
         continue
    }
Anton Z
  • 270
  • 2
  • 6
1

I just learned now from a good helper thanks to him ;)

You can try like this :

while($true) {
  if(Test-Connection -ComputerName $smtpserver -Count 1 -Quiet)
  {
    Write 'The host responded' 
    Send-MailMessage  -SmtpServer $SmtpServer -From "$Admin" -To "$Admin" -Subject "Unexpected folder in $path folder on ""$env:computername""" -Body "There is an unexpected folder in the $path Folder on ""$env:computername"". Check on this ASAP."
  break
  }
}
Hackoo
  • 18,337
  • 3
  • 40
  • 70