Gordon
Gordon

Reputation: 6873

Using Module and path

I am running into some issues with loading a PSM1 file. The PSM1 is always in the same folder as the PS1, but that folder can change. It works if I use a literal path like this...

Using module '\\Mac\iCloud Drive\Px Tools 4.#\Dev 4.0\#Spikes\Windows7\library.psm1'

But that's useless since the code could be installed anywhere. And the code is signed, so it can't change.

It also doesn't work to use the new (in PS3.0) $PSScriptRoot automatic variable...

Using module "$PSScriptRoot\library.psm1"

Nor does a relative path or simple file name, as in...

Using module ".\library.psm1"

or...

Using module "library.psm1"

What am I missing, other than perhaps it's time to call it a day?

Also, note that the library contains classes, so other module loading options like Import-Module don't work. This has me wondering if perhaps classes are better supported in a later version, and I should really be targeting PS 6.0, rather than 5.1?

Upvotes: 0

Views: 2726

Answers (2)

knurmia
knurmia

Reputation: 171

Looking for more info on your problem, I came around this blog post. It says relative paths work, so have you tried using relative path with single quotes or without quotes?

Since values inside double-quotes get evaluated before passing them to the cmdlet, using them might not work.

Upvotes: 1

Dragon Kao
Dragon Kao

Reputation: 1

you should run import-module first. in PS1, I have add code to call import-module like this:

ForEach($_ in  Get-ChildItem "$env:TEMP\*.psm1") {
    $checkModuleName = $_.Name -replace ".psm1"
    $importModule = $_.Name

    if (Get-Module $checkModuleName) {
        Write-Host "Update Module $importModule" -ForegroundColor Green

        Remove-Module $checkModuleName
        Import-Module "$env:TEMP\$importModule"
    }
    else {
        Write-Host "Import Module $importModule" -ForegroundColor Green

        Import-Module "$env:TEMP\$importModule"
    }
}

when import done, I can used all module.

Upvotes: 0

Related Questions