且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

点源“脚本"在函数内

更新时间:2023-12-04 21:54:16

正如其他人已经指出的那样,解决此问题的正确方法是将这些额外的功能放入 模块.在最简单的形式中,模块是 $env:PSModulePath 中列出的文件夹之一中的子文件夹,具有相同名称的 PowerShell 脚本(但扩展名为 .psm1.ps1):

As others have already pointed out, the proper way to go about this would be to put those extra functions into a module. In its simplest form a module is a subfolder in one of the folders listed in $env:PSModulePath with a PowerShell script of the same name (but with the extension .psm1 instead of .ps1):

$env:USERPROFILE
`-Documents
  `-WindowsPowerShell
    `-Modules
      `-ExtraFunctions
        `-ExtraFunctions.psm1

ExtraFunctions.psm1 包含您的所有函数,并以导出函数/别名/… 的 Export-ModuleMember 语句结束.您要发布:

ExtraFunctions.psm1 contains all your functions and ends with an Export-ModuleMember statement exporting the functions/aliases/… you want to publish:

function Get-Foo {
  ...
}

function New-Bar {
  ...
}

...

New-Alias -Name gf -Value Get-Foo
...

Export-ModuleMember -Function Get-Foo, New-Bar, ... -Alias gf, ...

这样你就可以导入特定的成员:

That way you can import specific members:

PS C:\> Import-Module ExtraFunctions -Function Get-Foo

或一次全部:

PS C:\> Import-Module ExtraFunctions

可以通过 Remove-Module cmdlet 卸载模块:

A module can be unloaded via the Remove-Module cmdlet:

PS C:\> Remove-Module ExtraFunctions

使用 PowerShell v3 和更新版本,您甚至不需要手动导入模块,因为模块是 在调用其导出的函数/cmdlet 之一时自动加载.

With PowerShell v3 and newer you don't even need to import your module manually, because modules are loaded automatically when one of their exported functions/cmdlets is called.

如果你想进行一些额外的工作,你可以添加一个 模块清单:

If you want to put in some extra work you can add a module manifest:

@{
  # Script module or binary module file associated with this manifest
  ModuleToProcess = 'ExtraFunctions.psm1'

  # Version number of this module.
  ModuleVersion = '1.0'

  # ID used to uniquely identify this module
  GUID = 'dbf5a7ca-683a-4f18-a090-0700ecccf6ff'

  # Author of this module
  Author = 'Ansgar Wiechers'

  # Company or vendor of this module
  CompanyName = ''

  # Copyright statement for this module
  Copyright = ''

  # Description of the functionality provided by this module
  Description = 'Extra functions.'

  # Minimum version of the Windows PowerShell engine required by this module
  PowerShellVersion = ''

  # Name of the Windows PowerShell host required by this module
  PowerShellHostName = ''

  # Minimum version of the Windows PowerShell host required by this module
  PowerShellHostVersion = ''

  # Minimum version of the .NET Framework required by this module
  DotNetFrameworkVersion = ''

  # Minimum version of the common language runtime (CLR) required by this
  # module
  CLRVersion = ''

  # Processor architecture (None, X86, Amd64, IA64) required by this module
  ProcessorArchitecture = ''

  # Modules that must be imported into the global environment prior to
  # importing this module
  RequiredModules = @()

  # Assemblies that must be loaded prior to importing this module
  RequiredAssemblies = @()

  # Script files (.ps1) that are run in the caller's environment prior to
  # importing this module
  ScriptsToProcess = @()

  # Type files (.ps1xml) to be loaded when importing this module
  TypesToProcess = @()

  # Format files (.ps1xml) to be loaded when importing this module
  FormatsToProcess = @()

  # Modules to import as nested modules of the module specified in
  # ModuleToProcess
  NestedModules = @()

  # Functions to export from this module
  FunctionsToExport = 'Get-Foo', 'New-Bar'

  # Cmdlets to export from this module
  CmdletsToExport = ''

  # Variables to export from this module
  VariablesToExport = ''

  # Aliases to export from this module
  AliasesToExport = 'gf'

  # List of all modules packaged with this module
  ModuleList = @()

  # List of all files packaged with this module
  FileList = 'ExtraFunctions.psm1'

  # Private data to pass to the module specified in ModuleToProcess
  PrivateData = ''
}

清单允许您定义依赖项,或将您的模块实现拆分为多个文件.它们可以手动或通过 创建新模块清单 cmdlet.将清单放入模块的根文件夹中:

Manifests allow you for instance to define dependencies, or to split your module implementation into multiple files. They can be created manually or via the New-ModuleManifest cmdlet. Put the manifest into the root folder of the module:

$env:USERPROFILE
`-Documents
  `-WindowsPowerShell
    `-Modules
      `-ExtraFunctions
        +-ExtraFunctions.psd1
        `-ExtraFunctions.psm1