如何从PowerShell中的模块运行空间获得变量?
有一个具有“初始化”函数的模块,该函数设置了一个变量,该变量可在模块中的其他脚本/函数中使用,以验证运行初始化函数的运行。 在另一个脚本/函数中类似的事情
Start-InitializeThing
Connect to the API
$Script:SNOWinit = $true
将检查:
if ($Script:SNOWinit -eq $true) { Do the thing!}
是否有一种方法可以在同一PowerShell窗口中获取$脚本:Snowinit,但不是同一模块?
我想运行相同的检查,但是对于不在模块中的其他功能。
我可以做到吗,可以像模块runspace一样“挖掘”并检查该变量。我没有手段来编辑模块中的函数,因此一旦初始化脚本运行,我就无法更改设置的变量类型。
There is a module that has an "initialize" function that sets a variable that gets used in other scripts/functions in the module to validate that the initialize function was run. Something like
Start-InitializeThing
Connect to the API
$Script:SNOWinit = $true
Then in another script/function it will check:
if ($Script:SNOWinit -eq $true) { Do the thing!}
Is there a way to grab that $Script:SNOWinit in the same PowerShell window, but not the same module?
I want to run the same check but for a different function that is not in the module.
Can I do this, can I "dig" into like the modules runspace and check that variable. I don't have the means to edit the functions in the module so I cant change what type of variable is set once the initialize script has run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设感兴趣的模块被命名为
foo
和已经导入(已加载):如果要导入模块 on Demand :
以上返回
$ snowinit
变量在模块的根范围foo
中定义的变量。。
请注意,通常不建议使用此技术,因为它违反了模块提供的预期封装。在眼前的情况下,应视为 non-public 模块变量的
$ snowinit
,应视为实现细节,这就是为什么您不应该t依靠其在生产代码中的存在。Assuming that the module of interest is named
foo
and that it has already been imported (loaded):If you want to import the module on demand:
The above returns the value of the
$SNOWinit
variable defined in the root scope of modulefoo
.Note that it is generally not advisable to use this technique, because it violates the intended encapsulation that modules provide. In the case at hand,
$SNOWinit
, as a non-public module variable, should be considered an implementation detail, which is why you shouldn't rely on its presence in production code.来自圣经,WPIA。呼叫操作员的更神秘用途。
From the bible, WPiA. More mysterious uses for the call operator.