0

I am trying to gather some information from my Hyper-V hosts. I have a whole bunch of them and would like to automate this process. I need to get the Virtual machines that are running on each host. I would like to do this from a batch script. When I run this command in the PowerShell V1.0 window (on the Hyper-V host) it works and gives me the information necessary:

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

This is how i am running this from a batch script:

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

This is the output I get

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

Does anyone have any clue as to why i keep getting this output?

1
  • Try this: start "" "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" .... Commented Mar 19, 2013 at 13:30

4 Answers 4

2

Get-VMMemory isn't a standard/built-in cmdlet - it's part of the Hyper-V module. So you've really got two issues here:

  1. If you're really running PowerShell v1.0 (the EXE lands in the 1.0 directory, but all versions are in the same place), the module probably won't even work
  2. You aren't loading the module, or don't have the module available where you're running the batch file.

For #1, check the version this way: \c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "write-host $psversiontable.psversion"

Aside: Why are you using a UNC path to point at the C drive of the local machine?

For #2, you will need to wrap your code in a PS1 file and tell powershell.exe to execute that, so that you can import the module. PowerShell 3.0 does automatic module loading, but I don't know if it will do it when you're executing the cmdlet like this.

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

powershell.exe -f myscript.ps1

Edit: Based upon your comments, neither of these will work. You need to use PSRemoting to execute the command on the remote system, from your system.

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

The above is untested. If this doesn't work, you will need to set up PSRemoting in your environment.

Sign up to request clarification or add additional context in comments.

8 Comments

I am using UNC because this script is meant to be run from any Admin's machine. The UNC points to the PowerShell location on any of the HV servers.
I have tried the following as well: \\<remote_computer>\c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "set-executionpolicy RemoteSigned"
and also: \\<remote_computer>\c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "import-module '\\SECAABHV03\c$\program files\modules\hyperv\hyperv.psd1'"; "get-vmmemory" >>aa.txt
The last one returns the following message: Get-WmiObject : Invalid namespace At \\<remote_computer>\c$\program files\modules\hyperv\VM.ps1:73 char:26 + Get-WmiObject <<<< -computername $Server -NameSpace $HyperVNamespace -Query $WQL | Add-Member -MemberType ALIASPROPERTY -Name "VMEleme ntName" -Value "ElementName" -PassThru + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
I checked the version and it seems that i am using powershell 2.0. Any idea on how i can get the script to output the powershell results?
|
1

By using the UNC path you'll be running the PowerShell executable from the remote machine on the local computer. However, that won't gain you anything, becaus it's no different from running a PowerShell executable installed on your local computer. You won't be able to load modules installed on the remote machine like that.

What you need to do instead is install the library providing Get-VMMemory on your local copmuter, import the module (as described e.g. here), and then use the cmdlet against the remote host:

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

Or, you could move to System Center Virtual Machine Manager.

2 Comments

This is the command that finally worked: Line written in BATCH file: C:\Windows\system32\windowspowershell\v1.0\powershell.exe -command "import-module '\\<remote_computer>c$\program files\modules\hyperv\hyperv.psd1'"; "get-vmmemory -server "<server_name>" | select VMelementname,reservation" >> outputfile.txt
How it works: I used the import command to import the hyperv.psd1 into the local powershell module, right after it has been imported i executed the get-vmmemory command, point it at what server i would like to run it against and use the select parameter to tell it what to return. Thank you all for the help. The eclectic information has been very useful in my understanding how this works and how i could potentially use it.
0

get-vmmemory is not a standard cmdlet. If that cmdlet is not available on the machine where you are running powershell you will get this error.

You might need to run import-module to ensure the vmware Ps module is loaded into the session.

Comments

0

When i type get-vmmemory at the PowerShell V1.0 console on the machine running hyper v (not VM) the command works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.