I wrote a script to check the IIS service status every 15 minutes on a web server. I scheduled this script via Windows Task Scheduler.
When Im logged onto the server and manually run the script if functions coorectly. When I am not logged onto the server and execute this via at batch script, the Get-Service query does not return Running for IIS even though it is indeed up and restarts the service.
Why would this script run differently when Im physically logged onto the server vs running as a scheduled task behind the scenes?
The script is called like this: C:\Scripts\powershell.exe -File verify_status.ps1 [Service param]
############################## # Get service status ############################## if($args[0] -ne $NULL){ $serviceName = $args[0] $serverName = hostname $status = (Get-Service $serviceName).Status if ($status -ne "Running"){ sendMail "$serviceName" "$serverName" Restart-Service $ServiceName }else{ # Service is running, do nothing; } } =====================================
EDIT - The Get-Service returns a NULL when the script is run with no one logged on. If that helps at all.