How to make public function - function

I would like to define function in file functions.ps1 and then call it from another script. Something like this:
Functions.ps1:
Function Hi()
{
"hi"
}
Call it from another script (Call.ps1).
Call.ps1:
invoke-expression -Command .\functions.ps1
Hi
But function is defined in local scope of script functions.ps1 and I get err:
The term 'hi' 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, v erify that the path is correct and try
again.
Is there a simple way to fix this ?

You have to dotsource your script to load it into your current runspace:
. .\functions.ps1
Hi

I think the way to do this would be to dot soruce the file in the script that should use the function(s).
So in the script that will use the function put something like this.
. .\functions.ps1
Then you can start calling funcitons in that file.
A bit by memory , but think it would work.
Edit: removed a brainfart.. :)

Yup, dotsource is what you're looking for.
If your script contains spaces, you would dot source it like this
."C:\Dotsourced script.ps1"
Without space, is as the other people say
.C:\function1.ps1
All the functions and logic contained in the dotsourced script will be loaded upon sourcing it. So try to keep the scripts your'e dotsourcing in functions, otherwise it will run when sourced.
Read more about it on SS64 for example:
https://ss64.com/ps/source.html

Related

unable to specify copy loop in JSON

Hi I'm trying to add a copyIndex into a script so I can pass an array of vm's into it.
The full script is at:
https://pastebin.com/embed_iframe/vQyyKrYn
The error I get is:
is not valid: The template function 'copyIndex' is not expected at this location. The function
can only be used in a resource with copy specified.
I do have the copy specified in the resource on line 44.. I think!
Any idea where I'm going wrong or how I could get this into a loop to iterate over one vm at a time.
Thanks in advance :)
Do you need to concat the copyIndex?
Instead of:
[parameters('vmName')[copyindex()]]
Use:
[concat(parameters('vmName'), copyIndex())]

Using variables inside function from dot sourced .ps1

I'm trying to create a config.ps1 file which contains all of the variables my scripts needs. Inside those scripts there are several functions. But when i dot source these variables they do not work inside my function. How do i get them to work?
. ./config.ps1
function RollOut {
write-host "$Rolling"
}
Where $Rolling is defiend in config.ps1
$Rolling="Go"
Thanks!
You need to make your variables in config.ps1 available on the global scope.
Change the definition to:
$Global:Rolling = "Go"

How to execute a menu.vim function from within vimrc?

I know that it is possible to call a vimrc function from menu.vim
but is it also possible the other way around?
p.e. Writing this in vimrc does't work:
nmap <F4> :call <SID>MyFunction_in_menuvim()<CR>
What did I wrong?
<SID> is used for local functions. If your function is not local, you do not need to add this. <SID> will be replace by a string like: <SNR>23_
See vim documentation on map for more information

Call a function that is not on the Matlab path WITHOUT ADDING THAT PATH

I have been searching an entire afternoon and have found no solution to call in matlab a function by specifying its path and not adding its directory to the path.
This question is quite similar to Is it possible to call a function that is not in the path in MATLAB?, but in my case, I do not want to call a built-in function, but just a normal function as defined in an m-file.
I think handles might be a solution (because apparently they can refer to functions not on the path), but I again found no way to create a handle without cd-ing to the directory, creating it there and the cd-ing back. Trying to 'explore' what a function handle object is and how to make one with a reference to a specific function not on the path has led me nowhere.
So the solution might come from two angles:
1) You know how to create a handle for an m-file in a specific directory.
2) You know a way to call a function not on the matlab path.
EDIT: I have just discovered the function functions(myhandle) which actually lets you see the filepath to which the handle is referring. But still no way to modify it though...
This is doable, but requires a bit of parsing, and a call to evalin.
I added (many years ago!) a function to the MATLAB Central File Exchange called externalFcn
http://www.mathworks.com/matlabcentral/fileexchange/4361-externalfcn
that manages calls to off-path functions. For instance, I have a function called offpathFcn that simply returns a structure with a success message, and the value of an input. Storing that function off my MATLAB path, I can call it using:
externalfcn('out = C:\MFILES_OffPath\offpathFcn(''this is a test'')');
This returns:
out =
success: 1
input: 'this is a test'
(Note that my implementation is limited, and improvable; you have to include an output with an equal sign for this to work. But it should show you how to achieve what you want.)
(MathWorks application engineer)
The solution as noted in the comment 1 to create a function handle before calling the function is nicely implemented by #Rody Oldenhuis' FEX Contribution:
http://www.mathworks.com/matlabcentral/fileexchange/45941-constructor-for-functionhandles
function [varargout]=funeval(fun,varargin)
% INPUT:
% fun: (char) full path to function file
curdir=cd;
[fundir,funname]=fileparts(fun);
cd(fundir);
[varargout{1:nargout}] =feval(funname,varargin{:})
cd(curdir);
I've modified Thierry Dalon's code to avoid the use of feval, which I always feel uncomfortable with. Note this still doesn't get around cd-ing to the directory in question, but well, it happens behind the scenes, so pretend it doesn't happen :-)
Also note what Ben Voigt pointed out above: calls to helper functions off the path will fail.
function [varargout]=funeval(FunctionHandle, FunctionPath, varargin)
% INPUT:
% FunctionHandle: handle to the function to be called; eg #MyFunction
% FunctionPath: the path to that function
% varargin: the arguments to be passed to Myfunction
curdir=cd;
cd(FunctionPath)
[varargout{1:nargout}] = FunctionHandle(varargin{:});
cd(curdir);
end
and calling it would look like
Output = funeval(#MyFunction, 'c:\SomeDirOffMatlabsPath\', InputArgToMyFunc)
The run command can run a script file from any directory, but it can't call a function (with input and output arguments).
Neither feval nor str2func permit directory information in the function string.
I suggest writing your own wrapper for str2func that:
saves the working directory
changes directory to the script directory
creates a function handle
restores the original working directory
Beware, however, that a handle to a function not in the path is likely to break, because the function will be unable to invoke any helper code stored in other files in its directory.

How do I define custom function to be called from IPython's prompts?

I had an old ipy_user_conf.py in which I included a simple function into the user namespace like this:
import IPython.ipapi
ip = IPython.ipapi.get()
def myfunc():
...
ip.user_ns['myfunc'] = myfunc
Then, I could use myfunc in the prompt.
However, I updated to IPython 0.12.1 and now the ip_user_conf.py does not work. I haven't seen how to translate such a custom function for prompts to the new configuration model.
Which is the way to do this?
Best regards,
Manuel.
UPDATE: Changed the subject to question
After reading a bit of the documentation (and peeking at the source code for leads) I found the solution for this problem.
Simply now you should move all your custom functions to a module inside your .ipython directory. Since what I was doing was a simple function that returns the git branch and status for the current directory, I created a file called gitprompt.py and then I included the filename in the exec_file configuration option:
c.InteractiveShellApp.exec_files = [b'gitprompt.py']
All definitions in such files are placed into the user namespace. So now I can use it inside my prompt:
# Input prompt. '\#' will be transformed to the prompt number
c.PromptManager.in_template = br'{color.Green}\# {color.LightBlue}~\u{color.Green}:\w{color.LightBlue} {git_branch_and_st} \$\n>>> '
# Continuation prompt.
c.PromptManager.in2_template = br'... '
Notice that in order for the function to behave as such (i.e called each time the prompt is printed) you need to use the IPython.core.prompts.LazyEvaluation class. You may use it as a decorator for your function. The gitprompt.py has being placed in the public domain as the gist: https://gist.github.com/2719419