Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 258290

Re: Register VMs in a datastore in the vCenter inventory

$
0
0

Try it like this

 

$Cluster = "MyCluster" 
$DatastoreName = "MIRROR*"
$VMFolder = "DR_Folder"
$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1

foreachforeach
($Datastore in (Get-Datacenter "MyDC" | Get-Datastore -Name $DatastoreName) {   # Set up Search for .VMX Files in Datastore   $ds = Get-View $Datastore.Id
  $SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
  $SearchSpec.matchpattern = "*.vmx"
 
$dsBrowser = Get-View $ds.browser
 
$DatastorePath = "[" + $ds.Summary.Name + "]"
  # Find all .VMX file paths in Datastore, filtering out ones with .snapshot   $SearchResult = $dsBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec) | where {$_.FolderPath -notmatch ".snapshot"} | %{$_.FolderPath + ($_.File | select Path).Path}   #Register all .vmx Files as VMs on the datastore   foreach($VMXFile in $SearchResult) {     $vm = New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $VMFolder -ErrorAction SilentlyContinue
    if($vm){       Get-hardDisk -VM $vm | where {$_.CapacityKB -eq 0} | Remove-HardDisk -Confirm:$false
      $newname = $vm.Name + "_Copy"
      Set-VM -VM $vm -Name $newname -confirm:$false
      if($vm.PowerState -ne "PoweredOn"){         $vm = Start-VM -VM $vm -ErrorAction SilentlyContinue
      }       Get-VMQuestion -VM $vm | Set-VMQuestion -Option "I moved it" -Confirm:$false
    }   } }

Viewing all articles
Browse latest Browse all 258290

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>