Search code examples
vmwarevspherepowercli

Cannot create new VM from powercli


I'm using PowerCLI 6.0 to create a new virtual machine from another virtual machine. I'm using this cmdlet:

New-VM -Name MyName -VM $sourceVM -Datastore $myDataStore -VMHost (Get-VMHost)

But it returns following exception:

New-VM The operation for the entity "xxx" failed with the following message: "The operation is not supported on the object."

The source VM is powered off. Version of vSphere is 5.5.

I tried to google this error but without any success.

Now, I have no idea where the problem is.


Solution

  • Get-vmhost by itself will return all hosts and not specify one host to use, which is what this command expects.

    If you know a host you wish to build on you could use the below script:

    New-VM -Name MyName -VM $sourceVM -Datastore $myDataStore -VMHost $(Get-VMHost "specify host name")
    

    Otherwise you could build an array and select a vmhost from that array by using the below

    # This command builds an array of all you host names into the variable $vmh
    
    $vmh = get-vmhost 
    
    # This command selects random host from this array assigning it to the $vmhost variable
    
    $vmhost = Get-random -inputobject $vmh
    
    # Now build your VM command with the randomly selected host 
    
    New-VM -Name MyName -VM $sourceVM -Datastore $myDataStore -VMHost $vmhost
    

    Make sure you are defining a datastore in a similar fashion as well for your $mydatastore variable

    Just making a variable built off get-datastore will not work you have to narrow it down to a single store for the operation New-VM to work

    $mds = get-datastore
    
    $mydatastore = Get-random -InputObject $mds
    

    Also make sure your $sourceVM is being defined correctly as well. if more than one object is in this variable it will not work. Make sure $sourceVM equals a single VM name.

    As you can see below, when I run the command without clearly identifying it in the variable it gives me nearly the same error (Please note my errors will look a bit different because I use PowerGUI to generate and build my scripts). The highlighted text is the command I ran which looks like yours. The text in red in the red box at the bottom is the error. B y defining your host through random selection or by name should make the command work.

    PowerGUI Image VM Creation

    I also replied to your VMWare Community Forum post as well.