【问题标题】:Using Batch to run remote PowerShell V1.0 command使用 Batch 运行远程 PowerShell V1.0 命令
【发布时间】:2013-03-19 13:26:31
【问题描述】:

我正在尝试从我的 Hyper-V 主机收集一些信息。我有一大堆,想自动化这个过程。 我需要获取在每个主机上运行的虚拟机。 我想从批处理脚本中做到这一点。 当我在 PowerShell V1.0 窗口(在 Hyper-V 主机上)中运行此命令时,它可以工作并为我提供必要的信息:

get-vmmemory | select VMelementName,reservation | out-file c:\Output.txt

这就是我从批处理脚本运行它的方式:

\\<RemoteMachine>\c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command get-vmmemory >>aa.txt

这是我得到的输出

The term 'get-vmmemory' 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:16
+ & {get-vmmemory <<<< }
    + CategoryInfo          : ObjectNotFound: (get-vmmemory:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

有人知道为什么我一直得到这个输出吗?

【问题讨论】:

  • 试试这个:start "" "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" ....

标签: powershell scripting batch-file hyper-v


【解决方案1】:

Get-VMMemory 不是标准/内置 cmdlet - 它是 Hyper-V 模块的一部分。所以你真的有两个问题:

  1. 如果您真的在运行 PowerShell v1.0(EXE 位于 1.0 目录中,但所有版本都在同一个位置),该模块可能甚至无法运行
  2. 您没有加载模块,或者在运行批处理文件的地方没有可用的模块。

对于#1,以这种方式检查版本: \c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "write-host $psversiontable.psversion"

旁白:为什么要使用 UNC 路径指向本地机器的 C 盘?

对于 #2,您需要将代码包装在 PS1 文件中并告诉 powershell.exe 执行该文件,以便您可以导入模块。 PowerShell 3.0 会自动加载模块,但我不知道当你像这样执行 cmdlet 时它是否会这样做。

import-module Hyper-V
get-vmmemory | select VMelementName,reservation | out-file c:\Output.txt

powershell.exe -f myscript.ps1

编辑:根据您的 cmets,这些都不起作用。您需要使用 PSRemoting 从您的系统在远程系统上执行命令。

invoke-command -computername hypervhost -scriptblock {get-vmmemory | select VMelementName,reservation

以上内容未经测试。如果这不起作用,您将需要在您的环境中设置 PSRemoting。

【讨论】:

  • 我使用的是 UNC,因为这个脚本可以在任何管理员的机器上运行。 UNC 指向任何 HV 服务器上的 PowerShell 位置。
  • 我也尝试了以下方法:\\\c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "set-executionpolicy RemoteSigned"
  • 还有:\\\c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "import-module '\\SECAABHV03\c$\program files\模块\hyperv\hyperv.psd1'"; "get-vmmemory" >>aa.txt
  • 最后一个返回如下信息:Get-WmiObject : Invalid namespace At \\\c$\program files\modules\hyperv\VM.ps1:73 char:26 + Get- WmiObject
  • 我检查了版本,似乎我使用的是powershell 2.0。关于如何让脚本输出 powershell 结果的任何想法?
【解决方案2】:

通过使用 UNC 路径,您将从本地计算机上的远程计算机运行 PowerShell 可执行文件。但是,这不会为您带来任何好处,因为它与运行安装在本地计算机上的 PowerShell 可执行文件没有什么不同。您将无法像那样加载安装在远程机器上的模块。

您需要做的是在本地计算机上安装 library providing Get-VMMemory,导入模块(如 here 所述),然后对远程主机使用 cmdlet:

Get-VMMemory -VM "virtual_host" -Server "hyper-v_host"

或者,您可以转到System Center Virtual Machine Manager

【讨论】:

  • 这是最终起作用的命令: BATCH 文件中写入的行:C:\Windows\system32\windowspowershell\v1.0\powershell.exe -command "import-module '\\ c$\程序文件\模块\hyperv\hyperv.psd1'"; "get-vmmemory -server "" | 选择 V​​Melementname,reservation" >> outputfile.txt
  • 它是如何工作的:我使用 import 命令将 hyperv.psd1 导入到本地 powershell 模块中,在它被导入之后我执行了 get-vmmemory 命令,将它指向我想要的服务器喜欢运行它并使用 select 参数告诉它要返回什么。谢谢大家的帮助。不拘一格的信息对我理解它是如何工作的以及我如何可能使用它非常有用。
【解决方案3】:

get-vmmemory 不是标准的 cmdlet。如果该 cmdlet 在您运行 powershell 的计算机上不可用,您将收到此错误。

您可能需要运行 import-module 以确保将 vmware Ps 模块加载到会话中。

【讨论】:

    【解决方案4】:

    当我在运行 hyper v(不是 VM)的机器上的 PowerShell V1.0 控制台上键入 get-vmmemory 时,该命令有效。

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 1970-01-01
      • 2017-10-04
      • 2012-04-28
      • 2021-12-22
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      相关资源
      最近更新 更多