【问题标题】:Run Powershell.exe from command line (cmd.exe) with -Command and -File switches?使用 -Command 和 -File 开关从命令行 (cmd.exe) 运行 Powershell.exe?
【发布时间】:2012-10-12 20:38:39
【问题描述】:

我有一个名为 MyModule.psm1 的简单模块定义为

function Show-Text($p)
{
    Write-Host $p
}

Export-ModuleMember Show-Text

模块保存在这里 - (一切正常,通过 ISE 或直接 Powershell 命令行的正常方式

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\MyModule (because 64-bit OS)

现在,我有一个像这样的名为 Test.ps1 的脚本

Show-Text "Hello World"

注意:我不想在 ps1 文件中执行 Import-Module MyModule

我正在从 cmd.exe 窗口运行以下命令

C:\>Powershell -Command "& {Import-Module MyModule}" -File "C:\temp\Test.ps1"

我收到以下错误

Import-Module : 未加载指定的模块“MyModule”,因为 在任何模块目录中都找不到有效的模块文件。在线:1 字符:17 + & {Import-Module ands.ImportModuleCommand

有人可以帮我如何通过 cmd.exe ONLY 执行上面的exactly what I have stated,而无需硬编码或指定完整的模块路径吗?

我们在加载 SystemModules 时所做的类似操作 - powershell.exe -ImportSystemModules

【问题讨论】:

  • 您运行的是 32 位 cmd.exe 吗?如果是这样,那么导入模块应该可以工作。
  • 睡眼惺忪需要一双新鲜的眼睛来指出错误。谢谢基思。如果你把它作为答案,我会接受。

标签: powershell powershell-2.0


【解决方案1】:

您是否正在运行 32 位 cmd.exe?如果是这样,那么 Import-Module 应该可以工作。如果您正在运行 64 位 cmd.exe,则将启动 64 位 PowerShell.exe,除非您完全限定 PowerShell.exe 的 32 位版本的路径(c:\windows\syswow64\windowspowershell\v1. 0\powershell.exe)。

【讨论】:

    【解决方案2】:

    你知道SysWOW64 是 32 位,System32 是 64 位,对吗?

    该模块存在于 32 位 Powershell 模块查找路径中,因此只能由 32 位 Powershell 控制台看到。如果您从 64 位 cmd 控制台调用 powershell,您将打开 64 位控制台。

    要么打开 32 位 cmd 提示符,要么将其移至 64 位模块路径,

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2022-11-09
      • 2017-04-11
      • 1970-01-01
      • 2012-10-13
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多