Hello – I keep getting stuck on functions. I have a few that I want to run and they aren't in my profile.
I've tried the "dot sourcing method" and it just fails everytime, and it's probably something I'm doing wrong.
What's the best way to call a function if it's not in your profile but you want to run it from the shell you have opened?
Thanks as always, you all Rock!
You have to understand that a function is meant to be run *by something else.* With that in mind, it's probably easiest to save your functions into a script module. That's just saving them into a file that's the proper path, and giving the file the .psm1 filename extension. One module can hold many functions, and you'll be able to run them just like a PowerShell command – no path or anything needed.
If you need to use the function from within a scheduled task, make sure the function is in a module on whatever machine will run the task. Write a script that calls the function, and schedule the script.
If you don't need the function to be a function, then put it in a script by itself and remove the function keyword, function name, and opening and closing curly brackets.
This is all pretty well laid-out in "Learn PowerShell Toolmaking in a Month of Lunches," along with a lot of other suggestions for making your functions behave in a standardized, consistent fashion.
You must be logged in to reply to this topic.