我的PowerShell配置文件变得有点麻烦,我发现我并不总是使用其中的所有内容。我希望缩小配置文件的大小并加快启动速度越来越慢的启动时间,但我仍然希望能够在需要时快速访问这些相对的功能
是否可以通过" Dot Source"一个单独的函数中的一组PowerShell函数和别名,以便源函数在该函数调用之外可用?
答案 0 :(得分:6)
正如其他人已经指出的那样,正确的方法是将这些额外的功能放入module。在最简单的形式中,模块是$env:PSModulePath
中列出的一个文件夹中的子文件夹,其中包含同名的PowerShell脚本(但扩展名为.psm1
而不是.ps1
):
$env:USERPROFILE
`-Documents
`-WindowsPowerShell
`-Modules
`-ExtraFunctions
`-ExtraFunctions.psm1
ExtraFunctions.psm1
包含您的所有函数,并以Export-ModuleMember
语句结束,导出要发布的函数/别名/ ...:
function Get-Foo {
...
}
function New-Bar {
...
}
...
New-Alias -Name gf -Value Get-Foo
...
Export-ModuleMember -Function Get-Foo, New-Bar, ... -Alias gf, ...
这样您可以导入特定成员:
PS C:\> Import-Module ExtraFunctions -Function Get-Foo
或一下子:
PS C:\> Import-Module ExtraFunctions
可以通过Remove-Module
cmdlet
PS C:\> Remove-Module ExtraFunctions
使用PowerShell v3及更新版本,您甚至不需要手动导入模块,因为当调用其中一个导出的函数/ cmdlet时,模块为loaded automatically。
如果您想加入一些额外的工作,可以添加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 = ''
}
清单允许您定义依赖项,或将模块实现拆分为多个文件。可以手动创建它们,也可以通过New-ModuleManifest
cmdlet创建它们。将清单放入模块的根文件夹中:
$env:USERPROFILE
`-Documents
`-WindowsPowerShell
`-Modules
`-ExtraFunctions
+-ExtraFunctions.psd1
`-ExtraFunctions.psm1
答案 1 :(得分:5)
经过大量的努力(以确保我以后不会失败),这就是我发现的作品。
我已将以下功能放在我的个人资料中:
function extras {. C:\...\WindowsPowerShell\extrafunctions.ps1}
然后我" Dot Source" PowerShell窗口中的函数。
. extras
并且我的所有额外功能都可用于会话,而不会减慢启动时间。