2013-03-19 158 views
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

回答

1

通過使用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

0

get-vmmemory不是標準cmdlet。如果該cmdlet在運行powershell的計算機上不可用,則會出現此錯誤。

您可能需要運行import-module以確保vmware Ps模塊已加載到會話中。

0

當我在運行hyper v(不是VM)的機器上的PowerShell V1.0控制檯上鍵入get-vmmemory時,該命令有效。

2

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