<# Scheduled Task metadata
General
    Get IPs for PA - Azure Backup
    run as ndgov\!itdvcenterscript
        run whether user is logged on or not
 Triggers
    Daily, 11:30am - repeat every 1 hour indefinitely
Actions
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "C:\itdscript\GetIPsMonitor.ps1"
Settings
    allow task to be run on demand
    stop the task if it runs longer than 1 hour -eq $true
    if the running task does not end when requested, force it to stop
#>


#cdfelchle 7/12/16

#Files to Monitor
$FileCheckWin = "c:\inetpub\wwwroot\win.txt"
$FileCheckLin = "c:\inetpub\wwwroot\lin.txt"
$FileCheckBak = "c:\inetpub\wwwroot\AzureBak.txt"
$FileCheckWinAz = "c:\inetpub\wwwroot\Azurewin.txt"
$FileCheckLinAz = "c:\inetpub\wwwroot\Azurelin.txt"

#E-mail Initialization and Construction
$emailSubject = "VMware-PA Integration Failure"
$emailRelay = "apprelay2.nd.gov"
$emailBody = 
"Scripting Server Error Report (ITDVMVCP1SCRIPT)
------------------------------------------
"
$emailRecipients = "itdvctremailalerts@nd.gov"
$emailSender = "itdvmvcp1script@nd.gov"

#File Presence Check
$WindowsPresent = Test-Path $FileCheckWin
$LinuxPresent = Test-Path $FileCheckLin
$AzureWindowsPresent = Test-Path $FileCheckWinAz
$AzureLinuxPresent = Test-Path $FileCheckLinAz
$AzureBakPresent = Test-Path $FileCheckBak


#Flag to send e-mail
$SendEmail = $False


#File Presence Check
If ($WindowsPresent -eq $False){
    $SendEmail = $True
    $emailBody += $FileCheckWin + " --- Missing File`n"
    }

If ($LinuxPresent -eq $False){
    $SendEmail = $True
    $emailBody += $FileCheckLin + " --- Missing File`n"
    }

If ($AzureWindowsPresent -eq $False){
    $SendEmail = $True
    $emailBody += $FileCheckWinAz + " --- Missing File`n"
    }

If ($AzureLinuxPresent -eq $False){
    $SendEmail = $True
    $emailBody += $FileCheckLinAz + " --- Missing File`n"
    }

If ($AzureBakPresent -eq $False){
    $SendEmail = $True
    $emailBody += $FileCheckBak + " --- Missing File`n"
    }


#File Size Check
If ($WindowsPresent -eq $True -and (Get-Item $FileCheckWin).length -lt 1000){
    $SendEmail = $True
    $emailBody += "$FileCheckWin --- File size is less than 1kb`n"
    }

If ($LinuxPresent -eq $True -and (Get-Item $FileCheckLin).length -lt 1000){
    $SendEmail = $True
    $emailBody += "$FileCheckLin --- File size is less than 1kb`n"
    }

If ($AzureWindowsPresent -eq $True -and (Get-Item $FileCheckWinAz).length -lt 100){
    $SendEmail = $True
    $emailBody += "$FileCheckWinAz --- File size is less than 1kb`n"
    }

If ($AzureLinuxPresent -eq $True -and (Get-Item $FileCheckLinAz).length -lt 100){
    $SendEmail = $True
    $emailBody += "$FileCheckLinAz --- File size is less than 1kb`n"
    }

If ($AzureBakPresent -eq $True -and (Get-Item $FileCheckBak).length -lt 10){
    $SendEmail = $True
    $emailBody += "$FileCheckBak --- File size is less than 10 bytes`n"
    }



#File Age Check
$CurrentTime = Get-Date

If ($WindowsPresent -eq $True -and (Get-Item $FileCheckWin).LastWriteTime -lt $CurrentTime.AddHours(-13)){
    $SendEmail = $True
    $emailBody += "$FileCheckWin --- File is older than 12 hours`n"
    }

If ($LinuxPresent -eq $True -and (Get-Item $FileCheckLin).LastWriteTime -lt $CurrentTime.AddHours(-13)){
    $SendEmail = $True
    $emailBody += "$FileCheckLin --- File is older than 12 hours`n"
    }

If ($AzureWindowsPresent -eq $True -and (Get-Item $FileCheckWinAz).LastWriteTime -lt $CurrentTime.AddHours(-13)){
    $SendEmail = $True
    $emailBody += "$FileCheckWinAz --- File is older than 12 hours`n"
    }

If ($AzureLinuxPresent -eq $True -and (Get-Item $FileCheckLinAz).LastWriteTime -lt $CurrentTime.AddHours(-13)){
    $SendEmail = $True
    $emailBody += "$FileCheckLinAz --- File is older than 12 hours`n"
    }

If ($AzureBakPresent -eq $True -and (Get-Item $FileCheckBak).LastWriteTime -lt $CurrentTime.AddHours(-13)){
    $SendEmail = $True
    $emailBody += "$FileCheckBak --- File is older than 12 hours`n"
    }


If ($SendEmail -eq $True){
    Send-MailMessage -To $emailRecipients -From $emailSender -Subject $emailSubject -Body $emailBody -SmtpServer $emailRelay
    }