简体   繁体   English

我可以在Powershell ps1脚本中隐藏内部帮助器方法吗?

[英]Can I hide internal helper methods in a Powershell ps1 script?

For example, one (bad) idea I had would be to import a module from a string defined in the file (I don't know if I can do this without first writing this string to the file system) 例如,我的一个(坏)主意是从文件中定义的字符串导入模块(我不知道是否可以不先将这个字符串写入文件系统就可以这样做)。

I'm writing a script that I want to be able to execute remotely, and following this answer I'm using the "Scripts Approach". 我正在编写一个希望能够远程执行的脚本,然后按照此答案使用“脚本方法”。 However, I want to hide some of the functions somehow - I'd like a sort of module within a script. 但是,我想以某种方式隐藏一些功能-我想要脚本中的某种模块。

Or is there a completely different approach to what I'm trying to do? 还是我要尝试的方法完全不同? All my scripts/modules are client side, and I want to run them in a remote PSSession. 我所有的脚本/模块都是客户端,我想在远程PSSession中运行它们。 I don't particularly want to handle copying of scripts to the remote server or shared drive. 我特别不想处理将脚本复制到远程服务器或共享驱动器的操作。

I am using the following to dot source my script into a remote PSSession: 我正在使用以下命令将脚本源代码添加到远程PSSession中:

invoke-command -Session $s -FilePath $p

and then invoking a scriptblock making use of the functions defined in the script file $p . 然后使用脚本文件$p定义的功能调用脚本块。 This works, but I don't think this works with modules, and if I want to reuse code in other scripts, I either have to manually import each one into the remote session, or duplicate code in a single monolithic script. 这行得通,但我认为这不适用于模块,并且如果我想在其他脚本中重用代码,则必须手动将每个脚本导入远程会话,或者在一个整体脚本中复制代码。 Neither is appealing, and neither seems to allow hiding of "private" methods. 两者都没有吸引力,而且似乎都不允许隐藏“私有”方法。

You can use New-Module cmdlet: 您可以使用New-Module cmdlet:

New-Module {
    function a {
        b
    }
    function b {
        'function b called'
    }
    Export-ModuleMember a
}|Out-Null
a #OK
b #Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM