VMware Cloud Community
jessem
Enthusiast
Enthusiast

vCenter folders

Hello,

I am looking to move hosts from one vCenter to another vCenter.  All the VMs in the old vCenter have a folder hierarchy that I want to transfer to the new vCenter.  Any way to automate or do a script where once I move a host and the VMs, I can run a script to organize the VMs into their respective folders just like on the old vCenter?

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

Maish did a nice migration script, including folders.

See vCenter PowerCLI Migration Script


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

Reply
0 Kudos
LittleNickey
Enthusiast
Enthusiast

Looks like there is a function to copy folder structure in Mark's script, he also seems to copy permission structures.

-- Oskar
Reply
0 Kudos
jessem
Enthusiast
Enthusiast

I read through the scripts but still am lost a bit I guess.

I am so novice on this topic.  I am trying to figure out how I can accomplish the following.

Moving one cluster of hosts and VMs from one vCenter to the new one (5.0 to 5.5) and arranging the VMs back into their respective folders on the new side.  I don't mind having to manually create all the folders in the new vCenter.  I just don't want to have to drag and drop all of the VMs and sort.  I'm not worried about permissions, HA/DRS settings, etc.  I just want to be able to script the moving of VMs into their folders.

Any help would be appreciated.

Reply
0 Kudos
LittleNickey
Enthusiast
Enthusiast

Okey, here's a simple script that moves the entire host from one VC to another, creates the folder if it doesn't exist and then moves the VM into the folder.

The script "assumes" that you don't have an elaborate folder structure with subfolders etc and will create all missing folders in the root folder of the DC.

For example; you will probably get an error if you have 2 folders with the same name, e.g. folders "vm\IT\Servers" and "vm\HR\Servers".

## Parameters

$SourceVC = "vc1.local"

$TargetVC = "vc2.local"

$HostToMigrate = "ESX1.local" ## Or you can do a foreach host in cluster loop, let me know if you need help with that

$ESXUser = "root"

$ESXPassword = "SomeSecretPassword"

$TargetCluster = "Cluster1"

$TargetDC = "TargetDC"

## Connect VCs

Connect-VIServer -Server $SourceVC,$TargetVC

## "Export" the folder info

$FolderInfo = @() ## Create hashtable to save info in

foreach($VM in (Get-VMHost $HostToMigrate -Server $SourceVC | Get-VM -Server $SourceVC)){

    $Info = "" | Select VMName, Folder

    $Info.VMName = $VM.Name

    $Info.Folder = $VM.Folder ## Note that this can be a subfolder, it's the actual folder the VM is placed in.

    $FolderInfo += $Info

}

## Disconnect host and remove it from SourceVC

Set-VMHost -VMHost $HostToMigrate -State Disconnected -Server $SourceVC

Remove-VMHost -VMHost $HostToMigrate -Server $SourceVC

## Add host to TargetVC

$Cluster = Get-Cluster -Name $TargetCluster -Server $TargetVC

Add-VMHost -Name $HostToMigrate -Location $Cluster -Server $TargetVC -User $ESXUser -Password $ESXPassword -Force

## Loop the "exported" info

$DC = Get-Datacenter -Name $TargetDC -Server $TargetVC

foreach($row in $FolderInfo){

    ## Create folder if it doesn't exist

    if(!($TargetFolder = Get-Folder -Name $row.Folder -Type VM -Server $TargetVC)){

     New-Folder -Name $row.Folder -Location $DC.GetVmFolder() -Server $TargetVC

        ## Note that this creates all folders in the root folder, no subfolders are taken into account.

    }

    ## Move VM to correct folder

    $TargetFolder = Get-Folder -Name $row.Folder -Type VM -Server $TargetVC

    Move-VM -Server $TargetVC -VM $row.VMName -Destination $TargetFolder -Confirm:$False | Out-Null

}

-- Oskar
Reply
0 Kudos