Question

I need to convert a little C# application which is being used as an interface between two systems to a PowerShell cmdlet. The first system is calling the application with some parameters and gets back one of a couple of return codes like 0 for success or 1000 or 1001 for specific errors. Currently the cmdlet I've written is passing the return codes back to the pipeline with WriteObject(). My question is if there is a better way to do this espacially if the return code wasn't 0 and an error occured?

Thanks in advance for any suggestions!

Was it helpful?

Solution

I'm not sure what you are looking for by "better". That said here is a suggestion.

Have your cmdlet write to the error stream if an error occurs. You'd use WriteError instead of WriteObject. The powershell script could then execute your cmdlet in a try/catch and catch errors that way. You could also translate error codes to warnings if that made sense for your app. Use WriteWarning. And finally there is ThrowTerminatingError if you want to terminate a script. ThrowTerminatingError can be caught as well, but if not the script terminates whereas if Write-Error isn't caught the script continues.

Here's a whole section at MSDN on cmdlets and error reporting: http://msdn.microsoft.com/en-us/library/windows/desktop/dd878251(v=vs.85).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top