Community forum

Please note that VisualCron support is not actively monitoring this community forum. Please use our contact page for contacting the VisualCron support directly.


Alex55
2015-10-28T17:46:38Z
I'm trying to run a Powershell task in a VisualCron job and expect to receive the same exit code and output as provided by Powershell. However, the exit status for a Powershell task is always 0 with no output or errors at all, I am attaching a simple PS script to demonstrate the issue.
  test3.txt (1kb) downloaded 140 time(s).

That is a txt file as your upload form doesn't allow for ps1 files. Your documentation specifically says:

Quote:


Passing back data from PowerShell
You can use these PowerShell commands to pass back output to VisualCron:
VisualCron standard output
write-output "my output"
VisualCron standard error
write-error "my error"



and that's what I am using in attached sample. Powershell prints the output into the console and exists with status 1, in VisualCron, however, I am getting 0 exit code and no output at all:

Capture.PNG

I tried setting "All errors" in "On error" configuration page but that didn't quite help. Could you please check if you're getting the same behavior in your deployments and let me know if there's any solution or workaround for this?
Sponsor
Forum information
KJDavie
2015-10-28T21:53:12Z
Hi Alex55 and Welcome.
When you run the code inline from VisualCron within a powershell task there are some limitations.

Per your example:
If you Exit (Raise an Error) you will get Error-Output Only

If You Don't Exit 1 at the end (Comment Out Line 3 - The Exit) you will get the output and error output you expect ? - But Success
2015-10-29_Alex55 - No Exit.png

If you Exit (Raise an Error) you don't write to Error-Output (Comment Out the Write-Error) you will not get output at all.

If you Throw an Error you get the Throw, and an Error but its noisy (and the second Job shown is an example of the solution discussed below):
2015-10-29_Throw Examples - Powershell Task and Execute Task using Powershell_exe.png

If you call a script using the Powershell Executable Using an Execute Task, you will get behaviour consistent with if you ran it yourself (via Powershell, the Powershell UI or third party Products such as PowerShellGUI).

For this reason we use Powershell tasks to run short code snippets:
2015-10-29_Powershell Snippet Example.png

but generally for consistency of behaviour with testing we use an execute task and powershell.exe.

For example (Passing VisualCron Variables) - and for the other reasons shown:
2015-10-29_VC Calling Powershell script using Powershell,exe.png

Good Luck

Kevin
Alex55
2015-10-31T09:19:46Z
Hello Kevin,

Thanks for your reply. I have changed the task type to "Execute" and now getting expected output and error messages in VisualCron. However, the exit code of any error is always 1. For example, if a script exits with a status of 6, as confirmed by $LASTEXITCODE in Powershell, VisualCron still displays it as 1.

Getting the correct exit code is desirable, as the script has an associated reference that maps particular exit codes to actions that need to be taken. Since VisualCron always sets exit code to 1 in the execute task, and doesn't specify actual script exit code anywhere else, it's a bit problematic. Is there any workaround to get actual exit codes in VisualCron for PS scripts?
That's not much of an issue, as we do have error output in VisualCron console, but correct exit codes for PS scripts would still be nice.
KJDavie
2015-11-01T05:13:19Z
Ok,

We are getting into some areas of deficiency in powershell I think !

ymmv depending on powershell version but 2 Things for you to try.

Below is on VisualCron 7.7.7.

1) Change Your Command Line to force it to run it as a file rather than the default command . . . so your parameter line now looks something like:

-File "C:\Batch\Test3.ps1"
rather than just
"C:\Batch\Test3.ps1"

You *May* Get your Exit Codes now straight out of the box, as *many* values are also defined in the Windows (Default) Exit Code Collection.

But using this collection everything but a zero is a bust . . . so depending on your scripts / exit codes see below.

2) But now you can also tell VisualCron what return codes you are expecting using an Exit Code Collection and what action you want.

So :
a) Go Into the On Error Tab in the Execute Task
b) Observe Exit Code Collection - Windows (Default)
c) Click on the 'Gears' Button to the Right
d) Click on Collection and Choose Add and Give Your Collection a Name
e) Setup Your Expected Error Codes and also whether they are a Warning (Result: Success) or Error (Result: Failure) - This controls how you want VisualCron to Handle it.
Error Code Collection Powershell - Values.JPG

Go Back and set that Exit Code Collection for this Task in the On Error Tab (it should now be an option in the drop down) and give it a try.
Error Code Collection Powershell.JPG

As shown above . . . if you dont specify a value in your collection, or use the Windows Default and exit with as a 40 which isn't set then you will get the default behavior specified on this page 77777

You can also use this technique for unusual return codes from other exes (like Robocopy).

i.e. We have an Exit Code Collection for RoboCopy as some of the Exit Codes > 0 are warnings / extra information on a success rather than an error so the standard Windows (Default) Error Collection is not cool.

Cheers

Kevin
Support
2015-11-02T10:07:58Z
If you could provide a sample that does not return output as expected please send it to support@visualcron.com. I am thinking about when using the PowerShell Task - not the Execute Task.
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
Scroll to Top