0

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.

piort
  • 29
  • 3
  • Sorry, I have corrected the code. Name is a string. Same as you would write in powershell script: Get-PrinterPort -Name "somePortName . I don't think that problem is related to errors in commands and parameters because my code is working as intended: it is deleting, setting and adding new ports to printers properly, reading them properly etc. – piort Jan 18 '23 at 14:05
  • What's the purpose of `AddStatement`? See [Windows PowerShell Host Quickstart](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/docs-conceptual/developer/hosting/windows-powershell-host-quickstart.md#addstatement) – Tu deschizi eu inchid Jan 18 '23 at 15:30
  • In this example none but I wanted to show how I am invoking powershell scripts in my code. I am calling commands multiple time in my code so AddStatement() is called because I don't need a result of previous command. – piort Jan 19 '23 at 07:06

0 Answers0