1

I am trying to write a script that will monitor a log file for time modified. If the file has been monitored on the last minute send me an email saying it is all good. IF the log file is older then a minute and the time is between 6am and 9pm send me an email. IF between 9pm and 6am reboot the server. except it is rebooting the server no matter what

    # Set perameters here
    $uncPath   = 'Unc path to log file' 
    $Server = 'ServerName'
    $min = Get-Date 06:00
    $max = Get-Date 21:00
    $now = Get-Date -Format hh:mm:ss
    
    
    # Look at the Printstream Log Files and check last modified time
    
    $files = (Get-ChildItem -Path $uncPath -Filter '*PrintstreamBroker*' -File | 
              Where-Object { $_.LastWriteTime -Gt (Get-Date).AddMinutes(-1) }).FullName
    
    #If there file is good do nothing, and send email 
    
    if ($files) {
        $Subject = 'Printstream Broker Is Running'
        $message = 'The Printstream Broker Is Running There Is Nothing To Do'
        $emailTo = '[email protected]'
    }
    
    
    
    Else ($min.TimeOfDay -lt $now -and $max.TimeOfDay -gt $now){ 
                    { 
                     $Subject = 'Printstream Broker Is Not Running'
                     $message = 'Printstream Broker Is Not Running Please Log Into $servername and start the Printstream Monarch Broker'
                     $emailTo = '[email protected]'
                    }
            }

            Else {
 
            Restart-Computer –ComputerName $server -Force

            $Subject = 'Monarch Server Rebooted'
            $message = 'The Monarch Server Has Been Rebooted'
            $emailTo = '[email protected]'
        }
      }  
   

# create a email 
$mailParams = @{
    From       = '[email protected]'
    To         = $emailTo
    Subject    = $Subject
    Body       = $message
    SmtpServer = 'smtpserver'
    # any other parameters you might want to use
}
# send the email
Send-MailMessage @mailParams
2
  • What exactly is the issue? Commented Sep 21, 2020 at 20:45
  • 1
    No matter what if the file is old it will reboot the server. It should only reboot the server if it is between 9pm and 6 am. If during 6am and 9pm it should send an emal Commented Sep 21, 2020 at 21:14

1 Answer 1

1

You have if else else.

It should be if elseif else.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.