powershell模块能否调用其导入器中的函数';什么范围?
powershell模块是否可以调用其导入程序范围内的函数 例如,假设我有module.psm1和script.ps1。在script.ps1内部,我导入module.psm1,并定义一个名为executepayload的函数。module.psm1中的代码是否可以调用执行有效负载?从模块中的(高级)函数内部,您可以使用参数powershell模块能否调用其导入器中的函数';什么范围?,powershell,module,Powershell,Module,powershell模块是否可以调用其导入程序范围内的函数 例如,假设我有module.psm1和script.ps1。在script.ps1内部,我导入module.psm1,并定义一个名为executepayload的函数。module.psm1中的代码是否可以调用执行有效负载?从模块中的(高级)函数内部,您可以使用参数$PSCmdlet.SessionState和从字符串创建的脚本块来执行调用方作用域中的任意代码 这项技术很感激地改编自 为简洁起见,以下代码演示了使用创建的动态模块的技术,
$PSCmdlet.SessionState
和从字符串创建的脚本块来执行调用方作用域中的任意代码
这项技术很感激地改编自
为简洁起见,以下代码演示了使用创建的动态模块的技术,但同样适用于常规的持久化模块:
#从模块调用的函数。
函数执行有效负载{
“在脚本范围内执行有效负载。”
}
#创建(并隐式导入)动态模块。
$null=新模块{
#定义调用“执行有效负载”的高级模块函数`
#在调用方的范围内。
函数调用某物{
[CmdletBinding()]
param()
$PSCmdlet.InvokeCommand.InvokeScript(
$PSCmdlet.SessionState,
#将要在调用方作用域中执行的带有任意代码的*字符串*传递给
#[脚本块]::创建()。
#!!必须使用[scriptblock]::Create()和
#!!作为*string*,因为它创建了一个*unbound*脚本块
#!!在指定会话中运行。
#!!如果需要,使用字符串扩展“烘焙”模块局部变量
#!!将值输入字符串,或将参数作为附加参数传递。
[scriptblock]::创建('Execute Payload')
)
}
}
#现在调用模块中定义的函数。
援引某物
上面的输出
在脚本的作用域内执行有效负载。
,证明脚本的函数已被调用。如果我正确理解了您试图执行的操作,有两种常用方法可以将自定义函数加载到主脚本中。我会给你举几个例子,因为,我不确定是否有一个最好的做法
在所有给定的示例上,执行的脚本始终是main.ps1
- 示例1:所有函数都存储在一个文件中
Functions.ps1
<代码>主.ps1
在第一行代码中,您可以添加如下内容:
$ErrorActionPreference = 'Stop'
# Option 1: Using Import-Module.
try
{
Import-Module "$PSScriptRoot\Functions\functions.ps1"
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
# Option 2: Dot Sourcing
try
{
. "$PSScriptRoot\Functions\functions.ps1"
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
$ErrorActionPreference = 'Stop'
# Option 1: Using Import-Module.
try
{
Get-ChildItem "$PSScriptRoot\Functions\*.ps1" | Import-Module
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
# Option 2: Dot Sourcing
try
{
Get-ChildItem "$PSScriptRoot\Functions\*.ps1" | ForEach-Object {
. $_.FullName
}
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
注意:两个选项都将加载所有功能
- 示例2:许多函数存储在不同的文件中。当您有许多复杂和/或冗长的函数时,可以使用此选项
myCustomFunction1.ps1
myCustomFunction2.ps1
myCustomFunction3.ps1
main.ps1
在第一行代码中,您可以添加如下内容:
$ErrorActionPreference = 'Stop'
# Option 1: Using Import-Module.
try
{
Import-Module "$PSScriptRoot\Functions\functions.ps1"
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
# Option 2: Dot Sourcing
try
{
. "$PSScriptRoot\Functions\functions.ps1"
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
$ErrorActionPreference = 'Stop'
# Option 1: Using Import-Module.
try
{
Get-ChildItem "$PSScriptRoot\Functions\*.ps1" | Import-Module
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
# Option 2: Dot Sourcing
try
{
Get-ChildItem "$PSScriptRoot\Functions\*.ps1" | ForEach-Object {
. $_.FullName
}
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
你想把保存在一个脚本上的函数加载到你的主脚本中吗?是的,但是理想的是,我希望在不创建命名空间污染的情况下这样做。如果你的问题还没有完全解决,请考虑回答或提供反馈。
function myCustomFunction1 {
....
}
function myCustomFunction2 {
....
}
function myCustomFunction3 {
....
}
$ErrorActionPreference = 'Stop'
# Option 1: Using Import-Module.
try
{
Get-ChildItem "$PSScriptRoot\Functions\*.ps1" | Import-Module
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}
# Option 2: Dot Sourcing
try
{
Get-ChildItem "$PSScriptRoot\Functions\*.ps1" | ForEach-Object {
. $_.FullName
}
}
catch
{
"Failed to load dependency Functions.`nError: $_"
}