Best Practice For Splitting Files

This topic contains 3 replies, has 3 voices, and was last updated by  Duffney 1 week, 3 days ago.

  • Author
    Posts
  • #79633

    Colin
    Participant

    I currently have a script that is reaching 500 lines and I'm barely half way through creating it.

    What are the recommended practices for handling large scripts. Do you split them? If so, how do you handle the execution of them, do you just 'dot-source' them? For modules, do you do use a .psd with nested modules?

    The same question goes for handling large pester scripts as those aren't complex but they have a lot of lists of things to check and code blocks so can get lengthy quickly

  • #79634

    Don Jones
    Keymaster

    This is something we cover in "Learn PowerShell Toolmaking in a Month of Lunches." Essentially, you want to break down your functionality into discrete functions, with a controller script that calls them all in the right order and combination. The functions go in a .psm1 script module. This does require you to really think about what a "task" consists of, and make each function a very discrete, small-as-possible "task." This makes development and debugging easier, as well as maintenance.

  • #79637

    Colin
    Participant

    Sorry I should have clarified. My script is actually a module in a single file. It has a large class created at the top followed by several advanced functions. Once you add in all the help comments etc, it is getting quite large

  • #79649

    Duffney
    Participant

    This might be overkill for what you're looking to do, but I follow a similar format for module development.

    https://xainey.github.io/2017/powershell-module-pipeline/

You must be logged in to reply to this topic.