VMware Cloud Community
shaka
Enthusiast
Enthusiast
Jump to solution

migrating VMs and folders from one datacenter to another datacenter.

So I found a script online that migrates your inventory over from one vCenter to another. I broke the script into two peices one to export (folder path, VM folder path, folder permissions and notes & custom attributes) and one to import (create folders, move VMs to original folder, reset permissions and set notes & custom attributes).

The export script works fine. I am having issues with the import script. If I have duplicate folder name in the tree structure the tree structure will get duplicated under each similar folder. i.e.

Exported Folder structure

Portuagal-->football

Spain>football>soccer

Imported Folder Structure

Portugal>football>soccer

Spain>football>soccer

I am guessing that I need to somehow figure out the folder ID but don't have a clue where to start.

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I rewrote the import folders section of the script to prevent the duplicated folder problem. The new script finds the parent folder by starting to look at the root folder instead of just the folder above where it should be created. Because the entire path is in the .csv file this is possible. The next script should work correct now:

##IMPORT FOLDERS 
$vmfolder = Import-Csv "c:\Folders-with-FolderPath-$($datacenter).csv" | Sort-Object -Property Path 

foreach($folder in $VMfolder){ 
	$Path = $folder.Path.split("\")
	$PathLength = $Path.Length
	$SubFolder = Get-Datacenter $datacenter | Get-Folder vm
	For ($i= 1; $i -lt $PathLength-1; $i++) {
	   $SubFolder = Get-Folder -Location $Subfolder -Name $Path[$i]
	}
	New-Folder -Name $folder.Name -Location $SubFolder
}

Because the forum software has problem with square bracket and the script contains them, I have attached the changed vCenter_migration_import.ps1 script.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
11 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I rewrote the import folders section of the script to prevent the duplicated folder problem. The new script finds the parent folder by starting to look at the root folder instead of just the folder above where it should be created. Because the entire path is in the .csv file this is possible. The next script should work correct now:

##IMPORT FOLDERS 
$vmfolder = Import-Csv "c:\Folders-with-FolderPath-$($datacenter).csv" | Sort-Object -Property Path 

foreach($folder in $VMfolder){ 
	$Path = $folder.Path.split("\")
	$PathLength = $Path.Length
	$SubFolder = Get-Datacenter $datacenter | Get-Folder vm
	For ($i= 1; $i -lt $PathLength-1; $i++) {
	   $SubFolder = Get-Folder -Location $Subfolder -Name $Path[$i]
	}
	New-Folder -Name $folder.Name -Location $SubFolder
}

Because the forum software has problem with square bracket and the script contains them, I have attached the changed vCenter_migration_import.ps1 script.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
YoMarK
Contributor
Contributor
Jump to solution

Tnx, Guys! Saved me a lot of mouseclicks today. Smiley Happy

0 Kudos
shaka
Enthusiast
Enthusiast
Jump to solution

Hi, I discovered one more issue regarding duplicate folder names. The section that migrates the permissions errors out when assigning the permissions to a folder that has a duplicate folder name in the tree. How can that be fixed?

0 Kudos
ggochkov
VMware Employee
VMware Employee
Jump to solution

Hi shaka,

If there is more than one folder with the specified name then "get-folder $perm.Foldername" returns more than one object and that's why the script fails.

There is a workaround using the FolderPath field that should be included in the import-permission csv - replace the import permission lines with:



# you should include FolderPath record in the CSV file for permissions and of course assume the Foldername is the entity name (in case of VM it will be the VM name)
$permissions = @()
$permissions = Import-Csv "c:\perms-$($datacenter).csv" 

foreach ($perm in $permissions) { 
$entity = $null

switch -wildcard ($perm.EntityId)     { 
Folder* {
# in case of folder use the FolderPath record to get the folder object
$Path = $perm.FolderPath.split("\")
$PathLength = $Path.Length
$SubFolder = Get-Datacenter $datacenter | Get-Folder vm
For ($i= 1; $i -lt $PathLength-1; $i++) {
$SubFolder = Get-Folder -Location $Subfolder -Name $Path[$i]
}
$entity = Get-Folder -Name $folder.Name -Location $SubFolder
} 
VirtualMachine* {  
$entity = get-datacenter $datacenter | Get-vm $perm.Foldername
}
}     

$role = Get-virole $perm.Role
if ($perm.propagate -eq "True") {
$propagate = $true 
} else { 
$propagate = $false
} 
$setperm = new-vipermission -Entity $entity -Role $role -Principal $perm.Principal -Propagate $propagate
}



Thanks,

Gospodin!

A_Mikkelsen
Expert
Expert
Jump to solution

To be 100% sure not to get a error if an parents subfolder contains a folder with the same as the one you are trying to create add " -NoRecursion" to the end of

<code>$SubFolder = Get-Folder -Location $Subfolder -Name $Path[$i] </code>

so it looks like

<code>$SubFolder = Get-Folder -Location $Subfolder -Name $Path[$i] -NoRecursion
</code>

If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points.

Regards

A. Mikkelsen

If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points. Regards A. Mikkelsen
0 Kudos
greendx
Contributor
Contributor
Jump to solution

I'm running this export script on a couple of different data centers. While it runs without any errors on one of my data centers it gives me the error below on my second data center. It looks like I'm getting most or all of the expected output but I do have many instances of the error message below. Anyone have any ideas?

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:\Powershell_Scripts\41_Upgrade\vc-export.ps1:20 char:20

+ $current = Get-View <<<< $_.Parent

+ CategoryInfo : InvalidData: (Smiley Happy , ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView

0 Kudos
shaka
Enthusiast
Enthusiast
Jump to solution

I am guessing that this is occurring at the top level of the datacenter

tree (on the datacenter name) and there is no parent ID for that item.

I will take a look into when I have some time during this week and let

you know if I find a cause/resolution.

Javed Husain

0 Kudos
Matt_B1
Enthusiast
Enthusiast
Jump to solution

Great script!  I made some minor changes that might help others.

  • Store this command "get-datacenter $datacenter | get-vm" in a variable to avoid it running 3 separate times
  • If you use the DataCenter name in one of your folders (ex: DCX VMs), it will get re-written when he does the replacement for the future import
    • I changed the line to "$line.Path = ($line.Path).Replace("\" + $datacenter + "\","\vm\") " to avoid that
  • The Notes line uses the wrong logic and should be "if (($vm | Select Notes) -ne $null){" instead of -eq
  • For the custom attributes, some people may have more than 2 attributes so be sure to update the Key and Value sections to add more lines
0 Kudos
Gdriver
Contributor
Contributor
Jump to solution

Hi,

I tried the script but ran into different errors.

script is from http://daverdave.com/node/130

exporting the permissions from a VirtualCenter 2.5 was no problem. But i cant import them to the new vcenter in vsphere 4.

You cannot call a method on a null-valued expression.
At D:\test\import_migrate-vcenter.ps1:179 char:103
+              $entity.value = ((get-datacenter $datacenter | get-folder $perm.Foldername).ID).Trimstart <<<< ("Folder-")
    + CategoryInfo          : InvalidOperation: (Trimstart:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Method invocation failed because [System.Object[]] doesn't contain a method named 'SetEntityPermissions'.
At D:\test\import_migrate-vcenter.ps1:201 char:35
+     $doactual.SetEntityPermissions <<<< ($entity, $setperm)
    + CategoryInfo          : InvalidOperation: (SetEntityPermissions:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

----------

Get-Folder : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null or empty and then try the command ag
At D:\temp\import_migrate-vcenter.ps1:179 char:71
+              $entity.value = ((get-datacenter $datacenter | get-folder <<<<  $perm.Foldername).ID).Trimstart("Folder-")
    + CategoryInfo          : InvalidData: (:) [Get-Folder], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetFolder

I´m a noob in powercli, so I wasted really hours to find whats wrong and didn´t get a bit forward.  folder for folder by hand would have been faster now...

i have dublicate folders (like   TEST\CITRIX\VM  ;  INTEGRATION\CITRIX\VM ;  PRODUCTION\CITRIX\VM  ....)   so the other migration scripts with XML output where just wasted time, already tried and the migration was incomplete, so i tried to find out whats wrong with this script.

I just want to get folders, folderpermissions, vm permissions

maybe someone could bring a bit of light to the darkness here.

0 Kudos
trevor12087
Contributor
Contributor
Jump to solution

Hi,

I haven't used the new-folder cmd much, so I was wondering if anyone can tell me this:

If I have a datacenter that has already been partially migrated, and I want to run this script to create  the folders for everything that has not been migrated, what will happen when this command is called for a folder that is already created, and has VMs in it? Will it overwrite anything? Will it produce any error? Or will it just move on?

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It will generate an exception.

You can use a Try-Catch construction to handle this


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

0 Kudos