VMware Cloud Community
angoletti1
Contributor
Contributor

HowTo search for all VMX files in all datastores and register them into VC?

Hi,

I want to search in all datastores for VMX files and if the VM for each founded file is not registered in VC, register it.

How can this be done by the powershell using the easiest way?

Thanks in advance

Chris

Reply
0 Kudos
56 Replies
Jasemccarty
Immortal
Immortal

Hey Luc,

As mentioned in previous posts, I cannot retrieve the datastores by querying the Cluster. I can retrieve the datastores by querying a host in the cluster.

And yes, I'm using vCenter 4.0 patch 1, with ESX 4.0.

I'm going to try to query a host instead of the Cluster.

Thanks,

Jase

Jase McCarty

http://www.jasemccarty.com

Co-Author of VMware ESX Essentials in the Virtual Data Center (ISBN:1420070274) Auerbach

Co-Author of VMware vSphere 4 Administration Instant Reference (ISBN:0470520728) Sybex

Please consider awarding points if this post was helpful or correct

Jase McCarty - @jasemccarty
Reply
0 Kudos
LucD
Leadership
Leadership

Jase, I'll do some testing with the script against a vSphere cluster but it will be for tomorrow morning (CET).

Don't have access to the environment right now Smiley Sad


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

Reply
0 Kudos
Jasemccarty
Immortal
Immortal

Thanks Luc.

Any help is appreciated.

Jase

Jase McCarty

http://www.jasemccarty.com

Co-Author: VMware ESX Essentials in the Virtual Data Center (ISBN:1420070274) Auerbach

Co-Author: VMware vSphere 4 Administration Instant Reference (ISBN:0470520728) Sybex

Please consider awarding points if this post was helpful or correct

Jase McCarty - @jasemccarty
Reply
0 Kudos
LucD
Leadership
Leadership

Jase, did a major rewrite of the script.

You can find the new version in Raiders of the Lost VMX

Let me know if this solves the problem you were seeing.


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

Reply
0 Kudos
Jasemccarty
Immortal
Immortal

Luc,

Thanks for all the effort. I'll have to try it out a little later in the day.

I really appreciate it!

Jase

Jase McCarty

http://www.jasemccarty.com

Co-Author: VMware ESX Essentials in the Virtual Data Center (ISBN:1420070274) Auerbach

Co-Author: VMware vSphere 4 Administration Instant Reference (ISBN:0470520728) Sybex

Please consider awarding points if this post was helpful or correct

Jase McCarty - @jasemccarty
Reply
0 Kudos
smccreadie
Enthusiast
Enthusiast

LucD,

Thank you very much for working on teh new script. This will help me out tremendously. I tried out the script and it was able to register VM's on datastores that previously didnt work with the old script. However, when i run the script a second time with the VM's already registered, it fails and prints the error on screen:

Cannot index into a null array.

At :line:66 char:34

+ if(($file.FolderPath + $file.File[<<<< 0].Path) -eq $vmx)

Any ideas? Thanks again for all the hard work, it is very appreciated.

PS I am using ESX 4.0 Update 1.

Sean

EDIT: It seems to work when I runt he script against datastores that contain vmx files, even when the VM in the datastore is already registered it completes cleanly. But when i run it against a list of datastores, some of which do not contain vmx files, thats when i get the error. Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

That could explain it. A datastore without any VMX files on it.

Probably some strange behavior of the SearchDatastoreSubFolders_Task method.

I'll try a test run first thing in the morning.


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

Reply
0 Kudos
LucD
Leadership
Leadership

Can you give the new version (on my blog) a trial run.

Should handle the empty datastores more gracefully now.

And the script can now be used to register lost templates as well.


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

Reply
0 Kudos
smccreadie
Enthusiast
Enthusiast

LucD,

I tried the new version of the script and it works great! Thank you very much for all the hard work. I have a question though. The script registers the VM's into the default "DIscovered Virtual Machine" folder, how do I mofdify it to register into a differnt folder? I see the variable $tgtfolder but Im not sure what to do with it. Also, how to alter the parameters so I can use a list of datastores stored in a text file as the dsNames? Thanks again for the great script, its a huge help for me.

Sean

Reply
0 Kudos
LucD
Leadership
Leadership

The $tgtFolder is now the set to the hidden "vm" folder, which you see in the vSPhere Client as the "Discovered virtual machine" blue folder.

But you can replace that line by something else

$tgtFolder = Get-Folder "LostAndFound" | Get-View

Reading the datastore names from a TXT files can be done before you call the function.

Something like this

$dsNames = Get-Content "C:\dsnames.txt"
Register-VMX -dsNames $dsNames


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

Reply
0 Kudos
rogermccarrick
Contributor
Contributor

Hi

I'm quite a novice at powershell. Very happy to find your script but have a more specific issue that I'm trying to tackle.

I don't want to add ALL vmx files to inventory, just those in one particular data store.

I have a set up where I have a DR SAN that has LUNs snapmirrored to it. To test the DR each month, I have to break snapmirrors, add the LUNs as VMFS datastores, re-inventory the VMs, test, remove the VMs, remove the datastores, start snapmirrors.

So I have a datacenter, a Cluster-1 and a resource pool named "my_resource_pool".

I'm wondering if this powershell script can be altered to inventory all VMs in my VMFS datastore named "vmfs_store" and add them to "my_resource_pool"?

All the other VMs in the other data stores are registered and fine so I don't want to touch them.

I'm still trying to work on a powershell script to mount the luns, but that another story.

thanks.

Reply
0 Kudos
LucD
Leadership
Leadership

The script in my Raiders of the Lost VMX post allows you to specify specific datastores with the -dsNames parameter.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
rogermccarrick
Contributor
Contributor

Thanks a lot. I will take a look at this today and try it out.

Reply
0 Kudos
rogermccarrick
Contributor
Contributor

I'm looking at the script now. At the end I think I should change the line:

  1. Register-VMX -dsNames "datastore1","datastore2"

and uncomment it to:

Register-VMX -dsNames "my_vmfs_store"

so that it only looks in my_vmfs_store.

But I see that it says you cannot use entityName and dsNames together.

I have two Clusters in my Datacenter. When you inventory manually it asks you which cluster and then which resource pool.

Where does the VM end up when inventoried with this script?

Can you specify to look in my_vmfs_store and that the VMs should go to Cluster-1 > my_resource_pool ?

Reply
0 Kudos
LucD
Leadership
Leadership

That is correct, you can specify

Register-VMX -dsNames " my_vmfs_store"

In the current version, the guest is registered into the hidden "Resources" pool in the root of the datacenter where the datastore is located.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
rogermccarrick
Contributor
Contributor

Thanks LucD. This worked for me.

I just did a test run with one data store and one VM.

It added the VM under the data center as you said.

At http://www.virtu-al.net/2009/06/11/storage-vmotion-the-powercli-way/ I found this command:

Move-VM -VM (Get-VM -Name MyVM)-Destination (Get-ResourcePool -Name “Important“)

I added it to the end of your script and ran the whole thing as a .ps1 file.

It successfully added the single VM from my data store and then moved it to my resource pool.

.. now I have to figure out how to do step 1, that is, script mounting the vmfs volumes resignatured .

Thanx very much for the help

Reply
0 Kudos
julianwood
Enthusiast
Enthusiast

There's a slightly easier way now to add VMs to the vCenter inventory if you have the .VMX file location.

The New-VM cmdlet now has a new parameter -VMFilePath which allows you to specify a path to the virtual machine you want to register.

This is in the format: [mydatastore] SERVER01/SERVER01.vmx

Have a look at my blog post for more info:

http://www.wooditwork.com/2011/08/11/adding-vmx-files-to-vcenter-inventory-with-powercli-gets-even-e...

http://WoodITWork.com
Reply
0 Kudos