Matt Galloy
Matt Galloy

Reputation: 13

Writing a powershell script to monitor a log file

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

Upvotes: 1

Views: 613

Answers (1)

OwlsSleeping
OwlsSleeping

Reputation: 1570

You have if else else.

It should be if elseif else.

Upvotes: 1

Related Questions