VMware Cloud Community
munizaba
Contributor
Contributor
Jump to solution

Folder migration

We are in the process of decommissioning one of the VC. I'm going to move all ESX host and VMs to new Virtual Centre. Only problem I have is the folders migration. (we have 300 VMs that are spread in some 20-40 folders)

Is there a way that I can create script that will place VMs in correct folder. I was thinking something like I can export VM folder membership to file and use that file to populate folders on new VC.

Thank,

DM

24 Replies
zenivox
Hot Shot
Hot Shot
Jump to solution

Hi Luc, did you have any chance to have a look at that?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I had a look, some things to do

Update your CSV file, it should have a number of strings separated by commas.

Like this

"Name","Path"

"VM1","Bldg 178\Bldg 178\VM1"

"VM2","Bldg 178\dir1\VM2"

"VM3","Bldg 178\dir1\VM3"

"-xsapt1n","Bldg 178\dir1\-xsapt1n"

"-xdnsb01","Bldg 178\dir1\-xdnsb01"

"-xsapsbxdm","Bldg 178\dir1\-xsapsbxdm"

"-x-yb02","Bldg 178\dir1\-x-yb02"

"-x-yb01","Bldg 178\dir1\-x-yb01"

"-xproxyb01","Bldg 178\dir1\-xproxyb01"

"-xsapT1Ndb","Bldg 178\dir1\-xsapT1Ndb"

"edc-legprx-01","Bldg 178\dir1\edc-legprx-01"

"-xisfa-x","DC2\AD Child Domains\-xisfa-x"

"-xsom-x","DC2\AD Child Domains\-xsom-x"

"-xgsc-x","DC2\AD Child Domains\-xgsc-x"

"-xrcca-x","DC2\AD Child Domains\-xrcca-x"

"-xdof-x","DC2\AD Child Domains\-xdof-x"

"-xsbv-x","DC2\AD Child Domains\-xsbv-x"

"-xtso-x","DC2\AD Child Domains\-xtso-x"

For the script, try like this

$hashTab = @{}

$folder = Get-View -ViewType Folder

$folder += (Get-View -ViewType Datacenter)

$folder | where {'host','vm','datastore' -notcontains $_.Name} | % {

$current = $_

$path = ""

while($current.Parent -ne $null){

$parent = $current

if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}

$current = Get-View $current.Parent

}

$hashTab[$path] = $_

}

$VMfolder = Import-Csv "C:\Users\xxxx\xxxx\VM-BR-FolderPath.csv" | Sort-Object -Property Path

foreach($guest in $VMfolder){

$key = $guest.Path.Substring(0, $guest.Path.LastIndexOf("\") + 1)

Move-VM -VM (Get-VM $guest.Name) -Destination (Get-VIObjectByVIView -MORef $hashTab[$key].MoRef) -WhatIf

}


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

0 Kudos
GabCava
Enthusiast
Enthusiast
Jump to solution

Thanks Luc! The behaviour is quite different now. I had to remove the quotation marks from Name and Path in the csv file. However when it detects a VM the Powercli output is positive (see screenshot attached) but in the vCEnter server it does not move anything. No output in the vSphere client either. I can assure you I logged in Powercli as VI administrator. Thanks again for your time!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's because of the WhatIf switch you used.

The Move-VM cmdlet tells you what it will do, without actually executing it.


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

GabCava
Enthusiast
Enthusiast
Jump to solution

Luc it's a pleasure to see it running and working! Many thanks again! Really appreciated!!

0 Kudos