Powershell tasks not working - VisualCron - Forum

Community forum

JoeCron
2017-08-14T10:08:12Z
Trying to get some powershell tasks to work and they are all failing with the same error:

Taken from the server log file:

14/08/2017 10:47:53 Info User 'Joe' - Ran Job: Test Log Monitor
14/08/2017 10:47:53 Info Task started(PowerShell): PowerShell (147593)
14/08/2017 10:47:53 Debug userToken: 5552, userTokenEx: 7352, Desktop: WinSta0\Default
14/08/2017 10:47:53 Err Unable to start process: A required privilege is not held by the client
14/08/2017 10:47:53 Info Task completed (Failure)->'PowerShell' (147593)
14/08/2017 10:47:53 Err Job completed (Failure)->'Test Log Monitor'

When logged into the server with the same user credentials, the scripts run fine...
Any ideas?

SnapDiligence
2017-08-15T17:36:19Z
Originally Posted by: JoeCron 

Trying to get some powershell tasks to work and they are all failing with the same error:

Taken from the server log file:

14/08/2017 10:47:53 Info User 'Joe' - Ran Job: Test Log Monitor
14/08/2017 10:47:53 Info Task started(PowerShell): PowerShell (147593)
14/08/2017 10:47:53 Debug userToken: 5552, userTokenEx: 7352, Desktop: WinSta0\Default
14/08/2017 10:47:53 Err Unable to start process: A required privilege is not held by the client
14/08/2017 10:47:53 Info Task completed (Failure)->'PowerShell' (147593)
14/08/2017 10:47:53 Err Job completed (Failure)->'Test Log Monitor'

When logged into the server with the same user credentials, the scripts run fine...
Any ideas?



Are the scripts running locally on the server or remotely? Have you tried running the script in the ISE and seeing what error message it kicks out? I think that's a file Read/Write permission error. If you're running it remotely, you might want to check what credentials the agent is actually using.
Support
2017-08-16T11:37:50Z
Can you create a simple powershell script on a local path that just;

write-output "Hello world"

Does it work?
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
JoeCron
2017-08-17T09:27:42Z
The "Hello World" works ok when the code is in the Task..
But how do we get an Exit code from the powershell script to return to VisualCron?
as soon as i introduce the Exit function, the code doesnt run.

Try this:

Write-Output "This Should Error..."
exit 1

this doesnt work, No output, No error and exitcode = 0, Success!

So how do we get Exit codes from powershell script?








JoeCron
2017-08-17T09:46:58Z
So I've resorted to this method to get a PowerShell script to return a non-zero exit code:

Write-Output "Testing for errors..."
Write-Error "This is an error"
return

If i switch on "Extra Task Debugging" i get all kings of strange ASCII at the start of the output!


SnapDiligence
2017-08-17T17:30:13Z
Originally Posted by: JoeCron 

So I've resorted to this method to get a PowerShell script to return a non-zero exit code:


Write-Output "Testing for errors..."
Write-Error "This is an error"
return

If i switch on "Extra Task Debugging" i get all kings of strange ASCII at the start of the output!




Generally, I've found that PowerShell scripts can only return two things that VisualCron can pick up on:
1) Write-Output == StdOut or "Output"
2) Write-Error == StdErr or "Output (stderr)"

(If I'm wrong, please correct me as that functionality would be useful in several of my scripts.)

To get different exit codes, you could probably either have the script connect to the server through the API and modify the Exit code in the base task object (which is probably the nuclear option/doesn't work if the task is run as multi-instanced; I'm also not sure if that field is exposed in the object through the API) or use the "On Error" options combined with returning the exit code and message in the StdErr.

To expand on the second option, to just have the task's "On Error" return a catch-all custom exit code by checking for a non-empty/null StdErr return and leaving the real exit code in the StdErr, you might want to do the following:

In the script, add this block to your try-catch statements or wherever you're doing the error handling:
Quote:


$errorCode = 1
Write-Error("Exit Code: $errorCode`n$errorMsgString")
exit 1



You would have to build your $errorMsgString yourself. Look at this blog post  for more information on what fields can be dragged out of the error object and returned in your error message.

Locate all locations in the script that return non-fatal error output, which includes cmdlet/function calls that are not assigned to a variable or being captured and handled in some way. Append "> Out-Null" to the end of the cmdlet/function calls to redirect their screen output/return to nowhere. This'll allow you to correctly target the strings you want returned.

To set the "On Error" options:
1) Navigate to the "On Error" tab in the task.
2) Navigate to the bottom half of the pop-up under "Output/Variable error handling".
3) Select "Raise error" from the drop down menu.
4) Set the custom error code.
5) Select "If error output" (if the error string is returned as Write-Error).
6) Select, under "Condition", "Not Equal (!=)".
7) Leave the bottom field blank.

I think what this should do now is throw an error if anything is returned as StdErr. From there, you can now locate your tasks that exit with your custom catch-all exit code and then read their StdErr string in the GUI/API for the real exit code.

That said, you lose access to non-handled, non-fatal error messages in the StdErr field if you do this, which may be undesirable.
The upside is that you would now be able to differentiate between jobs/tasks that return a VisualCron exit code and ones that are returning your exit code (in the StdErr). Basically, it's your solution plus a little extra.
Scroll to Top