Vote count:
0
I am running Windows Server 2012 used as an admin server. Here I have it setup to run PowerShell scrips as scheduled tasks to do various tasks on remote servers using Windows Remote Management aka Remote PowerShell.
The following script is one of 4 scripts, where underneath ############# everything stays exactly the same. Above ############# is where I set the different variables to run against different servers.
I have a problem where if I run one of the 4 scripts as a scheduled task, it persists in telling me the opposite result to what the result should be. All other scheduled task scripts run perfectly giving expected results. This script runs perfectly with expected results if I run it from PowerShell ISE an email to say the adfssrv service is running. However when this exact script is run using a scheduled task, I get an email to say the adfssrv service isn't running and it waits for 15 minutes before telling me again in an email that the adfssrv service is not running.
The Scheduled task is setup exactly the same as the others, powershell.exe -command c:\Scripts\XA-US-FS-02_adfssrv.ps1 with all scripts locacted in the exact same local directory c:\Scripts
$FirstCheck = @()
$arrService = @()
$computername = @()
$MyHost = @()
$cred = @()
$password = @()
$cmdAutoAdmin = @()
$WinRMPort = @()
$password = ConvertTo-SecureString "some_password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("xa-us-fs-02\svc.tasks", $password )
$computername = "xa-us-fs-02.cloudapp.net"
$cmdAutoAdmin = {Get-Service -Name adfssrv}
$WinRMPort = 61573
$MyHost = "XA-US-FS-02"
$mytime = Get-Date -displayhint time
$SMTPServer = "smtp.sendgrid.net"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("User", "SendGridPassword");
$EmailFrom = "Email_Alerts@xyz.com"
$EmailTo = "receiver@xyz.com"
#########################################
$arrService = Invoke-Command -ComputerName $computername -Port $WinRMPort -Credential $cred -UseSSL -ScriptBlock $cmdAutoAdmin
if ($arrService.Status -eq "Running"){
$Subject = "$MyHost - ADFS service"
$Body = "Hi, I just checked the ADFS service (adfssrv) on $MyHost at $mytime and it is running :)"
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
$FirstCheck = "Success"
}
elseif ($arrService.Status -ne "Running"){
Invoke-Command -ComputerName $computername -Port $WinRMPort -Credential $cred -UseSSL -ScriptBlock {Start-Service -Name adfssrv}
$Subject = "$MyHost - ADFS NOT RUNNING"
$Body = "This is an email to say that the ADFS service on $MyHost was checked at $mytime and it was not started. `
I have attempted to start the ADFS service just now. `
I will check again in 15 minutes"
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Start-Sleep -Seconds 900
}
if ($arrService.Status -ne "Running" -and $FirstCheck -ne "Success"){
$Subject = "$MyHost - ADFS NOT RUNNING AGAIN!"
$Body = "This is the SECOND email to say that the ADFS service on $MyHost was checked at $mytime and it was not started. `
Manual human intervention required"
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}
elseif ($arrService.Status -eq "Running" -and $FirstCheck -ne "Success"){
$Subject = "$MyHost - ADFS service"
$Body = "Hi, I just checked the ADFS service (adfssrv) for a SECOND time on $MyHost at $mytime and it is now running :)"
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}
Aucun commentaire:
Enregistrer un commentaire