PowerShell Import-Module vs Dot Sourcing

前端 未结 3 1201
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 02:38

If I want to separate out some of my functionality from my main PowerShell script, I can either write that as a .ps1 file and dot source the same or I can create th

3条回答
  •  天命终不由人
    2021-02-01 03:12

    A few module features in addition to other answers.

    • In PowerShell V3 one does not have to call Import-Module in order to use module's exported commands. This is especially useful when commands are used interactively. PowerShell somehow caches and knows all available module commands and even their help Get-Help SomeCommand (this is not true for module help Get-Help about_SomeModule, though).

    • There are several subtle differences in behavior of dot-sourced functions and script module functions. It is not easy to list them all, here is just one example: Strange behavior with Powershell scriptblock variable scope and modules, any suggestions? Sometimes using script modules gets painful, especially when one discovers unwanted differences and issues too late, i.e. simple things work fine in the beginning of development but complex things start to go wrong later.

    All in all, normally I use modules except cases when they do not work well. One of them is invoking user script blocks passed in script module functions.

提交回复
热议问题