0

我正在尝试从我的 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

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

4

4 回答 4

2

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

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

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

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

于 2013-03-19T13:43:04.830 回答
1

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

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

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

或者,您可以移至System Center Virtual Machine Manager

于 2013-03-19T14:19:52.800 回答
0

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

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

于 2013-03-19T13:32:47.477 回答
0

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

于 2013-03-19T13:38:32.543 回答