w2k8 r2 scheduled task posh script where start-service stop-service failing

Hi folks

First of all apologies - I am 100% sure this is me being dumb however, I have spent the last few hours rtfm and still no joy and thought it would make sense to post here -

Thank you in advance - cheers Nick

ok, so I need to schedule service restart on 2k8r2 server using PoSH script.
so - keep it simple and do something basic i.e. use spooler

test.ps1
start-transcript C:\Software_depot\Scripts\Logs\service.txt
get-service spooler
get-date
stop-service spooler
get-date
start-service spooler
get-date
stop-transcript

task is scheduled -

run whether user is logged in or not and ‘run with highest priv’

start program = C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
arguments = -noprofile -noninteractive -nologo C:\Software_depot\Scripts\test.ps1

domain account, with local admin rights and ‘Log on as batch’ set

so that’s the setup
test 1 - on execution of script manually with PoSH shell running as admin - all works services get, stop and start all work

test 2 - run scheduled task
get-service works
start / stop service - fails

here is the transcript


Windows PowerShell Transcript Start
Start time: 20200817085425
Username : blah
Machine : (Microsoft Windows NT 6.1.7601 Service Pack 1)


Transcript started, output file is C:\Software_depot\Scripts\Logs\service.txt

Status Name DisplayName


Running spooler Print Spooler

DisplayHint : DateTime
Date : 8/17/2020 12:00:00 AM
Day : 17
DayOfWeek : Monday
DayOfYear : 230
Hour : 8
Kind : Local
Millisecond : 165
Minute : 54
Month : 8
Second : 25
Ticks : 637332512651659007
TimeOfDay : 08:54:25.1659007
Year : 2020
DateTime : Monday, August 17, 2020 8:54:25 AM

Stop-Service : Service ‘Print Spooler (spooler)’ cannot be stopped due to the f
ollowing error: Cannot open spooler service on computer ‘.’.
At C:\Software_depot\Scripts\test.ps1:5 char:13

  • stop-service <<<< spooler
  • CategoryInfo : CloseError: (System.ServiceProcess.ServiceContro
    ller:ServiceController) [Stop-Service], ServiceCommandException
  • FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Command
    s.StopServiceCommand

DisplayHint : DateTime
Date : 8/17/2020 12:00:00 AM
Day : 17
DayOfWeek : Monday
DayOfYear : 230
Hour : 8
Kind : Local
Millisecond : 259
Minute : 54
Month : 8
Second : 25
Ticks : 637332512652596603
TimeOfDay : 08:54:25.2596603
Year : 2020
DateTime : Monday, August 17, 2020 8:54:25 AM

Start-Service : Service ‘Print Spooler (spooler)’ cannot be started due to the
following error: Cannot open spooler service on computer ‘.’.
At C:\Software_depot\Scripts\test.ps1:7 char:14

  • start-service <<<< spooler
  • CategoryInfo : OpenError: (System.ServiceProcess.ServiceControl
    ler:ServiceController) [Start-Service], ServiceCommandException
  • FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Comman
    ds.StartServiceCommand

DisplayHint : DateTime
Date : 8/17/2020 12:00:00 AM
Day : 17
DayOfWeek : Monday
DayOfYear : 230
Hour : 8
Kind : Local
Millisecond : 306
Minute : 54
Month : 8
Second : 25
Ticks : 637332512653065401
TimeOfDay : 08:54:25.3065401
Year : 2020
DateTime : Monday, August 17, 2020 8:54:25 AM


Windows PowerShell Transcript End
End time: 20200817085425


ok. i now have it working.

however, maybe someone could explain the logic.

findings -

Schedule created by Domain admin

when scheduled task was created by a domain admin user account to run as a domain service account with local admin rights the resulting script failed to take actions such as start/stop service but could query service.

Schedule created by the service account

when scheduled task was created by the domain service account with local admin rights the resulting script executed correctly and services started/stopped as required.

 

any light shed on this matter would really be appreciated.

cheers

Nick