Powershell Function Not Recognised - powershell

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?

Related

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

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! :)

powershell running the code instead of create a new line

I think that my question is something too easy that you guys will solve in 1 minute.
I'm trying to run a script that have multiple lines of code. But, when I write the first line and hits SHIFT+ENTER it runs the code. I need to write a new line, instead of running what I've wrote.
Anybody knows what should I do (instead killing myself because I'm too dumb) ?
In powershell console there are a few ways to make a new line
A. Shift + Enter : Use this at any point to make a new line
B. The opening of a string " or ' until the closing of the string " or ' : use this when you have a string that you wish to span many lines
C. A pipe | : Use this if you have output that you would like to pass to another command
D. The Back tick (escape char) ` : use this to separate lines for a new command or splitting a command into other lines
If you are new to powershell, I would suggest using Powershell ISE. If its installed you can go to the powershell console and type ISE or go to start and type Powershell ISE. This will be a good place to run scripts and debug as you can add breakpoints to your scripts.
The easiest and best way to do this would be to create the script inside of the PowerSheell ISE program. You can then reference this script and run it in the console by preceding it with a .\script.ps1.
If needed you can create script on the command line by creating and writing to the file from the console.
Open the PowerShell console
Run the following command to create a file New-Item script.ps1
Run the next command as many times as it takes to populate the file Add-Content script.ps1 "My code line here"
Run the code using the script run command .\script.ps1
Now let it be known that the ISE is a much better tool because it allows for debugging of files and testing them on demand. The only downside is it will cache whatever it uses or creates (such as variables or references). If you aren't getting the expected result trying closing and reopening to clear the cache run it from the console in tandem. One last thing to note is that if you use the ISE and it successfully runs there that doesn't mean it will run in the console. Be sure to test thoroughly.

Python os.system & subprocess do not recognize Microsoft's ie4uinit.exe

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.

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 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.