VMware Cloud Community
vin01
Expert
Expert

search for vmx in datastore and register as vm

Hi

I thank LucD for providing the below script to grab for vmx file and register as vm...In my scenario one of the storage box Luns are connected to multiple cluster there is maintenance activity for that storage so we are powering off all the vms which resides on that storage and unregistering that vms and unmounting\detaching the stoarge..So after completing the activitiy i need to register all the vms back to the vcenter on there respective clusters where they resides previously..I found the below script helpful...

$Cluster = Get-Cluster

$Datastores = Get-datastore "datastore 1","datastore 2"

$VMFolder = "MyFolder"

$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1

foreach($Datastore in Get-Datastore $Datastores) {

  # Collect .vmx paths of registered VMs on the datastore

  $registered = @{}

  Get-VM -Datastore $Datastore | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | %{$registered.Add($_.Name,$true)}}

   # Set up Search for .VMX Files in Datastore

  New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null

  $unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `

    where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.Name)})

  Remove-PSDrive -Name TgtDS

   #Register all .vmx Files as VMs on the datastore

   foreach($VMXFile in $unregistered) {

      New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync

   }

}

-==========================================

I above script i have only datastore names..Vms in that datastore should be registered on different clusters where that datastore is mapped and i dont know vm folder name...How can i modify the above script to search all folders in that datastore and get that vmx. and register on any host in the clusters where they are previously..

Regards Vineeth.K
0 Kudos
7 Replies
LucD
Leadership
Leadership

Wouldn't it be easier to store the information (VM - Datastore - Lun - VMHost - Cluster - Folder) in a file before actually unregistering the VMs ?

Once the LUNs are back, you can use that information to register the VMs again.


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

0 Kudos
vin01
Expert
Expert

Yes LucD i have below information but i don't have folder information..Can you provide me script to get folder information for vms..and after luns are back how to import .vmx file from .csv file to register them.

Sample info i collected :

VMVMIP1VMIP2DatacenterClusterHostHard DiskDatastoreLUNLUNIDLUNWWNVMConfigFileVMDKpathVMDK SizeDrive Size
testvm10.16.168.22810.50.45.102testdc10.50.37.204Hard disk 1SAN_CX4_960_0085_OS_LUN1_WAVE1naa.6006016002da2700dc197ab6a024e011050060160bb2059a2[SAN_CX4_960_0085_OS_LUN1_WAVE1] testvm/test.vmx[SAN_CX4_960_0085_OS_LUN1_WAVE1] test/test.vmdk3030
testvm10.16.168.22810.50.45.102testdc10.50.37.204Hard disk 2SAN_CX4_960_0085_OS_LUN1_WAVE1naa.6006016002da2700dc197ab6a024e011050060160bb2059a2[SAN_CX4_960_0085_OS_LUN1_WAVE1] testvm/test.vmx[SAN_CX4_960_0085_OS_LUN1_WAVE1] test/test1.vmdk69.0269.02
testvm10.16.168.22810.50.45.102testdc10.50.37.204Hard disk 3SAN_CX4_960_0085_OS_LUN1_WAVE1naa.6006016002da2700dc197ab6a024e011050060160bb2059a2[SAN_CX4_960_0085_OS_LUN1_WAVE1] testvm/test.vmx[SAN_CX4_960_0085_OS_LUN1_WAVE1] test/test2.vmdk2020
Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

Have a look at Folder by Path, in that post there is New-VIProperty called BlueFolderPath.

Have a look at Script to migrate VMs from one cluster to another, that discusses a similar scenario


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

0 Kudos
vin01
Expert
Expert

thanks LucD ill gothrough the above links and get back to you...

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert

with the help of above links and from other blogs..I Used the script as below..But while importing it is throwing errors can you help me out..

==============

$cluster = Get-Cluster "testcluster","testcluster1"

$inventory = Get-Cluster $cluster | Get-VM "testvin1","testvin2" |

Add-Member -MemberType ScriptProperty -Name 'VMXPath' -Value {$this.extensiondata.config.files.vmpathname} -Passthru -Force |

Select-Object Name,ResourcePool,Folder,VMXPath

$inventory |Export-Csv getvmfolderpathtest.csv -NoTypeInformation

==============

output :

NameResourcePoolFolderVMXPath
testvin1Resourcesvm[fcsantestdatastore] testvin1/testvin1.vmx
testvin2Resourcesvm[fcsantestdatastore] testvin2/testvin2.vmx

==========================

$inventory = Import-Csv getvmfolderpathtest.csv

======

after that used below to import.ps1

$cluster = Get-Cluster "testcluster","testcluster1"

foreach($vm in $inventory){

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

  New-VM -VMFilePath $vm.VMXPath -VMHost $ESXHost -Location $vm.Folder -ResourcePool (Get-Cluster $cluster)

}

====================

Showing below error..

error.jpg

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

It looks as if the value you pass to the Location parameter ($vm.Folder) comes back with more that 1 object.

So you probably have folders with the same name in multiple locations in your vSphere environment.

If you know the path to the intended folder, you might use the function from my Folder by Path post.


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

0 Kudos
vin01
Expert
Expert

Thanks LucD for update..I found some way to register multiple vms as below..This helped me a lot..

New-VM -Name test1 -VMHost 10.xx.xx.xx -VMFilePath “[SAN_datastore] test1/test1.vmx“ >> D:\\VM-Reg-Out.txt

New-VM -Name test2 -VMHost 10.xx.xx.xx -VMFilePath “[SAN_datastore] test2/test2.vmx“ >> D:\\VM-Reg-Out.txt

kept in .ps1 file and executed..It worked.

Regards Vineeth.K
0 Kudos