【发布时间】:2015-11-10 14:27:01
【问题描述】:
以下是我在使用 Vagrant 1.7.2、Ruby 和 Powershell 时遇到的问题,但我认为是使用 Powershell 版本时的普遍问题。
当我从 Cmd 或 Poweshell 控制台或 ISE 运行此命令时,它可以工作
powershell -version 4.0 -command {import-module C:\windows\system32\windowspowershell\v1.0\Modules\SmbShare\SmbShare.psd1}
当我在 ruby IRB 中运行它时,它也可以工作
system('powershell -version 4.0 -noprofile -file PsTest.ps1')
不过,当我在使用 Ruby 的 Vagrant 中运行它时
-version : The term '-version' 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:1 char:1
如果我取出 -version 参数,我会得到
import-module : The specified module 'C:\windows\system32\windowspowershell\v1.0\Modules\SmbShare\SmbShare.
psd1'
我认为它是在尝试使用 powershell 1.0,但在脚本中我使用了 $PSVersionTable 并且所有情况下我都得到了相同的结果
Key : PSVersion
Value : 4.0
Key : WSManStackVersion
Value : 3.0
Key : SerializationVersion
Value : 1.1.0.1
Key : CLRVersion
Value : 4.0.30319.42000
Key : BuildVersion
Value : 6.3.9600.17400
Key : PSCompatibleVersions
Value : {1.0, 2.0, 3.0, 4.0}
Key : PSRemotingProtocolVersion
Value : 2.2
也在我包含的脚本中
( get-process -id $pid ).Path
#In all cases (even the one that fails ) it returns
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
不确定不允许从 3.0 版开始加载模块的执行之间有什么不同,这就是我猜它正在运行 Powershell 1.0 虽然 PSVersionTable 清楚地表明它是 4.0
刚刚注意到,当我在 Visual Studio 2013 Update 4 项目中使用与 PreBuild Event 相同的脚本时,发生了同样的情况
有什么想法吗?
【问题讨论】:
-
C:\windows\system32\windowspowershell\v1.0是 所有 版本的 powershell 安装主目录,不要让v1.0文件夹名称欺骗您。 -
正如您提到的使用 (get-process -id $pid).Path 我看到所有的 powershell 执行都指向 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-
你能告诉我们来自 VagrantFile 的适当的 sn-p 吗?去年,我向 Vagrant 添加了对配置器的 shell 参数的支持。见:github.com/mitchellh/vagrant/pull/4548
-
在这些情况下可能出现的另一个问题是父进程的上下文。检查子 powershell 中的
[Environment]::Is64BitProcess以查看其中一个是否在错误的上下文中运行它。
标签: ruby powershell vagrant