PowerShell 模块 - 在导入模块时传递参数

2023-11-24

在下面的示例模块文件中,有没有办法在导入模块时传递 myvar 值。

例如,

import-module -name .\test.psm1 -?? pass a parameter? e.g value of myvar

#test.psm1
$script:myvar = "hi"
function Show-MyVar {Write-Host $script:myvar}
function Set-MyVar ($Value) {$script:myvar = $Value}
#end test.psm1

(此片段是从另一个问题复制的。)


这对我有用:

您可以使用–ArgumentList的参数import-module加载模块时传递参数的 cmdlet。

你应该使用param模块中的块来定义参数:

param(
    [parameter(Position=0,Mandatory=$false)][boolean]$BeQuiet=$true,
    [parameter(Position=1,Mandatory=$false)][string]$URL  
)

然后致电import-module像这样的 cmdlet:

import-module .\myModule.psm1 -ArgumentList $True,'http://www.microsoft.com'

正如您可能已经注意到的,您只能提供值(没有名称)–ArgumentList。所以你应该仔细定义你的参数position争论。

参考

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PowerShell 模块 - 在导入模块时传递参数 的相关文章

随机推荐