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).
Related
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! :)
I'm relatively new to batch scripts.
The below command with * displays all matching files
dir deploy*.bat
But the same does not work with call command
call deploy*.bat
It results in
'deploy*bat' is not recognized as an internal or external command,
operable program or batch file.
How can work this around?
Background:
On a different groovy script, based on the parameter passed, I'm either deploying deploy_sit.bat or deploy_uat.bat. Irrespective of the file deployed, I want to be able to call the file deploy*.bat
You could use the Resolve-Path cmdlet to do that:
call (Resolve-Path deploy*.bat).Path
I'm trying to call ie4uinit from a python script so that I can update the icon in my task bar. ie4uinit.exe -ClearIconCache does just that, however whenever python calls it, either with subprocess.call or os.system, the following error comes up and the program is not called.
"ie4uinit.exe" is not recognized as an internal or external command,
operable program or batch file.
I have tried giving the full path location i.e.
"C:\Windows\System32\ie4uinit.exe"
Same result.
I have even made a .bat to abstract the call.
I suspect the problem is stemming from a difference in permissions between calling the command from the command line, and calling it through python.
But I don't know how.
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?
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.