PowerShell:脚本模块变量在 ISE 之外不可用 [英] PowerShell: Script Module variables not available outside of ISE

查看:67
本文介绍了PowerShell:脚本模块变量在 ISE 之外不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力更新我编写的一些脚本.我仍在学习 PowerShell,所以我喜欢回顾我的旧版本,看看我是否可以改进.
我偶然发现了模块,非常喜欢使用它们来存储可以随时调用的不同操作的想法.

I have been working on updating some of the scripts I have written. I am still learning PowerShell so I like to go back over my old ones and see if I can improve things.
I stumbled upon Modules and really like the idea of using them to store different actions that can then be called on at anytime.

我写了一个简单的脚本来测试(或者我是这么认为的).我应该提到我将我的模块存储在服务器共享上.

I wrote a simple script to test (or so I thought). I should mention I am storing my modules on a server share.

Write-Host "Testing for available variables"
Write-Host "Loading Modules"
Import-Module <path to my module on server share>.psm1
Write-Host ""
Write-Host "Done"
Write-Host "Print loaded modules"
Get-Module
Write-Host ""
Write-Host "Done"
Write-Host "================="
Write-Host ""
Write-Host "Printing variables"
Write-Host ""
Write-Host "Variable: arch: " $arch
Write-Host "Variable: WowNode: " $WowNode
Write-Host ""
Write-Host "Done"

我的模块包含

If ( "${env:ProgramFiles(x86)}" -ne "" )
 {
    $script:arch = 64
    $script:WowNode = "\Wow6432Node\"
 }
 ELSE
 {
    $script:arch = 32
    $script:WowNode = "\"
 }

当我使用 Windows Powershell ISE 运行我的脚本时,一切都按照我的想法运行.我看到变量的内容并且它们正确打印出来.
当我从 Powershell 命令行窗口运行我的脚本时,模块被加载,但变量是空白的(没有打印出来).
我一直在搜索 Google,我看到的唯一可能导致此问题的原因与我用来定义变量的范围有关.
将它们定义为 $script:xx 会将它们放入全局范围(我很确定),这使得它们可以在整个脚本中使用.所以我认为我正确地定义了我的变量.但我仍然没有任何运气.

When I run my script using Windows Powershell ISE everything functions how I thought it would. I see the contents of the variables and they print out correctly.
When I run my script from a Powershell Command Line window, the module gets loaded, but the variables are blank (nothing prints out).
I have been digging around Google and the only thing I saw that could maybe be causing this was related to the scope I am using to define my variables.
Defining them as $script:xx puts them into the Global scope (I'm pretty sure) which makes them available throughout your script. So I think I am defining my variables correctly. But I still haven't had any luck.

我可能遗漏了一些明显的东西,但我一生都找不到它.
关于我可以采取哪些措施来解决此问题的任何建议?
我确认我正在运行 PowerShell 2.0 版,因为我读到模块仅在 2.0 版中可用.

I am probably missing something obvious, but for the life of me I can't find it.
Any suggestions on what I can do to troubleshoot this issue?
I have confirmed I am running version 2.0 of PowerShell since I read that Modules are only available in version 2.0.

推荐答案

从我现在能想到的(没有测试),问题是你说的范围.在 ISE 和控制台中运行脚本之间存在差异.我猜当你在 ISE 中运行脚本时,你打开脚本并按下运行按钮.这将在控制台中逐行执行脚本,就像您直接在控制台中键入它们一样.这使得脚本本身在 global(会话)变量范围内运行.由于脚本在 global 中,您的模块将是唯一一个在 script 范围内运行的模块.

From what I can think of right now(without testing), the problem is the scope as you say. There is a difference between running the script in ISE and in a console. I'm guessing when you're running the script in ISE, that you open the script and press the run button. This will execute the script line by line in the console, AS IF you were typing them directly into the console. This makes the script itself run in the global (session) variable scope. Since the script was in global, your module will be the only one running in script scope.

但是,当您在 powershell 控制台中运行脚本时;喜欢:

However, when you run the script in a powershell console; like:

PS > .\myscript.ps1

然后您的脚本在 script 范围内运行(脚本完成后,每个变量都会被删除).现在,模块将在它自己的 script 范围内运行,低于您的主脚本运行的范围.当模块导入完成后,模块的 script 范围将被删除,并且您的主脚本将在它自己的范围内查找,您的变量不存在.

Then your script is run in the script scope(every variable is deleted when the script is done). Now, the module will run in it's own script scope, BELOW the scope that your mainscript runs in. When the module is finished with being imported, the module's script scope is deleted, and your main script will look in it's own scope, where you variables do not exist.

Global (session)
 - Script (mainscript)
   - Script (module)
     #Module completes, and scope is deleted
   #You look for $script:arch , which was actually deleted right above.
   #Powershell searches through the mainscript's scope, but finds nothing.

要解决此问题,请尝试将值存储在 global(会话)范围内,例如 $global:arch.指定 global 作为创建变量(必需)和读取变量(推荐)时的作用域.

To solve this, try storing the values in the global (session) scope, like this $global:arch. Specify global as the scope when you create the variable(required), and when you read from it(recommended).

这篇关于PowerShell:脚本模块变量在 ISE 之外不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆