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..
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
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 :
VM | VMIP1 | VMIP2 | Datacenter | Cluster | Host | Hard Disk | Datastore | LUN | LUNID | LUNWWN | VMConfigFile | VMDKpath | VMDK Size | Drive Size |
testvm | 10.16.168.228 | 10.50.45.102 | testdc | 10.50.37.204 | Hard disk 1 | SAN_CX4_960_0085_OS_LUN1_WAVE1 | naa.6006016002da2700dc197ab6a024e011 | 0 | 50060160bb2059a2 | [SAN_CX4_960_0085_OS_LUN1_WAVE1] testvm/test.vmx | [SAN_CX4_960_0085_OS_LUN1_WAVE1] test/test.vmdk | 30 | 30 | |
testvm | 10.16.168.228 | 10.50.45.102 | testdc | 10.50.37.204 | Hard disk 2 | SAN_CX4_960_0085_OS_LUN1_WAVE1 | naa.6006016002da2700dc197ab6a024e011 | 0 | 50060160bb2059a2 | [SAN_CX4_960_0085_OS_LUN1_WAVE1] testvm/test.vmx | [SAN_CX4_960_0085_OS_LUN1_WAVE1] test/test1.vmdk | 69.02 | 69.02 | |
testvm | 10.16.168.228 | 10.50.45.102 | testdc | 10.50.37.204 | Hard disk 3 | SAN_CX4_960_0085_OS_LUN1_WAVE1 | naa.6006016002da2700dc197ab6a024e011 | 0 | 50060160bb2059a2 | [SAN_CX4_960_0085_OS_LUN1_WAVE1] testvm/test.vmx | [SAN_CX4_960_0085_OS_LUN1_WAVE1] test/test2.vmdk | 20 | 20 |
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
thanks LucD ill gothrough the above links and get back to you...
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 :
Name | ResourcePool | Folder | VMXPath |
testvin1 | Resources | vm | [fcsantestdatastore] testvin1/testvin1.vmx |
testvin2 | Resources | vm | [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..
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
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.