How to add a script to the standard path in PowerShell? - powershell

how can i add a custom function / object to the standard array of recognized functions in PowerShell so that i can call it from the shell of PowerShell?
Thanks

You can put the function into your profile script. You can find out where this is by looking into the variable $profile. That script will be run automatically on starting Powershell (if you are allowed to run scripts) and functions declared in it will be available in every session.

Related

How to pass parameters to a Powershell script with UiPath

I am trying to run a powershell script with uipath. I have found two approaches to run it;
Read the script as a text and pass to Invoke Powershell activity
Use Run powershell script activity from Script Activities package
Now I need to pass arguments to the powershell script from uipath. Some have mentioned about formatting the string with parameters and invoking the script.
But, rather than that, think we can directly pass parameters from UiPath.
In Run Powershell script, it has Parameters as input
In Invoke Powershell, it has Parameters under Input section and PowershellVariables under Misc
I have been searching for a while. But still I am unable to figure out how to pass parameters using above activities.
I am trying to send outlook mails using powershell. Still in the process of learning how to work with it. Plz help…
EDIT
Found solution for one approach. Added it as an answer.
Found an answer from this link. Out of the two approaches, it provides a solution to pass the parameters using Invoke Power Shell activity.
Read the .ps1/.txt file containing the script, with Read Text File activity
Call the Invoke Power Shell activity by passing file content as CommandText and parameters in the Parameter collection.
But, it would be great, if I can get to know how to pass parameters using Run power shell script activity.

How to pass value to an exe and then the next button should be executed by powershell?

I have the code below which executes an exe file and then i want the path which is assigned to a variable to be copied into the text box of that exe and once it gets copied then the next button in the exe should get clicked\executed automatically by powershell.
& "D:\SOFTWARE\notepad ++.exe"
$Path="C:\Program Files"
Basically i will be using this code for some other exe file but process would be same. So is there any way by which i can do this by using powershell?
Consider the snapshot below of the UI of application installed. If i want to pass the product key which i have declared in a variable inside the powershell after passing it to the application and then i want its Get Product Details button to be hit\Run.
What you actually want is pass the variables into the msi by running it quietly without UI. Try the following first (assuming your exe is actually just an MSI wrapper)
msiexec.exe /i $PathToExe /q /l*vx log.txt
This should quietly launch the msi and produce a log.txt file. Wait a few moments for it to complete since it is installing in the background. Then check if it correctly installed. Then check the log file to see all the variables that were set at the end. Guessing by their names or values, you could then pass them in the next time simply by adding variablename=variablevalue to the command line call. You might need to experiment and learn a bit about MSI and msiexec as part of this.
If talk about invoking UI actions in general, then you would need to look at the AutomationElement class but it can get very complex here on...
You should look at using SendKeys.
https://blogs.technet.microsoft.com/heyscriptingguy/2011/01/10/provide-input-to-applications-with-powershell/

How would I run a Lua script with user specified parameters from inside another Lua script?

How would I run a Lua script with user specified parameters from inside another Lua script?
Would the below code work? Where "content_image" is my specified input image (either saved to an image file, or still in the script) into the "deepdream.lua" script, and "output_image" is the output from the "deepdream.lua" script that I want to use in my Lua script.
dofile("deepdream.lua -content_image content_image -output_image output_image")
The script I am seeking to run within another Lua script can be found here: https://github.com/bamos/dream-art/blob/master/deepdream.lua
If you want to load and execute a script by passing it a number of parameters, you have to do this by... loading the script and executing it by passing it a number of parameters:
local chunk = loadfile("deepdream.lua")
chunk("-content_image", "content_image", "-output_image", "output_image")
Note that this will not fill in args for the arguments the way lua.exe does. It will pass the parameters as variadic parameters, just like any other Lua function. So it can mess with your globals and so forth. Also, unlike executing lua.exe, this will be executed in the current process, so if it errors out, the error will have to be handled by you.
If you want, it wouldn't be difficult at all to write a function that takes the string you provided, uses Lua patterns to parse parameters and so forth, and then loads the script with those parameters.
If you want to execute a script exactly as if you had used lua.exe on it, then you would just use os.execute:
os.execute("lua.exe deepdream.lua -content_image content_image -output_image output_image")
you can use loadfile with parameters in arg:
loadfile("deepdream.lua")({content_image="content_image",output_image="output_image"})
in deepdream.lua:
local arg={...}
local content_image = arg[1].content_image
local output_image = arg[1].output_image

Execute PowerShell function on change of path

Is it possible to execute some code (eg. a function, script block, cmdlet etc) whenever the current path changes in the PowerShell console or ISE?
The scenario I am thinking of is to modify some environment variables and dot source some location-specific PowerShell functions depending on the current folder.
You have several options. You can remove the "cd" alias and then write a "cd" function and add the desired logic. The downside to this approach is that if someone uses Set-Location, your function is bypassed. Another option is to create a proxy command for Set-Location. Shay (and Kirk) have a video on how to do this here. The nice thing about this approach is that the built-in ways to change dir (cd and Set-Location) will go through your proxy command.

Run a PowerShell script from another one

What is the best and correct way to run a PowerShell script from another one?
I have a script a.ps1 from which I want to call b.ps1 which does different task.
Let me know your suggestions. Is dot sourcing is the best option here?
Dot sourcing will run the second script as if it is part of the caller—all script scope changes will affect the caller. If this is what you want then dot-source,
However it is more usual to call the other script as if it were a function (a script can use param and function level attributes just like a function). In many ways a script is a PowerShell function, with the name of the file replacing the naming of the function.
Dot sourcing makes it easier to at a later stage convert your script(s) into a module, you won't have to change the script(s) into functions.
Another advantage of dot sourcing is that you can add the function to your shell by adding the file that holds the functions to Microsoft.PowerShell_profile.ps1, meaning you have them available at all times (eliminating the need to worry about paths etc).
I have a short write-host at the top of my dot sourced files with the name of the function and common parameters and I dot source the functions in my profile. Each time I open PowerShell, the list of functions in my profile scrolls by (If like me, you frequently forget the exact names of your functions/files You'll appreciate this as over time as the number of functions start to pile up).
Old but still relevant.
I work with modules with "Import-Module ", this will import the module in the current powershell session.
To avoid keep in cache and to always have the last changes from the module I put a "Get-Module | Remove-Module" that will clear all the loaded modules in the current session.
Get-Module | Remove-Module
Import-Module '.\IIS\Functions.psm1'