Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 封装Powershell函数_Function_Powershell_Scoping - Fatal编程技术网

Function 封装Powershell函数

Function 封装Powershell函数,function,powershell,scoping,Function,Powershell,Scoping,我不知道如何执行下面的任务。与OOP中的类成员一样,我们可以使用私有修饰符隐藏实现。我的目标是创建一个基本powershell函数,该函数包含多个函数用于代码重用的逻辑,同时将该函数隐藏在全局访问之外。根据以下引用,以下作用域可用于全局、脚本和私有。我对函数的标记不能产生期望的结果。ecapsulated功能应如下所示工作 function Invoke-VMDoSomething { Invoke-PrivateMiniFunc } function Invoke-VMDoSomet

我不知道如何执行下面的任务。与OOP中的类成员一样,我们可以使用私有修饰符隐藏实现。我的目标是创建一个基本powershell函数,该函数包含多个函数用于代码重用的逻辑,同时将该函数隐藏在全局访问之外。根据以下引用,以下作用域可用于全局、脚本和私有。我对函数的标记不能产生期望的结果。ecapsulated功能应如下所示工作

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

最后一个命令Export ModuleMember定义了您希望公开哪些函数

接下来,在另一个文件中导入该模块

其中,只有导出的函数可见/可调用,但Invoke PrivateMiniFunc不可见:

结果:

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作为示例。根据文档,您需要指定函数是全局的-函数全局:InvokePrivateMinifunc{ReusableCode}不工作吗?所有函数都使用全局范围修饰符工作。但是,根据我的设计规范,我希望Global:InvokePrivateMinifunc{ReusableCode}只被驻留在模块中的函数看到,因此需要它不是全局的。作用域可能不是隐藏InvokePrivateMinifunc的效果的解决方案,因此它只能封装到模块中其他函数的知识中。正如假设的命令提示符中的第三行所述,它必须失败。你是说我应该只调用VMDoSomething并调用VMDoSomethingElse全局?谢谢你回答这个问题。这是一个有点冗长的步骤…但它是有效的!我认为这个惯例在未来的PS版本中应该更干净。对于代码维护非常有用。
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