在编写powershell模块时(在powershell中),如何在导入模块时运行命令?
当用户运行import-module mymodule时,我希望模块首先运行一些命令来完成一些初始设置。我该怎么做?
发布于 2016-04-26 18:10:02
如果您有一个PowerShell *.psm1模块,您还应该指定一个模块清单(*.psd1)。在这个清单中,您可以在加载模块时指定一个PowerShell脚本(*.ps1*)数组来调用:
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
ScriptsToProcess = @()发布于 2016-04-26 18:12:12
您可以执行前面提到的ScriptsToProcess操作,但也可以将代码直接放入.psm1文件(函数定义之外)。所有这些都是在导入模块时执行的,因此不需要使用单独的文件。(在执行output代码时写入标准输出的任何内容通常都会被丢弃。)
发布于 2016-04-26 18:10:03
实现这一目标的一种方法是在模块准备就绪时创建模块清单。您可以通过以下cmdlet实现这一点
New-ModuleManifest查询更多详细信息:
Get-Help New-ModuleManifest你要找的选择是
-ScriptsToProcess <String[]>
Specifies script (.ps1) files that run in the caller's session state when the module is imported. You can use these scripts to prepare an environment, just as you might use a login script.
Required? false
Position? named
Default value None
Accept pipeline input? false
Accept wildcard characters? falsehttps://stackoverflow.com/questions/36872389
复制相似问题