UIPath; execute simple powershell command: "(Get-PrintJob -PrinterName '015-pr362318-esr').count()" - powershell

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:
calling the command directly from within the above activity. Result: Error message: The term .... is not recognized as the name of a cmdlet, function,....
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()

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

I'm using GET CLI in flutter and everthing is working fone except for this command "get generate model on home from "https://randomuser.me/api/"

I've installed the GET CLI flutter on my windows machine and everything seems fine. Also, I've just created a few projects using that. But when I use this command
get generate model on home from "https://randomuser.me/api/"
then I'm getting this error from the terminal in the vs code
get generate model on home from "https://randomuser.me/api/?result=10"
Calls one batch program from another.
CALL [drive:][path]filename [batch-parameters]
batch-parameters Specifies any command-line information required by the
batch program.
If Command Extensions are enabled CALL changes as follows:
CALL command now accepts labels as the target of the CALL. The syntax
is:
CALL :label arguments
A new batch file context is created with the specified arguments and
control is passed to the statement after the label specified. You must
"exit" twice by reaching the end of the batch script file twice. The
first time you read the end, control will return to just after the CALL
statement. The second time will exit the batch script. Type GOTO /?
for a description of the GOTO :EOF extension that will allow you to
"return" from a batch script.
%~dp1 - expands %1 to a drive letter and path only
%~nx1 - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
environment variable for %1 and expands to the
drive letter and path of the first one found.
%~ftza1 - expands %1 to a DIR like output line
In the above examples %1 and PATH can be replaced by other
valid values. The %~ syntax is terminated by a valid argument
number. The %~ modifiers may not be used with %*
This is the version of my Get-cli
What is the issue here? Maybe that can be some windows issue. Please help me

Unable to call script function from command line

I am trying to call a function in a PowerShell script in the same directory. However, when I call it, I get this error:
The term 'functionName' is not recognized as the name of a cmdlet, function, script...
Any idea why this occurs? I also tried dot loading the script first like this before calling the function:
.\Script.psm1
.\Script.psm1 would run the script (script module, actually) in a child context. To be able to use functions from it in the current context you need to run/load it in the current context. That can be done via dot-sourcing for regular scripts, or via Import-Module (for modules).

Powershell Function Not Recognised

I've been putting my first powershell scripts together for the last couple of hours and I keep seeing an error that I can't seem to get to the bottom of.
I'm using the Powershell ISE tool to write and run the scripts.
To see if its something in my script I've created a super simple test script and I'm seeing the same problem. The entire test script is:
Test;
function Test
{
New-Item C:\Users\jgreen\Desktop\jammer\ -type directory
}
When I hit the Run Script button the error produced is:
Test : The term 'Test' 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.
If I simply hit the Run Script button again, it work and does the job. I simply do not understand what is wrong. I simply don't get it. Is there a problem with my script or not?
Why would a script that works bomb out the first time after opening the script in PS ISE?
You are calling the function before it is defined. The reason it works the second time, is a result of the first run. When it is ran the first time it's defining the function, so when you run the script the second time it knows what the function is.
You need to declare your function before you invoke it. It works the second time because then it's been declared. Think of how this would work if you were just at a powershell command line and you typed: "Test;" What would you expect to happen?

Lua: command lines parameters are nil on windows 8.1

I run the Lua script using command line:
scipt.lua arg
But when I want to print the value arg1 in script:
print(arg[1])
Result is nil.
When I try to run it like:
lua script.lua arg
It returns not recognized command for windows.
What I am doing wrong? How can I get parameters from command line?
I don't see any problem with your example. Since you are able to run this command, but do not get any arguments passed, it's possible that whatever script registered the association, didn't use the syntax for passing arguments. You can find the registered association and check the command to make sure it includes %* to pass all the parameters to the script.
You can find where the executable is by using where lua.exe command and then call that executable directly from the command line to see if it works.

Powershell script not being executed

I have a powershell script that takes an input parameter (int). the script then updates the status of the service based on this input parameter.
I have been working on this task using the powergui editor .
Screencap from powergui window
Whenever I try to run the script from the command line of powershell file, there is nothing that's being reported by powershell . No output .. nothing.
Screencap from powershell window
Can you please let me know what might be happening here.
Thanks
Maybe you called the programm services.exe from the command line. You might try to rename your function.
You might know the following allready, but did you parse your powershell script before you called your function? This is nessesary to include your function into the shell. You have to invoke D:\ps>.\myScript.ps before you can use the function.