VMware Cloud Community
Oatcake
Contributor
Contributor
Jump to solution

Add .vmx files to inventory

Hi All

We have a situation where we are trying to automate the adding of VMs to our DR site. At the moment we are simulating this using a seperate VMFS datastore. We use a NetApp SAN which mirrors a copy of this over to the DR Datacentre, we break the mirror then mount the VMFS datastore. But what we are finding is that within the datastore the folder needs to be renamed before we can add the vmx file to the other site, maybe this is because it still exists in the 'Live' environment? I'm not sure.

So, what I would like to achive is to have a script that I can specify the datastore, cluster and maybe the resource group and it mounts all the vmx files within that datastore. I have already seen an excellent script by LucD (attached) which looks about perfect but when I run it it doesnt appear to do anything but also produces no error, I think it has something to do with how it is figuring out the location of the datastore but I'm not sure about this at all.

I hope that makes sense.

##############################
# Configure these Parameters

$dsname = "HS1-DRS-SYS1"
$datacenter = "MyCompany"
$cluster = "Leeds"
$virtualcenterserver = "hs1-vmc-001"


#####################################################################################################
#Actual Script (Do Not Make Any Changes below this line

Connect-VIServer -Server $virtualcenterserver
$ESXname = Get-Cluster $cluster | Get-VMHost | select -First 1
$dsBrowser = Get-View (Get-View (Get-VMHost -Name $ESXname).ID).DatastoreBrowser
$folder = Get-View (Get-Datacenter -Name $datacenter | Get-Folder -Name "vm").ID
$pool = Get-View (Get-Cluster -Name $cluster | Get-ResourcePool -Name "Resources").ID
#cls
Write-Host " "
Write-Host "Welcome to the Datastore Recovery System"
Write-Host " "
Write-Host "This script will now commence adding ALL Virtual Machines"
Write-Host "from Datastore" $dsname "on vSphere Server" $virtualcenterserver
Write-Host " "
foreach($dsImpl in $dsBrowser.Datastore){
    $ds = Get-View $dsImpl
    if($ds.Summary.Name -ne $dsname){continue}

    $datastorepath = "[" + $ds.Summary.Name + "]"

    $searchspec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
    $searchSpec.matchpattern = "*.vmx"

    Write-Host "Searching in path" $datastorepath

    $task = Get-View ($dsBrowser.SearchDatastoreSubFolders_Task($datastorePath, $searchSpec))
    while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued"){
        $task.UpdateViewData()
        sleep 5
    }
    if($task.info.result -ne $null){
        foreach ($file in $task.info.Result){

            if($file.FolderPath -match ".snapshot"){continue}
            $found = $FALSE
            foreach($vmx in $vms){
                if(($file.FolderPath + $file.File[0].Path) -eq $vmx){
                    Write-Host "`tVM is registered"
                    $found = $TRUE
                }
            }
            if (-not $found -and $task.Info.Result[0].File -ne $null){
                $vmx = $file.FolderPath + $file.File[0].Path
                $params = @($vmx,$null,$FALSE,$pool.MoRef,$null)
                Write-Host "Registering" $vmx
                $folder.GetType().GetMethod("RegisterVM_Task").Invoke($folder, $params)
            }

        }
    }
}

When I run this at the moment it says 'Welcome to the Datastore Recovery System' etc etc and then just does nothing. I did notice a thread where I got this from (again thanks) and it mentioned and NFS datastore which we don't use, I guess we use the traditional method.

We use vSphere 4.0 by the way.

I have tried breaking down the script to figure out what it is doing myself but to no avail unfortunately and I'm now Powershell guru! I have noticed that if I run through it manuall the $datastorepath returns [].

Any help with this would be much appreciated.

Thanks in adavnce

Julian

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, let's check if the DatastoreBrowser finds the all the datastores on all the nodes.

Use this to check.

foreach($esx in Get-Cluster $cluster | Get-VMHost){
    $dsBrowser = Get-View $ESX.ExtensionData.DatastoreBrowser
    foreach($dsObj in $dsBrowser.Datastore){
        $ds = Get-View $dsObj
       
Write-Host $esx.Name $ds.Name     } }


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

View solution in original post

0 Kudos
23 Replies
avlieshout
VMware Employee
VMware Employee
Jump to solution

What folder do you need to rename? the VM folder on the datastore?

Is the DR site using a dedicated vCenter Server?

I have a similar situation and we perform multiple tests this way without any problems. No need to rename anything.

When $datastorepath return "[]", that means that $ds.summary.name does not exist and I think the $dsbrowser doesn't see any datastores.

Are you sure that the datastore is mounted, As ESX4.0 doesn't add a copied datastore during a normal rescan operation.

You need to manually mount it using the Add storage button.

Script seems to work here.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

Hi

Thanks for the feedback.

Yes it's the VM folder in the datastore after it has been mounted. The DR site doesn't use a dedicated vCenter server, it is shared with the main one (the DR for that is also on my list to do).

Once we have done the necessary work on the SAN the datstore is definately being mounted after invoking the following command on each of the hosts

Get-VMHostStorage -RescanAllHba -RescanVmfs

I'm not having to having to mount them manually. As a test I removed an existing VM from the DR site and ran the script against the datastore and it still doesn't mount it.

Many Thanks

Julian

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

My mistake sorry, yes the script does work. It must be something to do with mounting the datstore. Thanks for pointing me in the right direction.

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

It's still quite odd though, if i run the following

Get-VMHostStorage -VMHost hs2-vmh-001 | select -expandproperty FileSystemVolumeInfo |fl

It does indeed tell me the datastore is on that server

AccessMode : readWrite
Path       : /vmfs/volumes/4d2c3a79-94a4ef9b-f754-00144f8ceb65
Capacity   : 268167020544
Name       : HS1-DRS-SYS1
Type       : VMFS

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Note that the script uses the first ESX host in the cluster to find the DatastoreBrowser.

$ESXname = Get-Cluster $cluster | Get-VMHost | select -First 1

Could that have anything to do with it ?

Is the datastore visible on all the ESX hosts in the cluster ?

You can check with

Get-Cluster $cluster | Get-VMHost | Select Name,@{N="DS";E={[string]::Join(',',(Get-Datastore -VMHost $_ | %{$_.Name}))}}


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

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

Thanks for the reply

Yes it is available to both ESX servers in the cluster, if I run that command i get the following:

Name : hs2-vmh-002
DS   : HS2-UAT-SYS1,HS2-UAT-PAG1,HS2-DEV-SYS1,HS2-PRD-PAG1,HS2-VMH-002 Local,HS
       2-UAT-SYS2,HS2-DEV-SYS2,HS2-PRD-SYS1,HS1-DRS-SYS1

Name : hs2-vmh-001
DS   : HS2-UAT-SYS1,HS2-UAT-PAG1,HS2-DEV-SYS1,HS2-PRD-PAG1,HS2-UAT-SYS2,HS2-DEV
       -SYS2,HS2-PRD-SYS1,HS2-VMH-001 Local,HS1-DRS-SYS1

The datastores in bold are the ones I'm trying to mount from.

Man ually running throuigh the script it seems as though it's the line

ds = Get-View $dsImpl

That isn;t picking the datastore up, if i run the following (although I'm not sure if I've done it quite right)

Get-View  $dsbrowser.datastore

It shows all the datastores except the one I am trying to add the files from.

The ds exisiting on another cluster to begin with and has now been mounted on another and has also been disconnected from the original.

Hope this helps.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, let's check if the DatastoreBrowser finds the all the datastores on all the nodes.

Use this to check.

foreach($esx in Get-Cluster $cluster | Get-VMHost){
    $dsBrowser = Get-View $ESX.ExtensionData.DatastoreBrowser
    foreach($dsObj in $dsBrowser.Datastore){
        $ds = Get-View $dsObj
       
Write-Host $esx.Name $ds.Name     } }


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

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

Interesting, it doesnt appear to be picking the DS in question up:

hs2-vmh-002 HS2-VMH-002 Local
hs2-vmh-002 HS2-DEV-SYS1
hs2-vmh-002 HS2-DEV-SYS2
hs2-vmh-002 HS2-PRD-SYS1
hs2-vmh-002 HS2-PRD-PAG1
hs2-vmh-002 HS2-UAT-SYS1
hs2-vmh-002 HS2-UAT-SYS2
hs2-vmh-002 HS2-UAT-PAG1
hs2-vmh-001 HS2-VMH-001 Local
hs2-vmh-001 HS2-DEV-SYS1
hs2-vmh-001 HS2-DEV-SYS2
hs2-vmh-001 HS2-PRD-SYS1
hs2-vmh-001 HS2-PRD-PAG1
hs2-vmh-001 HS2-UAT-SYS1
hs2-vmh-001 HS2-UAT-SYS2
hs2-vmh-001 HS2-UAT-PAG1

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Does the following pick up the datastore ?

Get-VMHostStorage -VMHost $esx -RescanAllHba -RescanVmfs

Make sure $esx holds one of the nodes of the cluster.


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

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

Part of the process we run through is to run these commands on each ESX host and then the datastore pops up in the vsphere client.

hs1-drs-001.JPG

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And although you see the datastore in the vSphere client, the script from 2 posts ago doesn't show the datastore ?


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

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

Ive just ran it again and it's definately not showing using the script you provided two posts ago.

[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> & '.\DataBroswer finds a
ll Stores.ps1'
hs2-vmh-002.thompsons.local HS2-VMH-002 Local
hs2-vmh-002.thompsons.local HS2-DEV-SYS1
hs2-vmh-002.thompsons.local HS2-DEV-SYS2
hs2-vmh-002.thompsons.local HS2-PRD-SYS1
hs2-vmh-002.thompsons.local HS2-PRD-PAG1
hs2-vmh-002.thompsons.local HS2-UAT-SYS1
hs2-vmh-002.thompsons.local HS2-UAT-SYS2
hs2-vmh-002.thompsons.local HS2-UAT-PAG1
hs2-vmh-001.thompsons.local HS2-VMH-001 Local
hs2-vmh-001.thompsons.local HS2-DEV-SYS1
hs2-vmh-001.thompsons.local HS2-DEV-SYS2
hs2-vmh-001.thompsons.local HS2-PRD-SYS1
hs2-vmh-001.thompsons.local HS2-PRD-PAG1
hs2-vmh-001.thompsons.local HS2-UAT-SYS1
hs2-vmh-001.thompsons.local HS2-UAT-SYS2
hs2-vmh-001.thompsons.local HS2-UAT-PAG1
[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts>

Thanks for you help with this.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Does it change when you add the -Refresh parameter ?

Get-VMHostStorage -VMHost $esx -RescanAllHba -RescanVmfs -Refresh


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

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

I'm afraid not. I've pasted the commands and output I ran. In your script to find what the databrowser thinks it has got I substituted

$cluster with "Leeds".

[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> Get-VMHostStorage -VMHos
t "hs2-vmh-001.thompsons.local" -RescanAllHba -RescanVmfs -Refresh

Id                             SoftwareIScsiEnabled
--                             --------------------
HostStorageSystem-storageSy... False


[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> Get-VMHostStorage -VMHos
t "hs2-vmh-002.thompsons.local" -RescanAllHba -RescanVmfs -Refresh

Id                             SoftwareIScsiEnabled
--                             --------------------
HostStorageSystem-storageSy... False


[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> & '.\DataBroswer finds a
ll Stores.ps1'
hs2-vmh-002.thompsons.local HS2-VMH-002 Local
hs2-vmh-002.thompsons.local HS2-DEV-SYS1
hs2-vmh-002.thompsons.local HS2-DEV-SYS2
hs2-vmh-002.thompsons.local HS2-PRD-SYS1
hs2-vmh-002.thompsons.local HS2-PRD-PAG1
hs2-vmh-002.thompsons.local HS2-UAT-SYS1
hs2-vmh-002.thompsons.local HS2-UAT-SYS2
hs2-vmh-002.thompsons.local HS2-UAT-PAG1
hs2-vmh-001.thompsons.local HS2-VMH-001 Local
hs2-vmh-001.thompsons.local HS2-DEV-SYS1
hs2-vmh-001.thompsons.local HS2-DEV-SYS2
hs2-vmh-001.thompsons.local HS2-PRD-SYS1
hs2-vmh-001.thompsons.local HS2-PRD-PAG1
hs2-vmh-001.thompsons.local HS2-UAT-SYS1
hs2-vmh-001.thompsons.local HS2-UAT-SYS2
hs2-vmh-001.thompsons.local HS2-UAT-PAG1
[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts>

It also still appears in the GUI on both the servers in cluster Leeds.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Let's check if the LVM.Enableresignature is set.

First check with

Get-Cluster $cluster | Get-VMHost  | %{
  $optmgr = Get-View $_.Extensiondata.configManager.advancedOption

  $optarray = $optmgr.QueryOptions("LVM.Enableresignature")
  Write-Host $_.Name  $optarray[0].Key $optarray[0].Value
}

If it show 0, you can enable it with

Get-Cluster $cluster | Get-VMHost | %{
  $optmgr = Get-View $_.Extensiondata.configManager.advancedOption

  $optarray = $optmgr.QueryOptions("LVM.Enableresignature")
  $optarray[0].Value = [long]1
  $optmgr.UpdateOptions($optarray)
}

And then check again if the datastore is present in the datastorebrowser.


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

0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

Do you have esx4.0 or 4.1?

I would strongly advise you to create another vcenter for your dr site.

That said, you are mounting a copy of a datastore which has the same uuid as the original one. Both are in the same vcenter inventory, maybe that's causing problems.

Did you try to resignature the copy on mounting?

Sent from my iPad

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

OK, It was set to 0 and now it is set to 1 but i'm afraid it is still not showing the databrowser::smileyconfused: does the resignature only happen when the datastore is added?

[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> .\CheckLVMEnableresignat
ure.ps1
hs2-vmh-002.thompsons.local LVM.Enableresignature 0
hs2-vmh-001.thompsons.local LVM.Enableresignature 0
[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> .\EnableVMResignature.ps
1
[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> .\CheckLVMEnableresignat
ure.ps1
hs2-vmh-002.thompsons.local LVM.Enableresignature 1
hs2-vmh-001.thompsons.local LVM.Enableresignature 1
[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> & '.\DataBroswer finds a
ll Stores.ps1'
hs2-vmh-002.thompsons.local HS2-VMH-002 Local
hs2-vmh-002.thompsons.local HS2-DEV-SYS1
hs2-vmh-002.thompsons.local HS2-DEV-SYS2
hs2-vmh-002.thompsons.local HS2-PRD-SYS1
hs2-vmh-002.thompsons.local HS2-PRD-PAG1
hs2-vmh-002.thompsons.local HS2-UAT-SYS1
hs2-vmh-002.thompsons.local HS2-UAT-SYS2
hs2-vmh-002.thompsons.local HS2-UAT-PAG1
hs2-vmh-001.thompsons.local HS2-VMH-001 Local
hs2-vmh-001.thompsons.local HS2-DEV-SYS1
hs2-vmh-001.thompsons.local HS2-DEV-SYS2
hs2-vmh-001.thompsons.local HS2-PRD-SYS1
hs2-vmh-001.thompsons.local HS2-PRD-PAG1
hs2-vmh-001.thompsons.local HS2-UAT-SYS1
hs2-vmh-001.thompsons.local HS2-UAT-SYS2
hs2-vmh-001.thompsons.local HS2-UAT-PAG1

[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> Get-VMHostStorage -VMHos
t "hs2-vmh-001.thompsons.local" -RescanAllHba -RescanVmfs -Refresh

Id                             SoftwareIScsiEnabled
--                             --------------------
HostStorageSystem-storageSy... False


[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> Get-VMHostStorage -VMHos
t "hs2-vmh-002.thompsons.local" -RescanAllHba -RescanVmfs -Refresh

Id                             SoftwareIScsiEnabled
--                             --------------------
HostStorageSystem-storageSy... False


[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts> & '.\DataBroswer finds a
ll Stores.ps1'
hs2-vmh-002.thompsons.local HS2-VMH-002 Local
hs2-vmh-002.thompsons.local HS2-DEV-SYS1
hs2-vmh-002.thompsons.local HS2-DEV-SYS2
hs2-vmh-002.thompsons.local HS2-PRD-SYS1
hs2-vmh-002.thompsons.local HS2-PRD-PAG1
hs2-vmh-002.thompsons.local HS2-UAT-SYS1
hs2-vmh-002.thompsons.local HS2-UAT-SYS2
hs2-vmh-002.thompsons.local HS2-UAT-PAG1
hs2-vmh-001.thompsons.local HS2-VMH-001 Local
hs2-vmh-001.thompsons.local HS2-DEV-SYS1
hs2-vmh-001.thompsons.local HS2-DEV-SYS2
hs2-vmh-001.thompsons.local HS2-PRD-SYS1
hs2-vmh-001.thompsons.local HS2-PRD-PAG1
hs2-vmh-001.thompsons.local HS2-UAT-SYS1
hs2-vmh-001.thompsons.local HS2-UAT-SYS2
hs2-vmh-001.thompsons.local HS2-UAT-PAG1
[vSphere PowerCLI] C:\ServerSupport\DR Testing Scripts>

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

Hi avilesot - we are on ESX 4.0. I think with the help of LucD I have just tried a resignature of the DS.

I have removed the original DS from the Live site for the purpose of this so I'm not sure that it would have the smae uuid, saying that it was removed after.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, rescan doesn't add it, try adding the LUN again (but I'm not sure that will make a difference).


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

0 Kudos