使用批处理来运行远程PowerShell V1.0命令

问题描述:

我想从我的Hyper-V主机收集一些信息。我有一大堆他们想要自动化这个过程。 我需要获取每台主机上运行的虚拟机。 我想从批处理脚本执行此操作。 当我运行在PowerShell的V1.0窗口(Hyper-V主机上)这个命令它的工作原理,并给了我所需要的信息:使用批处理来运行远程PowerShell V1.0命令

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 

没有人有任何线索,为什么我不断收到这个输出?

+0

试试这个:'start“”“c:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe”....' – Endoro 2013-03-19 13:30:13

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

你需要做的,而不是什么是你的本地copmuter安装library providing Get-VMMemory,导入模块(如例如here),然后使用该cmdlet对远程主机:

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

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

+0

这是最终工作的命令:在BATCH文件中写入的行:C:\ Windows \ system32 \ windowspowershell \ v1.0 \ powershell.exe -command“import-module'\\ c $ \ program files \ modules \ Hyper-V的\ hyperv.psd1' “; “get-vmmemory -server”“| select VMelementname,reservation”>> outputfile.txt – gmilic 2013-03-19 15:28:44

+0

工作原理:我使用import命令将hyperv.psd1导入到本地PowerShell模块中,在它被导入后立即执行get-vmmemory命令,将它指向我要运行它的服务器,并使用select参数告诉它返回的内容。谢谢大家的帮助。折衷信息对我理解这是如何工作的以及我如何可能使用它非常有用。 – gmilic 2013-03-19 15:30:57

get-vmmemory不是标准cmdlet。如果该cmdlet在运行powershell的计算机上不可用,则会出现此错误。

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

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

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

  1. 如果你真的运行PowerShell的1.0版(在1.0目录下的EXE土地,但所有版本都在同一个地方),模块可能赢得” t即使工作
  2. 您没有加载模块,或者没有可用的模块在您运行批处理文件的位置。

#1,检查版本是这样的: \ C $ \ Windows \ System32下\ WindowsPowerShell \ V1.0 \ powershell.exe “写主机$ 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。

+0

我使用的是UNC,因为这个脚本是从任何管理员的机器上运行的。 UNC指向任何HV服务器上的PowerShell位置。 – gmilic 2013-03-19 13:52:42

+0

我也尝试了以下内容:\\ \ c $ \ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe -command“set-executionpolicy RemoteSigned” – gmilic 2013-03-19 13:53:21

+0

还有:\\ \ c $ \ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe -command“import-module'\\ SECAABHV03 \ c $ \ program files \ modules \ hyperv \ hyperv.psd1'”; “get-vmmemory”>> aa.txt – gmilic 2013-03-19 13:53:46