Powershell无法识别模块

时间:2015-11-10 14:27:02

标签: ruby powershell vagrant

以下是我使用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事件相同的脚本时发生了同样的事情

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这几乎可以肯定是在-version之后意外放-command的结果,可能是这样的:

powershell -command -version 4.0 {import-module C:\windows\system32\windowspowershell\v1.0\Modules\SmbShare\SmbShare.psd1}

你没有确切地说出你在流浪汉中使用它的地点或方式,所以我不能肯定地说,但是检查它被执行的地方,看看你是否与订单不匹配。

Mathias对这条路是正确的;每个版本都是一样的,所以不用担心。

相关问题