打破全局、脚本的 Powershell 命名空间限制 [英] Breaking the Powershell namespace limit of global,script
问题描述
如何在脚本中创建命名空间或提供程序",而无需编写任何 C# 或使用 Windows Box 中尚未包含的任何内容?- 或者,我可以通过我点源它们的脚本名称来引用脚本中的变量吗?我想避免在点采购生成的 PoSH 脚本时发生冲突.Set-Variable cmdlet 没有提供有关下一步该往何处看的任何线索,是否可能有一个 cmdlet 来创建为您完成所有框架的提供程序?目前我正在使用全局"(ick)变量列表来跟踪我定义的所有值,基本上我正在尝试构建一个库,在这种情况下,该库提供数据值/常量.
How can I create namespaces or "providers" in a script without writing any C#, or using anything not already inside the Windows Box? - Alternately put, can I reference variables in a script by the name of the script I dot-sourced them from? I want to avoid clashes when dot sourcing a generated PoSH script. Set-Variable cmdlet does not give any clues as to where to look next, is there perhaps a cmdlet to create providers with all the framework done for you? At the moment I am using a "global" (ick) variable-list to track all values I define, basically I am trying to build a library, and in this case, the library supplies data values/constants.
推荐答案
您可以在被调用的脚本中检查 $myinvocation 以查找脚本名称.
You can check $myinvocation within the called script to find the script name.
听起来您可以在主脚本中实现一种伪命名空间,方法是将变量列表设为哈希表的哈希表,然后每个值将在主脚本中作为 $vars.scriptname.scriptvariable 引用.
It sounds like you could implement a kind of pseudo-namespace in the main script by making your variable list a hash table of hash tables, and then each value would be referenced in the main script as $vars.scriptname.scriptvariable.
这篇关于打破全局、脚本的 Powershell 命名空间限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!