点源“脚本"在函数内 [英] Dot Source a "script" within a Function

查看:82
本文介绍了点源“脚本"在函数内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 PowerShell 配置文件变得有点麻烦,而且我发现我并不总是使用其中的所有内容.我想减小我的配置文件的大小并加快启动时间越来越慢,但我仍然希望能够在需要时相对快速访问这些功能.

My PowerShell profile is getting a bit cumbersome and I find that I don't always use everything in it. I want to reduce the size of my profile and speed up the startup time that's been getting slower and slower, but I still want to be able to access those functions relatively quickly when I need them.

有没有办法从一个单独的函数中点源"一组 PowerShell 函数和别名,这样源函数将在该函数调用之外可用?

Is there a way to "Dot Source" a set of PowerShell Functions and Aliases from within a separate function such that the sourced functions will be available outside of that function call?

推荐答案

正如其他人已经指出的那样,解决此问题的正确方法是将这些额外的功能放入 模块.在最简单的形式中,模块是 $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

这篇关于点源“脚本"在函数内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆