Reputation: 18187
I was told to write multiple actions using powershell script. Actions such as Apppool creation, SQL updation, File editing and etc.
I am going to write such a bulk thing in script first time.
So i would like to know the best practice before writting them.
Is it a good practice to write all the function in a single file?
I am thinking at least 10 functions i may need to write. Assuming each function may have 10 lines of code.
Upvotes: 3
Views: 6928
Reputation: 72650
Be pragmatic, truth come from action, no from words ;O)
So begin, by the beginining :
1) Does the thing you want to do exists somewhere on internet EX PoshCode (if so you can adapt it)
2) Think about your functions (not to much) object : reuse the code (write your algorith in pseudo code)
3) Use internet to look for the functions even existing
4) Wrote all functions in the same file as the main code to test them. During this phase you'll discover new functions and parameters to add or to remove from existing ones
5) Once you have tested your code, put the reusable functions (and the ones they depend on) into one or multiple module.
Upvotes: 1
Reputation: 60938
My solutions will be to create a custom Module where will be possible add function later.
You can save your single file with all functions as mymodule.psm1
in mymodule
folder under this path $env:psmodulepath
.
then add-module mymodule
(or better call it in you $profile
to have it ready when console is up)
Upvotes: 0
Reputation: 109080
Consider modules: the simplest format is a manifest (.psd1
) and a single script file (.psm1
) containing all the functions, aliases, ... the module exports (plus any internal helpers).
In this case you are clearly putting multiple connected functions in one file. Even if much of the code is only dot-sourced into the script module they are still logically in one entity.
On the other hand using scripts in your path to execute without having to load before hand would tend (as per Adriano's comment to the question) to support one function (at script scope rather than a function
statement) makes sense.
Therefore: there is no one "good practice": it all depends on the details of the circumstance.
Upvotes: 5