Powershell save functions - function

I already know how to make a function in powershell. The problem is: How do I save that function so I can use it in the future?
When I write myFunction{3+3} in Powershell I can use that function in that session.
Altough, if I quit powershell and open it again, that function is gone. How do I "save" the function so I can use it even after I restart Powershell?

There are several ways:
You can compose it to a module and load it with import-module functions.psm1 into a script or via use the ps profile.
You can also dot sourc any saved functions into another ps1 ( . .\functions.ps1).

Put it in your Powershell profile:
Rather than explain it here, try this article:
http://www.howtogeek.com/50236/customizing-your-powershell-profile/

Related

Setting envs or globals in bash script [duplicate]

How can I keep the environment variables, set from a shell script, after the script finishes running?
This is not possible by running the script. The script spawns it's own sub-shell which is lost when the script completes.
In order to preserve exports that you may have in your script, call it
either as
. myScript.sh
or
source myScript.sh
Notice the space between the . and myScript.sh; also note that "source is a synonym for . in Bash, but not in POSIX sh, so for maximum compatibility use the period."
run the script as follows:
source <script>
-OR-
. <script>
This will run the script in the current shell, and define variables in the current shell. The variables will then be retained in the current shell after the script is done.
Environment variables exist the the environment that is private to each process. The shell passes a COPY of exported variables to it's children as their environment, but there is no way for them to pass their environment back to the parent or any process other than their children.
You can print those variables and load them into the parent.
Or as has been already mentioned, you can run your script in the current shell with either "source script" or ". script" (and you might need ./script if . isn't in your PATH).
Some tools print their vars and the shell can load them using backticks like ssh-agent. That way whatever ssh-agent prints will be run as a command. If it prints something like "VAR1=VAL;VAR2=VAL2" that may do what you want.

When you write a Powershell function how long does it last in memory?

When you write a Powershell function how long does it last in memory. So I have developed a custom function and ran it to create the function, does this function remain on my machine permanently so all I need to do is call the function? If i turned my machine on and off and re-booted can I call the function etc. Does another user need to run the function first before they can use it?
The function exists only in the session where it's code is executed. As soon as you close the Powershell process it's gone. But you can just load up the function definition again and it's back. If you want to have a function in every powershell session available create a module and add it to you userprofile (here is how).
function foo {write-host "I am alife"}
foo
Powershell functions last as the powershell session itself, so when you define a function in the console, you can still call it as this console is running, once you close it, you will have to define the function again.
However, Powershell has specific scripts in the system that runs whenever Powershell session is started, these scripts are called Powershell profiles.
So in your case, if you put the function in Powershell profile, it will be defined directly when you open your Powershell session and it will be permanently defined.
Look here to see about powershell profile
This code will put your function in the profile:
New-Item -Type Directory -Path "$home/Documents/WindowsPowerShell"
"<Your Function Here>" | Out-File -Force -FilePath $profile.CurrentUserAllHosts
Now whenever you open a new Powershell console (or any powershell process runs), your function will be loaded (even after you reboot your system).
If you have a powershell file (.ps1) that contains many functions, you can simply copy its content to $Profile.CurrentUserAllHosts, or put this code in $Profile.CurrentUserAllHosts to import your file as Powershell starts.
Import-Module "<Your File (.ps1) Path Here>"
Update
I forgot to tell you that you have to change your Powershell privacy policy to unrestricted, otherwise you will get error telling you that running script is disabled on this system.

Filemaker - how/where to use custom function?

I have downloaded the BaseElements plugin for Filemaker and managed to get it installed, I have downloaded this specifically to make use of "BE_ExportFieldContents" (https://baseelementsplugin.zendesk.com/hc/en-us/articles/204700538-BE-ExportFieldContents) which basically allows me to export from a Container field on a server side script. I have looked through the documentation and cannot seem to find help.
Now I have the function, I'm completely at a loss on how to actually call the function? I want to export something from the container file to the filemaker documents path - so my question is, where and how the hell do I use this function in Filemaker? Apologies in advance for the noob question.
You make a script where you call this function from the record in question. This script can be run in the client, or via a schedule on FileMaker Server or via the Perform Script on Server script step.
The syntax is like this:
BE_ExportFieldContents ( field ; outputPath )
Where the ‘field’ parameter is the container field and the ‘outputPath’ is where you want the file to end up.
Usually you call such functions via the Set Variable script step. After the execution the variable contains any error or result from the call.
Note that the plugin needs to be installed and enabled on the server for it to work there.

Best practice for writing equivalent powershell scripts and functions

I have a script that takes in multiple parameters, and that I've documented with proper help comments (e.g. .SYNOPSIS, .DESCRIPTION, .PARAMETER). Several different users in my organization are going to use this powershell script, some who know powershell and will call it from powershell with specific parameter values, and some who don't know powershell and will simply right-click on the script file in Windows Explorer and choose Run with PowerShell (so the parameters will use their default values).
My conundrum is what is the best way to do this in powershell without a bunch of duplicate code. The way I see it, these are my options:
1 - Just write a DoStuff.ps1 script that provides default values for all parameters. This allows it to be ran directly from Windows Explorer, but feels clunky for the powershell users that want to use it as a function from their own scripts, since instead of writing:
Do-Stuff param1 param1
they will be doing:
.\DoStuff.ps1 param1 param2
2 - Within DoStuff.ps1, move the operations it performs into a DoStuff function, and after the function declaration call the DoStuff function with the parameters passed into the script. This would still allow the script to be ran from Windows Explorer, as well as developers to dot source the script into their own scripts so they can access the function. The downside is that when the developers dot source the script, the script is going to call the function with the default parameters (unless I allow them to provide an optional Switch parameter to the script that triggers the function to not be called). Even with this though, it means that I would have to duplicate all of the scripts help text so that it shows for both the script and the function (description, parameter descriptions, etc.).
I can't think of any other options. Ideally I would just be able to write functions in .ps1 file and tag a function with a "default" keyword so that if the script is called, that function is ran by default; but I don't think PowerShell provides anything like this.
What do you think is the best approach in this situation. Is there something I'm overlooking or don't know about? Thanks.
but feels clunky for the powershell users that want to use it as a function from their own scripts
Default parameters would seem, based on your description, to be the best (or, at least, least-worse) approach.
But rather than naming your script DoStuff.ps1 name it and call it so it can be called more like an internal function:
Name it with the dash: Do-Stuff.ps1
Remember you don't need to specify the ps1
If the script is in a folder in $env:Path then you don't need to specify a path.
Also consider a script can load a module from a relative path: you could put most of the code in a script module which the front end (right click on it) script loads and calls into it. Script authors load the module themselves.

function defined in .zshrc not found when called from a script

I have defined some variables, aliases and functions in my .zshrc file:
export MY_VAR="example"
alias my_alias="echo an example"
function say_hello
{
echo "say hello"
}
I have verified that all three are defined and whatnot when called from the terminal. However, when I try to call the function say_hello from another script (which itself is called from the same terminal), the function does not appear to be defined. I see a 'command not found' error. The alias and variable MY_VAR appear to be defined just fine when referenced from this other script.
Any ideas on what might be going on? Thanks.
When zsh is called from a terminal it is invoked in interactive mode that causes zsh to source additional configuration files, including $ZDOTDIR/.zshrc ($HOME/.zshrc). By default (in non-interactive mode in non-login shell) it sources only /etc/zsh/zshenv* and $ZDOTDIR/.zshenv ($ZDOTDIR is most of time $HOME) files, see last but two section of man zsh named STARTUP/SHUTDOWN FILES. I have no idea why alias is defined (how exactly did you check?), but MY_VAR is defined in script because you exported it.
* /etc/zshenv according to doc, much likely they are Gentoo maintainers of zsh package who changed it to /etc/zsh/zshenv.
Note: it is bad idea to put such functions into .zshenv file as it creates implicit dependency. You should use script libraries instead: put it into
~/.zsh/say_hello.zsh
and do
source ${ZDOTDIR-$HOME}/.zsh/say_hello.zsh
in both your script and .zshrc.