"Dot Sourcing" in PowerShell Scripts

seth.orell's Avatar

seth.orell

12 Oct, 2011 10:52 PM

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

  1. Support Staff 1 Posted by Paul Stovell on 12 Oct, 2011 10:58 PM

    Paul Stovell's Avatar

    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

  2. 2 Posted by seth.orell on 13 Oct, 2011 01:09 PM

    seth.orell's Avatar

    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

  3. Support Staff 3 Posted by Paul Stovell on 13 Oct, 2011 04:11 PM

    Paul Stovell's Avatar

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

    Paul

  4. Support Staff 4 Posted by Paul Stovell on 13 Oct, 2011 09:08 PM

    Paul Stovell's Avatar

    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:

    http://octopusdeploy.com/Download

    Paul

  5. 5 Posted by seth.orell on 14 Oct, 2011 02:57 PM

    seth.orell's Avatar

    Paul,

    Thank you - this is working in the latest build.

    -Seth

  6. seth.orell closed this discussion on 14 Oct, 2011 02:57 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac