c# - issues with powershell script

126 Views Asked by At

I am working on script which uses powershell to add, remove ports to the specific printer and it works really nice! App works on 4.6.2 framework and I am referencing System.Management.Automation.dll from nuget Microsoft.PowerShell.5.1.ReferenceAssemblies.1.0.0

The problem is that in my logs I am getting exceptions from System.Management.Automation.PSInvalidCastException like that:

System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.Printer.RenderingModeEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.Printer.PrinterStatus" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.Printer.TypeEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.Printer.DeviceTypeEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.Printer.WorkflowPolicyEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrinterConfiguration.DuplexingModeEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrinterConfiguration.PaperSizeEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrintJob.JobStatus" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.TcpIpPort.ProtocolEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.WSDPrinterPort.DiscoveryMethodEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) | 
System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrinterProperty.PropertyTypeEnum" value of type "System.String" to type "System.Type".
   at System.Management.Automation.LanguagePrimitives.ConvertStringToType(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) |

I am adding commands, parameters and calling powershell like this:

using (var powerShell = Create())
{
powerShell.AddCommand("Get-PrinterPort").AddParameter("Name", "somePortName").AddStatement();
var output = powerShell.Invoke();
}

Has anyone an idea how to get rid of these errors?

I was trying with few versions of System.Management.Automation.dll but I am limited by System.Runtime 4.1.2 version.

0

There are 0 best solutions below