【问题标题】:Powershell does not recognize modulePowershell 无法识别模块
【发布时间】: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


【解决方案1】:

这几乎可以肯定是不小心将-version 放在-command 之后的结果,可能是这样的:

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

您并没有确切地说明您从 vagrant 那里或如何使用它,所以我不能肯定地说,但请检查执行它的位置,看看您是否与订单不匹配。

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

【讨论】:

  • 不幸的是,情况并非如此,在所有场景中我都使用了powershell -version 4.0 -command {import-module C:\windows\system32\windowspowershell\v1.0\Modules\SmbShare\SmbShare.psd1}
  • 在 vagrant 中我使用的是`config.trigger.before :up do provider = ['hyperv'] run "powershell -version 4.0 -file 'PsTest.ps1'" `
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2021-11-01
  • 2016-07-08
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
相关资源
最近更新 更多