Trying to run HyperV commands on a Guest

Mar 23, 2012 at 4:34 PM
Edited Mar 23, 2012 at 4:37 PM

I am in the process of setting up an automated script for building out new server environments in HyperV. I am trying to use powershell from a HyperV guest. Everytime I try running something like

NEW-VM -Name "TESTVM" -Server "DEV-VHOST03"

it returns an error

Get-WmiObject : Invalid class
At C:\Program Files\modules\HyperV\VM.ps1:445 char:34
+         $VSMgtSvc = Get-WmiObject <<<<  -ComputerName $Server -Namespace $HyperVNamespace -Class "MSVM_Virtu
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand


I found some other posts and tried

Get-WmiObject -List -Namespace "root\virtualization"

this in turn gave me



Get-WmiObject : Could not get objects from namespace root\virtualization. Invalid namespace
At line:1 char:14
+ Get-WmiObject <<<<  -List -Namespace "root\virtualization"
    + CategoryInfo          : ObjectNotFound: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : INVALID_NAMESPACE_IDENTIFIER,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I am beginning to think I cannot get this to run from a guest, which really would stink, as I don't want to have all of this on one of the hosts.

The last thing I tried is

gwmi -ComputerName "Dev-Vhost03" -Namespace root\virtualization -Class "MSVM_VirtualSystemManagementService"

Which does actually connect and gives me the output I had expected.



If anyone has any ideas how I can get this to work or even some ideas I can try that would be great.




Mar 23, 2012 at 5:13 PM

After some further digging, it looks like the variable $HyperVNamespace is not set when it tries to execute the code in vm.ps1. the weird thing to me is that on initial load of the vm.ps1 the variable is set correctly. I will continue digging.

May 11, 2012 at 3:43 PM

I'd check if the WMI repository has no issues. Can you run winmgmt.exe /verifyrepository

if it reports an error, try the salvagerepository switch:

Cheers, E