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
23 Replies
Oatcake
Contributor
Contributor
Jump to solution

It doesn't appear the have made a differene. I unmounted the LUN did a rescan on both hosts, made sure it had the correct name and I'm still getting the same symptoms and it's not appearing in the databrowser. I hope it's not me being totally dumb here!

0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

Need to rescan after resignature.

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

I tried that I'm afraid.

0 Kudos
Oatcake
Contributor
Contributor
Jump to solution

Hi LucD

I know it's been a few weeks now but I just thought I'd let you know that the script works a treat but for some reason I have to restart the VMware VirtualCenter Server for it to pick up the new datastore using PSH. I'm not sure why.

Many Thanks

0 Kudos