powershellmodulescopedot-source

Splitting a large PowerShell script using dot-source does not work


I have written a very large script that does different parts of a larger process.

Now I wanted to seperate these parts to different scripts so I moved each part of the logic to a different ps1 files.

I created: Basic_Functions.ps1 > this contains some functions I use in all the different parts Part1.ps1 > This is the first part of the script Part2.ps1 > The second part should just start after the first part ...

All Part* Scripts use functions defined in Basic_Functions.ps1.

I run the script from the task scheduler and to be sure that the different parts are running in the correct order I created a Runner.ps1 where the code is:

. ".\Basic_Functions.ps1"
. ".\Part1.ps1"
. ".\Part2.ps1"

But then I get an error from Part1.ps1 that the functions defined in Basic_Functions.ps1 do not exist.

When I simple start a PowerShell Session and do . ".\Basic_Functions.ps1" I can use the methods defined and everything works just fine.

Can someone point me into the right direction? Thank you.


Solution

  • I thought about the answer from @Start-Automating but this would use the same technical method and I did not understand why it does not work in my simple example.

    Then I tried to do the my example again and it worked and I was wondering what happened.

    Then I checked my original sample and found out that I used a function to shorten some things like this:

    function Run-Module ($Modulename){
         . ("path-to-module\"+$modulename+".ps1")
    }
    

    But of cause this does not make sense as these module functions would only be available in this lokal function scope and the other modules cant access them as it would never became a global function!