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

1 Answer 1

0

I suggest using job rather that scheduler.

start-job -ScriptBlock {. "C:\Scripts\Startup.ps1"; $LASTEXITCODE} -Credential $Credential | wait-job | receive-job 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.