English 中文(简体)
为什么在PowerShell,在试图在这一模块中援引一厘米子时自动进口安装的模块?
原标题:Why, in PowerShell, an installed module will be automatically imported when try to invoke a cmdlet in this module?

I m a novice in PowerShell. I have encountered a problem with importing modules. It seems that an installed module will be automatically imported when I try to invoke a cmdlet in this module.

That s is:

  1. If a module has been installed, such as Install-Module -Name PSscriptAnalyzer
  2. Do not import it. Check it has not been imported:
    Get-Module PSScriptAnalyzer
    # Show nothing
    
  3. Try to use a cmdlet in PSScriptAnalyzer:
    Invoke-ScriptAnalyzer -Path ./ -Recurse
    # Ignore any info
    
  4. Then the module PSScriptAnalyzer will be automatically imported:
    Get-Module PSScriptAnalyzer
    
    It shows as the follows:
    ModuleType Version    PreRelease Name                                
    ExportedCommands
    ---------- -------    ---------- ----                                ----------------
    Script     1.21.0                PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAnalyzer}
    

Why will the module be imported automatically as long as its ExportedCommands have been imported? What is the mechanism it is? And if so, the Remove-Module doesn t seem to make much sense in general usage scenarios anymore.

提前感谢。

问题回答

这个特点称为Module Auto-Loading < 并在PowerShell 3.0中介绍:

轮胎进口模块是你首次在安装的模块中进行任何指挥的。 现在,你可以使用模块中的指挥系统而无需安装或配置配置,因此没有必要在你安装电脑后管理单元。


如果是的话,<代码>Remove-Module在一般使用情景中似乎更有意义。

确实,很少使用圆柱子,Remove-Module仍然有其用途,主要是用于模块开发。 如果你需要测试对你的模块的改动而不需要重新开始会议,那么该圆顶点仍然是可行的(对于非组合单元来说,这只是现实的,因为你们必须重新开始会议,会议不能卸载。 <代码>Import-Module - Force也用于同一用途。

另一个使用案例也是少见的,是关于具有习惯的模块:OnRemove>。 见Using the OnRemove event





相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签