封装 Powershell 函数
Encapsulating Powershell Functions
我不知道如何执行以下任务。与 OOP 中的 class 成员一样,我们可以使用 private 修饰符隐藏实现。我的目标是创建一个基本的 powershell 函数,其中包含多个函数用于代码重用的逻辑,同时隐藏该函数以防止全局访问。根据以下参考 https://ss64.com/ps/syntax-scopes.html ,以下范围可用于全局、脚本和私有。我对函数的标记没有产生预期的结果。封装函数应如下所示工作。
function Invoke-VMDoSomething {
Invoke-PrivateMiniFunc
}
function Invoke-VMDoSomethingElse {
Invoke-PrivateMiniFunc
}
function Invoke-PrivateMiniFunc {
###BaseReuseable code
}
假设的命令提示符
PS > Invoke-VMDoSomething <<<Invoke-PrivateMiniFunc Executes successfully
PS > Invoke-VMDoSomethingElse <<<Invoke-PrivateMiniFunc Executes successfully
PS > Invoke-PrivateMiniFunc <<<Fails Cannot find Such Commandlet -- Desired result.
如何实现此约定,我是否需要将函数存储在 .psm1 文件中而不是 ps1 文件中?有可能吗?
也许不完全是您想要的,但您可以将函数隐藏在模块中。
对于您的情况,创建一个新文件并将其另存为 *.psm1(为了演示,我将其命名为 InvokeModule.psm1
)
function Invoke-VMDoSomething {
Invoke-PrivateMiniFunc
}
function Invoke-VMDoSomethingElse {
Invoke-PrivateMiniFunc
}
function Invoke-PrivateMiniFunc {
Write-Host "Called by: $((Get-PSCallStack)[1].FunctionName)"
}
# export the functions you want to make available and leave out
# the functions you want to keep hidden (but available to the functions in the module)
Export-ModuleMember -Function Invoke-VMDoSomething, Invoke-VMDoSomethingElse
最后一个命令 Export-ModuleMember
定义了您想要公开的功能和不公开的功能。
接下来,在另一个文件中导入该模块。
在那里,只有导出函数是 visible/callable 而 Invoke-PrivateMiniFunc
不是:
Import-Module 'D:\InvokeModule.psm1'
Invoke-VMDoSomething # works as expected
Invoke-VMDoSomethingElse # works as expected
Invoke-PrivateMiniFunc # errors out
结果:
Called by: Invoke-VMDoSomething
Called by: Invoke-VMDoSomethingElse
Invoke-PrivateMiniFunc : The term 'Invoke-PrivateMiniFunc' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:7 char:1
+ Invoke-PrivateMiniFunc
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-PrivateMiniFunc:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我不知道如何执行以下任务。与 OOP 中的 class 成员一样,我们可以使用 private 修饰符隐藏实现。我的目标是创建一个基本的 powershell 函数,其中包含多个函数用于代码重用的逻辑,同时隐藏该函数以防止全局访问。根据以下参考 https://ss64.com/ps/syntax-scopes.html ,以下范围可用于全局、脚本和私有。我对函数的标记没有产生预期的结果。封装函数应如下所示工作。
function Invoke-VMDoSomething {
Invoke-PrivateMiniFunc
}
function Invoke-VMDoSomethingElse {
Invoke-PrivateMiniFunc
}
function Invoke-PrivateMiniFunc {
###BaseReuseable code
}
假设的命令提示符
PS > Invoke-VMDoSomething <<<Invoke-PrivateMiniFunc Executes successfully
PS > Invoke-VMDoSomethingElse <<<Invoke-PrivateMiniFunc Executes successfully
PS > Invoke-PrivateMiniFunc <<<Fails Cannot find Such Commandlet -- Desired result.
如何实现此约定,我是否需要将函数存储在 .psm1 文件中而不是 ps1 文件中?有可能吗?
也许不完全是您想要的,但您可以将函数隐藏在模块中。
对于您的情况,创建一个新文件并将其另存为 *.psm1(为了演示,我将其命名为 InvokeModule.psm1
)
function Invoke-VMDoSomething {
Invoke-PrivateMiniFunc
}
function Invoke-VMDoSomethingElse {
Invoke-PrivateMiniFunc
}
function Invoke-PrivateMiniFunc {
Write-Host "Called by: $((Get-PSCallStack)[1].FunctionName)"
}
# export the functions you want to make available and leave out
# the functions you want to keep hidden (but available to the functions in the module)
Export-ModuleMember -Function Invoke-VMDoSomething, Invoke-VMDoSomethingElse
最后一个命令 Export-ModuleMember
定义了您想要公开的功能和不公开的功能。
接下来,在另一个文件中导入该模块。
在那里,只有导出函数是 visible/callable 而 Invoke-PrivateMiniFunc
不是:
Import-Module 'D:\InvokeModule.psm1'
Invoke-VMDoSomething # works as expected
Invoke-VMDoSomethingElse # works as expected
Invoke-PrivateMiniFunc # errors out
结果:
Called by: Invoke-VMDoSomething
Called by: Invoke-VMDoSomethingElse
Invoke-PrivateMiniFunc : The term 'Invoke-PrivateMiniFunc' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:7 char:1
+ Invoke-PrivateMiniFunc
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-PrivateMiniFunc:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException