2

I'm running this code below as a function to iterate through a list of servers to check if services are running. This works when run manually. When run from a scheduled task the code $arrService.Status translates to the service name. I even tried something like Get-Service -Computer $Server -display $ServiceName | Select -expandproperty Status but that returns empty string or no value.

Any idea why the status property would not work when run from a scheduled task?

function CheckService{
     param($Server, $ServiceName)   
     $arrService = Get-Service -Computer $Server -display $ServiceName
     if ($arrService.Status -ne "Running"){
         Start-Service -InputObject $ServerService
     }
}
pretzelb
  • 1,131
  • 2
  • 16
  • 38
  • 2
    I suspect you are running into a permission issue. Try spinning up a PowerShell session that runs as the same user as your scheduled task. Are you still able to check the status of your services? – djs Dec 15 '15 at 05:32
  • I can't log into the server with the account credentials I was using but I did enter my NT login credentials for the scheduled task and of course that did work. It seems odd that the account has permission to run the command but does not return the status field information. The next question is what permissions are needed to get the status so I can look into granting that for the account. This will require updates to a lot of servers. – pretzelb Dec 15 '15 at 19:18
  • Found a link on running powershell as different user [link]http://blogs.technet.com/b/benshy/archive/2012/06/04/using-a-powershell-script-to-run-as-a-different-user-amp-elevate-the-process.aspx[/link] – pretzelb Dec 15 '15 at 22:36

1 Answers1

0

Thanks to @djs I was able to find a solution, albeit not a fantastic solution. Adding the account to the admin group of each server allows the commands to execute. In our case this isn't a problem so we are going to go with this solution.

pretzelb
  • 1,131
  • 2
  • 16
  • 38