"Dot Sourcing" in PowerShell Scripts

(seth.orell) #1

I would like to include a common set of functions in a separate PowerShell file that I include in my NuGet package. Call it “InstallUtils.ps1.”

In my PreDeploy script, I’m assuming I can load (import) this script with a simple dot-source:
. .\InstallUtils.ps1
But when I call the methods in my file, I get a message that the function is not recognized.

Is the path for my dot-source correct? Or is the PreDeploy script run from another directory?

Thanks for the input,

-Seth

(Paul Stovell) #2

Hi Seth,

Your pre-deploy script gets run from the directory the package is extracted to - e.g.,:

C:\Octopus\Applications\Acme.Website.1.0.0.0

Even if the script is in a subfolder (e.g., Scripts\PreDeploy.ps1) the working folder will be the package root. You can verify this by adding “dir” to your script. A single . should be fine if the script is in the current directory.

However, from my experience you need to export functions to make them callable. I normally set up .psm1 files with the functions I need. An example is:

Build.psm1

function Build-Release ([string]$BuildNumber=1)
{
    & $msbuild "Tools\Build.proj" /p:build_number=$BuildNumber /t:BuildAndPublish /v:q
    Revert-Changes
}

Export-ModuleMember -function * -alias *

I then include it like this:

Import-Module .\Build.psm1

The functions should then be available for use.

Paul

(seth.orell) #3

I currently run a version of these scripts manually without problems (i.e., the dot sourcing works as I expected). I will look into exposing the functions in a .psm1 file and/or other ways to share methods.

I am still learning how best to work with PowerShell and appreciate all the guidance.

-Seth

(Paul Stovell) #4

Thanks Seth, I’ll investigate why the difference exists tonight.

Paul

(Paul Stovell) #5

Hi Seth,

I just pushed up a new build, which fixes dot-sourcing plus possibly some other PowerShell related issues. Scripts like this now work:

. .\MyFunctions.ps1

The build is 2047:

Paul

(seth.orell) #6

Paul,

Thank you - this is working in the latest build.

-Seth

(system) closed #7