Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a PowerShell "main" function?

Tags:

powershell

I've used many scripting languages in the past, and am now learning PowerShell. In these other languages I typically define my main logic prior to defining my functions, so someone reading the code will focus on the main logic first. Often this takes the form of creating a "main" function or class at the top of the file, and invoking it at the bottom.

In PowerShell this pattern might look like this:

$main = {
    ...
    do-something
    ...
}
function do-something() {
    <function definition here>
}

& $main

This works well, but I now want to leverage PowerShell's ability to run code remotely. Since $main is a PowerShell ScriptBlock object, I [think I] can run that code on a remote machine like this:

Invoke-Command -ScriptBlock $main -ComputerName whatever;

However, the remote machine will know nothing about my function since it is defined outside the scope of $main. I can, of course, move the definition of the function into $main, but then I have to put it above the main logic and I'm right back to my first problem.

Is there a common pattern for writing PowerShell scripts where the main logic is in a function or script block at the top of the file, similar to many traditional languages? How do people write complex scripts -- do they always just write them from top-to-bottom, sprinkling in functions as needed at the top?

Someone has flagged this as a possible duplicate of the question Why do I need to have my functions written first in my Powershell script?. This is not a duplicate of that. I know why functions need to be defined first -- I've been programming since C was the cool new language.

What I'm asking for is the pattern for doing so in poweshell, particularly in the context of wanting to be able to run the main code remotely.

Is there a common pattern for putting a main block at the top of a powershell script? Powershell syntax and idioms are sufficiently different from more traditional languages, and even many popular modern scripting languages, that the pattern for moving the main logic to the top is not obvious (or maybe it is, but I just haven't found the right examples or documentation)

like image 601
Bryan Oakley Avatar asked Jan 08 '14 16:01

Bryan Oakley


People also ask

How do you create a main function in PowerShell?

The only way I know of to have the subroutines appear after the main function is to use a PowerShell module. When you import the module, all the functions defined by it are imported before any of the code is executed. So when you execute the main function all the subroutines are already defined.

Is there a main in PowerShell?

There is a function called "MAIN", but if you notice the author is calling the function main in the last line of the script.

How do you define a function in PowerShell script?

To define a function, you use the function keyword, followed by a descriptive, user-defined name, followed by a set of curly braces. Inside the curly braces is a scriptblock that you want PowerShell to execute.


1 Answers

If you want emulate the "traditional" structure in PowerShell, one way to do that is to use the Begin, Process and End keywords.

These can appear in any order in the script, but the Begin block will always run first, so you can put it at the bottom of your script, and put your function definitions there, and have your main logic at the top of the script in a Process or End block.

like image 107
mjolinor Avatar answered Oct 06 '22 02:10

mjolinor