Error Message Using Console

Sep 14, 2009 at 1:10 PM

I am trying to run a script that I know works when I run directly in Powershell. I am getting the following error:

System.Management.Automation.RuntimeException: Exception setting "ForegroundColor": "Cannot convert null to type "System.ConsoleColor" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White"." ---> System.Management.Automation.SetValueInvocationException: Exception setting "ForegroundColor": "Cannot convert null to type "System.ConsoleColor" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White"." ---> System.Management.Automation.PSInvalidCastException: Cannot convert null to type "System.ConsoleColor" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White".
   at System.Management.Automation.LanguagePrimitives.ConvertNull(Object originalValue, Type resultType, TypeCode resultTypeCode)
   at System.Management.Automation.LanguagePrimitives.ValueToConvertIsImediatellyConvertible(Object valueToConvert, Type resultType, TypeCode resultTypeCode, Object& result, Type& originalType)
   at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider)
   at System.Management.Automation.Adapter.PropertySetAndMethodArgumentConvertTo(Object valueToConvert, Type resultType, IFormatProvider formatProvider)
   at System.Management.Automation.DotNetAdapter.PropertySet(PSProperty property, Object setValue, Boolean convertIfPossible)
   at System.Management.Automation.Adapter.BasePropertySet(PSProperty property, Object setValue, Boolean convert)
   --- End of inner exception stack trace ---
   at System.Management.Automation.Adapter.BasePropertySet(PSProperty property, Object setValue, Boolean convert)
   at System.Management.Automation.PSProperty.SetAdaptedValue(Object setValue, Boolean shouldConvert)
   at System.Management.Automation.PSProperty.set_Value(Object value)
   at System.Management.Automation.Parser.AssignableProperty.set_Value(Object value)
   --- End of inner exception stack trace ---
   at System.Management.Automation.Parser.AssignableProperty.set_Value(Object value)
   at System.Management.Automation.Parser.AssignmentStatementNode.Execute(Array input, Pipe outputPipe)
   at System.Management.Automation.Parser.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList)
 
 Any suggestions?
 
 

Nov 27, 2009 at 10:49 AM

Have you or anyone else managed to find a solution to this? Is this a problem that stems from the fact that whatever the default host object is, it does not contain non-null settings for colours? If so, is the solution to create a subclass of PSHost and pass to the constructor of the runspace?

Nov 27, 2009 at 1:39 PM

The problem was a "cls" that I forgot at the top of my script. This makes sense, as there is no screen to clear. I assume all other commands of that sort will have a similarly disastrous effect. Remove them and it should work.

Coordinator
Nov 29, 2009 at 9:29 AM

Indeed, this is not running inside a console, hence the issues you had. And keep in mind that you have to format the output into text, otherwise the ToString() method will be called for all the objects you will send to the output.