VMware Cloud Community
alasdair_carnie
Enthusiast
Enthusiast
Jump to solution

Registering existing VMs

Hi all,

I've read a few posts regarding searching a datastore for VMs and registering them with Virtual Center, and I'm able to do that. However I was wondering if anyone can help me with being able to register specific VMs on specific ESX Hosts using an IF ESX Host name equals XYZ then register the following VMs. I essentially have a bunch of VMs and need to regiser them based on hte ESX hostname rather than the name of the VM. My Powershell knowledge is still quite basic so any help will be much appreciated.

Best Regards,

Alasdair....

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

To check if a VM is already registered you can use the Get-VM cmdlet.

If this returns an object the VM is already registered.

Something like this for example

...
# This entry is used only to prevent the result from being printed to screen
	$dummy = $results.File[0].Path -match [regex]"^(.+)\.vmx"
	$vmName = $Matches[1]
# Check if guest $vmName is already registered 
                if((Get-VM $vmName -ea SilentlyContinue) -eq $null){
	  $task = $folder.RegisterVM_Task($vmx,$vmName,$FALSE,$pool.MoRef,$esx.MoRef)
                }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

I would need a bit more information to check if that is possible.

In the unregistered guest's .VMX file there is no indication of an ESX hostname.

And datastores can be shared between different ESX servers, so I can't use the datastorename to determine which ESX host to pick for the registration.

So in short, how can I place the link between the .VMX files I find on datastore X to register those on ESX server Y ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
alasdair_carnie
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

I know the names of the ESX Hosts and the names of the VMX files. I want to be able to scan the datastore and then based on the .VMX names register them to a specific ESX Host. I was thinking about using IF, THEN, ELSE to do it but I'm not sure if that would work. I didn't want to have to hardcode the names of the Hosts and VM Names so if it's possible to do it a quicker or at least less code intensive way then I'd appreciate any guidence. I have the code you sent me before about registering a pre existing template and I planned on using it for registering the VMs, however that means registering them one at a time and I'd like to be able to register a number of them based on the name of the VMX file.

Cheers,

Alasdair......

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Aren't there any rules for deciding which VMX needs to be registered on which ESX host ?

Something like for example, all VMX files that start with the letters DEV should be registered on ESX1 and all the VMX files that start with OPS need to be registered on ESX2.

Without any rules it will be difficult to write and IF, THEN, ELSE.

Could you perhaps show with some examples how you would decide which VMX goes to which ESX ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
alasdair_carnie
Enthusiast
Enthusiast
Jump to solution

I've been playing around with your register-vm script and I'm wondering if I could modify this script to specify a datastore and change the regular expression to look for a text pattern, so if I have say 50 VMs and 10 of them have the word copper in the .vmx filename, can I use this information to run the RegisterVM.Task? I'm sorry if I'm asking basic questions, but I've not had any time to sit with powershell and learn how to use it. I'm basically doing a Frankenstein with bits of scripts to try and get something going, so any help is appreciated.

0 Kudos
admin
Immortal
Immortal
Jump to solution

You can probably use something like:

$vmxPath -like "*copper*"

If you put that in a where loop you can do conditional registrations.

0 Kudos
alasdair_carnie
Enthusiast
Enthusiast
Jump to solution

Could you perhaps provide me with some sample code I can adapt. I'm struggling with this as my programming knowledge is very limited. I've managed to put together a number of scripts by adapting bits of code, but I don't have the knowledge yet to atempt to code from scratch.

Thanks in advance.

0 Kudos
alasdair_carnie
Enthusiast
Enthusiast
Jump to solution

I've spent the last couple of days trying to figure this out and I've come up with a working script. I'm sure it's not elegant but it does what I need it to do. Please feel free to offer any improvements. It basically creates an array of ESX Hosts and then scans a datastore for VMX files and then registers them based on a specific search pattern associated with each ESX host. I've attached the script for convenience. I've not been able to figure out how to insert a check to ensure that the VM's are not already registered with another ESX host or are Powered On, but that shouldn't be an issue in my environment.

Best regards,

Alasdair............

PS. Thanks for all the suggestions.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

To check if a VM is already registered you can use the Get-VM cmdlet.

If this returns an object the VM is already registered.

Something like this for example

...
# This entry is used only to prevent the result from being printed to screen
	$dummy = $results.File[0].Path -match [regex]"^(.+)\.vmx"
	$vmName = $Matches[1]
# Check if guest $vmName is already registered 
                if((Get-VM $vmName -ea SilentlyContinue) -eq $null){
	  $task = $folder.RegisterVM_Task($vmx,$vmName,$FALSE,$pool.MoRef,$esx.MoRef)
                }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
alasdair_carnie
Enthusiast
Enthusiast
Jump to solution

Thanks LucD.

0 Kudos
Trebormint
Contributor
Contributor
Jump to solution

Hi

Thanks for a useful thread, from it and a couple of others I've now got a script that registers VMs from a predefined list of datastores. It is predefined as it is a BC test and I'm recovering replicated datastores on another infrastructure.

What I would also like to do is change the networkname used by the networkcard. I've got this working to a point, the point being it will work if I run it on an existing VM. However, I'd like to change it on the fly as the existing VM is registered, but I'm getting an error stating that the VM does not exist. This makes sense because it attempts to make the change before the machine has registered. Any ideas how I can get around this? My initial thought was to make the script wait until the registration is complete but I have no idea how to do this.

Also, I'm being asked to confirm the change to the networkname. Presumably I can remove the confirmation prompt as well?

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can loop after the RegisterVM_Task method till the task is finished.

Something like this

...
	$taskMoRef = $folder.RegisterVM_Task($vmx,$vmName,$FALSE,$pool.MoRef,$esx.MoRef)
# Wait till VM is registered
       $task = Get-View $taskMoRef
       while($task.info.state -eq "running" -or $task.info.state -eq "queued"){
          $task = Get-View $taskMoRef
       }
# Test the return code of the RegisterVM_Task
       if($task.info.error -eq $null){
# Change network name
          ....
       }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Trebormint
Contributor
Contributor
Jump to solution

Thanks, that looks like it'll do the trick.

I'm very rusty with the programming skills at the moment and have only just started looking at Powershell. Not the best combination and it makes progress a little slow!!!

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

hi Luc,

I need a script to do this,

1. i have a csv file with the VMX locations in it.(one path per row) eg: /vmfs/volumes/datastore000/abc/abc.vmx

2. The script should take a input from csv file and ask for the esx box name to register.

3. then the script should register all the VM's in that csv file to the esx box given.

4.Finally the script should give a result that registered successfully or not.

Also need a one liner to retrive the vmx location of all vm's in the VC. already have one script but that does not work now after updating powercli. Planning to use when esx crashes and there is a need to re-register all the VM's manually in other hosts.

Thanks in Advance!

0 Kudos