Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

Powershell: Installing Custom-Module with Install-Module

I have written a Powershell module. I would like to use this permanently. I understand that an Import-Module keeps the module in memory only during the session and is volatile.

It becomes persistent only by Install-Module. Unfortunately, Powershell expects a repository. I don't have that. Although I could manually copy the module to the module directory by determinating the path with $env:Path. But this is not nice.

Is there a "clean" way to install a custom module using Install-Module?

question from:https://stackoverflow.com/questions/65861078/powershell-installing-custom-module-with-install-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It becomes persistent only by Install-Module

This is only because Install-Module copies the module to one of the locations in $env:PSModulePath. It doesn't do anything clever to made the module persistent.

PowerShell reads the module manifests (.psd1 files) in those locations and any functions in the FunctionsToExport will be available.

When you try to use one of those functions, PowerShell will import that module.
You can test this out by running Get-Module before and after using a function from an unloaded module.
You can also test the fact that it has to import the module by adding a throw to the top of your .psm1 file - trying to use a function from that module will give you:

Custom-Command : The 'Custom-Command' command was found in the module 'Custom-Module', but the module could not be loaded. For more information, run 'Import-Module Custom-Module'.


Unfortunately, Powershell expects a repository. I don't have that

You can create one.

$customModulePath = 'C:TempModulesCustom-Module'
$repoPath = 'C:TempLocalRepo'
New-Item -Path $repoPath -ItemType Directory

$repoParameters = @{
    Name               = 'LocalRepo'
    SourceLocation     = $repoPath
    PublishLocation    = $repoPath
    InstallationPolicy = 'Trusted'
}
Register-PSRepository @repoParameters
Publish-Module -Path $customModulePath -Repository LocalRepo
Install-Module Custom-Module -Repository LocalRepo
  • Publish-Module will do some validation on your module and fail if it sees anything it doesn't like.
  • Publish-Module will create a zip of you module in $repoPath - you can open it using 7zip etc
  • Install-Module will install in the AllUsers scope by default if it can (running as admin) and CurrentUser if it can't. You can use the -Scope parameter to override this.
  • As detailed in Install-module documentation:
    AllUsers path: $env:ProgramFilesPowerShellModules
    CurrentUser path: $homeDocumentsPowerShellModules

Your module will be in one of the paths above, exactly as if you had copied it. The only difference will be an additional (hidden) PSGetModuleInfo.xml file. This has information related to the module package and repository.


Although I could manually copy the module to the module directory by determinating the path with $env:Path. But this is not nice.

I think this is a viable option, using one of the paths above, as that's the part of Install-Module that gives you persistence.

Another way is to modify your $profile to import the module at start or add your module directory to $env:PSModulePath. I use the second one so I can keep my modules in my repo with the rest of my code.

  • Import-Module 'C:TempModulesCustom-Module'
  • $env:PSModulePath += ';C:TempModules'

Is there a "clean" way to install a custom module using Install-Module?

If by "clean" you mean Install-Module 'C:TempModulesCustom-Module', then no - that's not what Install-Module is designed for.
If you're after module persistence, that's a yes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...