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

0 Kudos
56 Replies
smccreadie
Enthusiast
Enthusiast

LucD,

Thanks for the quick response. I have determined that the loop is working correctly after all, but the problem lies with my failed over LUNs. We use EMC recoverpoint for our datastore replication and DR. When I enable access to these LUNs in our offsite location, the ESX hosts can see all the new storage and can browse the new datastores. I can register the failed over VM's no problem through the GUI or command line, I just cant use Register-VMx function on those datastores. I was wrong earlier when I said I could enter the datastore names manually, when i do that for the Recoverpoint LUNs, it fails. But I can use the Register-VMx function and the foreach loop that I posted earlier to successfully register any VM that is on a LUN that is not replicated to offsite, meaning any LUN that is available in our offsite location that is not part of Recoverpoint. When i use the LUN names of the replicated LUNs, the function loops through them with no result, as if the name was invalid. So im thinking that maybe there is some issue with the way the Register-Vmx function uses the datastore name to locate the datastore and folders? Im using ESX and VC 4.0. I know this is not really an issue with the function, but any help is most appreciated. Thanks again.

Sean

0 Kudos
LucD
Leadership
Leadership

The datastore name passed to the function is used to determine which datastore to search.

Can you do a browse datastore from the VIC on these replicated LUNs ?

Perhaps you should first call this method ResignatureUnresolvedVmfsVolume_Task on the replicated LUNs ?


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

0 Kudos
smccreadie
Enthusiast
Enthusiast

Yeah i can browse and use the datastores no problem in the VIC. I can register and power on the VM's through the VIC. I wonder if there is an issue with the LUN signing, these 4.0 hosts are upgraded from 3.5. When I did the upgrade, the hosts had "disallowsnapLUN" parameter set to "0", so now when I enable access to these replicated LUNs, the datastores are automatically mounted. This wasn't intended by me but it is working. I wonder if a fresh build of 4.0 would behave differently, and require resigning? Anyways i dont know how to test this or to resolve. I will look at the link you just sent. Thanks again.

Sean

0 Kudos
smccreadie
Enthusiast
Enthusiast

So i tested all my replicated LUNs (I currently have 7) and discoverd that I can use the Register-Vmx function on one of them, but the other 6 dont work. It doesnt appear like anything is different between the LUNs, so i guess its back to the drawing board! Maybe if i invoke a resigature of the LUNs like you suggested that will free up whatever is causing the issue. Thanks again for all the help and the great code.

Sean

0 Kudos
smccreadie
Enthusiast
Enthusiast

So i tried resigning the datastores and running the Register-Vmx function, still doesnt work. It baffles me as to what it is about the replicated LUNs that this function doesnt like. I tried resigning, renaming, etc. I just cant figure out where the place is that the function is able to tell something is different. Any ideas would be really helpful. Thanks again for all the help.

Sean

0 Kudos
smccreadie
Enthusiast
Enthusiast

One last try on this before I give up I guess. I tried running the Register-VMx function by authenticating directly to the ESX host and not Virtualcenter. It worked. So it seems there is some reason that the function doesnt have permission to access files on the datastores that are replicated? Just wondering if anyone has seen anything like this? Thanks

Sean

0 Kudos
bulletprooffool
Champion
Champion

I am scripting the migration of VMs from one Datastore and cluster to a different set (this is for DR - we replicate all VMs using NFS and the Datastores are accesible on the remote side as 'read only')

I have an array, which is created by either importing the previous known config (nightly script to Inventory VM config to csv, or in the case of graceful failover, by prompting the user.

So far, my script does the following:

Prompt user for source and destination Datacenter, Cluster, Datastore

It then reads the source Datastore and finds all VMs, creating an array with the following:

VMName, ResourcePool, VMXPath, PortGroups, SourceCluster, Sourcedatastore, DestinationDatastore, MacAddress, Hostname, IPAddress, VMState,

It then shuts down the VMs and removes them from the source inventory

All I'd like to do is figure out how to register the VMs on the new cluster (I know the cluster name) and make sure they are registered to the same ResourcePool names as before.

The easy way I guess is to import ALL VMs, then change the the resourcepool setting for each VM?, but I was hoping for something a little prettier.

One day I will virtualise myself . . .
0 Kudos
bulletprooffool
Champion
Champion

ne'er mind - figured it out

One day I will virtualise myself . . .
0 Kudos
bobcampling
Contributor
Contributor

when using the RegisterVM.ps1 script, I am getting the following error:

Get-View : The argument cannot be null or empty.

At C:\DATA\RegisterVM.ps1:12 char:17

+ $ds = Get-View <<<< $dsImpl

Get-View : The argument cannot be null or empty.

At C:\DATA\RegisterVM.ps1:15 char:19

+ $vm = Get-View <<<< $vmImpl

I tried this on a couple systems, I get the error.

How do I troubleshoot this?

Thank you for your help.

0 Kudos
LucD
Leadership
Leadership

It looks as if there is no datastore connected to the ESX server you specified.

Or the problem could be higher up in the script. Are the datacenter and clustername correct ?


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

0 Kudos
bobcampling
Contributor
Contributor

There is a datastore, but it is a snap_*** volume (snap-31dbea94-DATA01). Does that matter?

This site is a DR site. We are using HP Lefthand SANs and doing a replication (remote IP copy).

We want to be able to snapshot, mount, then backup.

We are using vCenter, ESXi 4.0

0 Kudos
smccreadie
Enthusiast
Enthusiast

Sounds like my issue, where the function works perfect on ordinary LUNs but fails against my EMC recoverpoint replicated LUNs. I dont get any error output though. I am using vSphere 4.0 as well. I hope someone can figure this out, its way beyond me! Thanks

Sean

0 Kudos
bobcampling
Contributor
Contributor

FYI - i can mount, then add the guest to inventory in the vSphere client.

I can also use the CLI (vmware-cmd.pl) to manually add a test guest VM.

After every remote snapshot ... my datastore name will change so I can't write a static script.

0 Kudos
LucD
Leadership
Leadership

Does it show when you do a

Get-VMHost <hostname> | Get-Datastore

If not, does it show up when you do a rescan ?


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

0 Kudos
bobcampling
Contributor
Contributor

i can see the volume in vCenter client and in the PowerCLI:

C:\&gt; connect-viserver -server wyovm03

Name Port User

---- -


-

wyovm03 443 root

C:\&gt; get-vmhost wyovm03 | get-datastore

Name FreeSpaceMB CapacityMB

---- -


-

snap-6adb5566-DATA01 126613 562944

C:\&gt;

When i run the script:

Get-View : The argument cannot be null or empty.

At C:\DATA\RegisterVM.ps1:11 char:17

+ $ds = Get-View &lt;&lt;&lt;&lt; $dsImpl

Get-View : The argument cannot be null or empty.

At C:\DATA\RegisterVM.ps1:14 char:19

+ $vm = Get-View &lt;&lt;&lt;&lt; $vmImpl

You cannot call a method on a null-valued expression.

At C:\DATA\RegisterVM.ps1:22 char:57

+ $taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task( &lt;&lt;&lt;&lt; $datastorePath

, $searchSpec)

Get-View : The argument cannot be null or empty.

At C:\DATA\RegisterVM.ps1:23 char:19

+ $task = Get-View &lt;&lt;&lt;&lt; $taskMoRef

Cannot index into a null array.

At C:\DATA\RegisterVM.ps1:29 char:41

+ if(($file.FolderPath + $file.File[0 &lt;&lt;&lt;&lt; ].Path) -eq $vmx){

0 Kudos
bobcampling
Contributor
Contributor

OK ...well i hacked at the script a little more. I pointed the script towards the direct host ESXi box instead of the vCenter host.

Now is can see the datastore, I do a Write-Host for the variables $vmx & $matches[1]

if (-not $found -and $file.File -ne $null){

$vmx = $file.FolderPath + $file.File[0].Path

$res = $file.File[0].Path -match $guestname

Write-Host $vmx

  • Write-host $matches[1]*

$folder.RegisterVM_Task($vmx,$matches[1],$FALSE,$null,$null)

When i run the script, I can see the path and name of the VM. I get the following error:

Exception calling "RegisterVM_Task" with "5" argument(s): "A specified paramete

r was not correct.

pool"

At C:\DATA\RegisterVM2.ps1:40 char:30

+ $folder.RegisterVM_Task( &lt;&lt;&lt;&lt; $vmx,$matches[1],$FALSE,$null,$null)

Any ideas or comments?

0 Kudos
robertbr
Contributor
Contributor

Thanks a lot for the script, I used it to register 200 machines and it worked great.

I think there is an issue however with registering machines that have - (dash) in the path to vmx. I did not spend time to correct it because there were not many of them.

0 Kudos
bobcampling
Contributor
Contributor

I have one server with a " - " in the name. It seems like that imported OK. I will have to try it again.

if someone wants it ... This script updates the UUID so you don't get that UUID question:

$key = "uuid.action"

$value = "create"

get-vm | foreach {

$vm = Get-View $_.Id

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

$vmConfigSpec.extraconfig += New-Object VMware.Vim.optionvalue

$vmConfigSpec.extraconfig[0].Key=$key

$vmConfigSpec.extraconfig[0].Value=$value

$vm.ReconfigVM($vmConfigSpec)

}

This one disconnects the NIC on each server:

get-vm | foreach {

Write-Host Get-VM $_

Set-NetworkAdapter -NetworkAdapter ( Get-NetworkAdapter (Get-VM $_ )) -StartConnected:$false -Confirm:$false

}

0 Kudos
Jasemccarty
Immortal
Immortal

Hey Luc,

I'm having a problem with this script. I've made the appropriate Datacenter, Cluster, and Host changes for my environment, but I'm failing on the $dsImpl line:

Get-View : The argument cannot be null or empty. At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\atl.ps1:11 char:17

Any suggestions?

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
0 Kudos
LucD
Leadership
Leadership

Hi Jase,

Looks a bit like the problem bobcampling was having a bit higher up in this thread.

I suspect you're running this against a vSphere cluster with ESX 4 hosts ?

When you do a

Get-Cluster <cluster-name> | Get-Datastore

do the datastores show up ?


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

0 Kudos