1

Here is the problem:

We have a remote server that can execute commands on our servers. We have lots of servers that run different Applications. We have scripts written on the box in powershell that will start our applications on a server by server basis. However, when we execute them remotely they are executed under the wrong username and password. Therefore, I was going to create a scheduled task on the box that will execute a startup script with the correct username and password. However, they would also like to get return values from these scripts to indicate success, failure, or whatever. I know that Scheduled tasks do have error codes associated with them but how can I create my own and retrieve it remotely.

The question:

Is it possible to execute a scheduled task remotely and get an exit code for it?

ex: schtasks /run /tn "StartupApplications"

Or are you aware of a better solution? I tried using a powershell credential script:

$credential = New-Object System.Management.Automation.PSCredential $username,$password
exit ((start-process powershell -ArgumentList '-NoProfile -File "C:\Scripts\Startup.ps1"' -Credential $Credential -Wait -Passthru).ExitCode)

But the keys I created using ConvertFrom-SecureString were tied to my username Information on that problem

Community
  • 1
  • 1
richbria90
  • 159
  • 2
  • 5

1 Answers1

0

I suggest using job rather that scheduler.

start-job -ScriptBlock {. "C:\Scripts\Startup.ps1"; $LASTEXITCODE} -Credential $Credential | wait-job | receive-job 
Jackie
  • 2,476
  • 17
  • 20