0
votes

I'm new to powershell.I'm facing an error while working on an existing script. In C:/Scripts I have 3 files . One .ps1 file and 2 .psm1 file.

ps1 file Name : RunScript.ps1
This imports .psm1 files like below.

$modules = @(
    "$PsscriptRoot\Modules\Module1.psm1",
    "$PsscriptRoot\Modules\Module2.psm1"
)

foreach ($module in $modules) {
    if (-not $(Get-Module $module)) {
        Import-Module $module -Force -WarningAction SilentlyContinue
    }
}

This above code is giving me error as below

Get-Module : Running the Get-Module cmdlet without ListAvailable parameter is not supported for module names that include a path. Name parameter has this element 'C:\Scripts\Modules\Module1.psm1' which resolves to a path. Update the Name parameter to not have path elements, and then try again.

It is expecting to not give path of the file. But how do I do Get-Module in this scenario. Any help is very helpful.Thanks.

1
Is the file name the same as the module name?guiwhatsthat
@guiwhatsthat yesCrazyCoder
So you could check just with the filename. Something like that: Get-Module -Name (([System.IO.FileInfo]"$Path").BaseName)guiwhatsthat

1 Answers

0
votes

Get-Module returns modules imported into the current session or modules installed that are available for you to import. You should install your module then it will be available with the get-module cmdlet by using the name of your module.

I'm not quite sure what you're trying to do with your script? You only want to import it if its not already imported?

I would have added this as a comment but I cant yet...