Home > Back-end >  UIPath; execute simple powershell command: "(Get-PrintJob -PrinterName '015-pr362318-esr&#
UIPath; execute simple powershell command: "(Get-PrintJob -PrinterName '015-pr362318-esr&#

Time:05-11

I want to execute the following simple powershell command from within UIPath:

(Get-PrintJob -PrinterName '015-pr362318-esr').count()

and retrieve the output from within UIpath.

In order to execute the powershell command, I am currently using the invoke Power Shell activity from UIPath.

I tried already the following:

  1. calling the command directly from within the above activity. Result: Error message: The term .... is not recognized as the name of a cmdlet, function,....

  2. calling just "get-printjob". from within the above activity. Result: Error message: Unable to cast object of type 'Microsoft.Management.Infrastructure.CimInstance' to type 'System.Management.Automation.PSObject'

Hint: I have given the parameter -PrinterName via the parameter section of the invoke power shell activity.

Edit: This shows that the command: get-printjob is now recognized - but the resulting collection is of type ...CimInstance which cannot be cast to ...PSObject

Question: How can I use the get-printjob Powershell command within UIpath?


Update - Solution (thanks to Mathias R. Jensen)

1st The checkbox "is script" must be checked. This will recognize the following line: "Import-Module PrintManagement; @(Get-PrintJob -PrinterName '015-pr362318-esr').Count" within UIPath's invoke power shell activity.

2nd In order to extract the int32 value of the printjob count, put TypeArgument in the activity to: Int32

3rd Define the UIPath variable <yourReturnVariable> which you assign within Output in the UIpath activity as follows: Collection<Int32>

4th With an assign activity (after the invoke Power Shell activity), extract the number of print jobs as follows: int_Printjobs = <yourReturnVariable>.First()

CodePudding user response:

The full error message gives a hint:

The term 'Import-Module PrintManagement; @(Get-PrintJob -PrinterName '015-pr362318-esr').Count' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

So it's interpreting the entire script as the name of a single command to invoke - which obviously fails!

This might make very little sense if you're used to working with PowerShell interactively as a console application, how would you even attempt to invoke a command named like that?! (Hint: &)

But if you're working with the underlying API (like UiPath is), it's actually pretty easy:

PowerShell ps = PowerShell.Create();
ps.AddCommand("<# whole script text goes here#>");
// should have been `ps.AddScript("<# ... #>")`

The C# compiler happily compiles and executes this code, only you'll find at runtime that an error like the one you encounter.

So we need some way to instruct UiPath to call AddScript() instead of AddCommand(). According to the UiPath docs, the InvokePowerShell command element has an IsScript setting - my guess is that if you toggle it, it'll work! :)

  • Related