VMware Cloud Community
vespavbb
Enthusiast
Enthusiast

Export Import Folder Structure with vm´s

Hello,

there are some Topics arround, but I want to ask...

I try to export Folders and VM Location on Folder to csv and import them on a differen vcenter.

Export looks ok,

-But import does only create the first Folders and all subfolders are missing.

-The VM move to destination folder is not wokring as well, problem is the subfolder as well.

I found the cheap disaster scripts....

ps scripts

1. export folderstructure

2. export vm Folder information

3. import Folderstructre

4. move vm´s to Folder

1

$sourceVI ="test"

$datacenter = "test"

$creds = Import-Clixml -Path "test"

connect-viserver -server $sourceVI -Credential $creds

filter Get-FolderPath {

    $_ | Get-View | % {

        $row = "" | select Name, Path

        $row.Name = $_.Name

        $current = Get-View $_.Parent

        $path = $_.Name

        do {

            $parent = $current

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

            $current = Get-View $current.Parent

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

        $row.Path = $path

        $row

    }

}

## Export all folders

$report = @()

$report = get-datacenter $datacenter -Server $sourceVI| Get-folder vm | get-folder | Get-Folderpath

        ##Replace the top level with vm

        foreach ($line in $report) {

        $line.Path = ($line.Path).Replace($datacenter + "\","vm\")

        }

$report | Export-Csv "C:\folders\04-$($datacenter)-Folders-with-FolderPath.csv" -NoTypeInformation

Disconnect-VIServer "*" -Confirm:$False

2

$sourceVI ="test"

$datacenter = "test"

$creds = Import-Clixml -Path "test"

connect-viserver -server $sourceVI -Credential $creds

filter Get-FolderPath {

    $_ | Get-View | % {

        $row = "" | select Name, Path

        $row.Name = $_.Name

        $current = Get-View $_.Parent

        $path = $_.Name

        do {

            $parent = $current

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

            $current = Get-View $current.Parent

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

        $row.Path = $path

        $row

    }

}

##Export all VM locations

$report = @()

$report = get-datacenter $datacenter -Server $sourceVI| get-vm | Get-Folderpath

$report | Export-Csv "\folders\05-$($datacenter)-vms-with-FolderPath.csv" -NoTypeInformation

Disconnect-VIServer "*" -Confirm:$False 

3

$creds = Import-Clixml -Path "test

$destVI = "test"

$datacenter = "test"

connect-viserver -server $destVI -Credential $creds

##IMPORT FOLDERS

$vmfolder = Import-Csv "folders\04-$($datacenter)-Folders-with-FolderPath.csv" | Sort-Object -Property Path

foreach($folder in $VMfolder){

    $key = @()

    $key =  ($folder.Path -split "\\")[-2]

    if ($key -eq "vm") {

        get-datacenter $datacenter -Server $destVI | get-folder vm | New-Folder -Name $folder.Name

        } else {

        get-datacenter $datacenter -Server $destVI | get-folder vm | get-folder "$key" | New-Folder -Name $folder.Name

       

        }

}

Disconnect-VIServer "*" -confirm:$false

   

4

$sourceVI ="test"

$datacenter = "test"

$creds = Import-Clixml -Path "test"

connect-viserver -server $sourceVI -Credential $creds

# move the vm's to correct location

$VMfolder = @()

$VMfolder = import-csv "folders\04-$($datacenter)-vms-with-FolderPath.csv" | Sort-Object -Property Path

foreach($guest in $VMfolder){

$key = @()

$key =  Split-Path $guest.Path | split-path -leaf

if ($key -eq $datacenter) {

Write-Host "Root folder $guest.path"

##

Move-VM (Get-VM $guest.Name) -Destination "vm"

}

else

{

Move-VM (Get-VM $guest.Name) -Destination (Get-folder $key)

}

}

Disconnect-VIServer "*" -Confirm:$False

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
41 Replies
LucD
Leadership
Leadership

I would need to see the file, or at least some lines, of the folder export CSV


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

can you run the export script on your lab? In my export are vms with  fqdn..

the export is easy and looks ok..

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

Sure, in my small test environment it seems to work ok.

The exported folders

"Name","Path"

"Discovered virtual machine","vm\Discovered virtual machine"

"Linux","vm\Linux"

"Photon","vm\Linux\Photon"

"Ubuntu","vm\Linux\Ubuntu"

The exported VMs

"Name","Path"

"W2K4","DC\W2K4"

"PhotonPS2","DC\PhotonPS2"

"UbuntuBionicPS","DC\Linux\UbuntuBionicPS"

"PhotonPS","DC\Linux\PhotonPS"


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

Thanks for your help Luc,

are you able to import them, for example on a different vcenter... just the folders

The Datacenter name could be different,

two problems..

I was only able to import the first level of folders and all subfolders are missing.

in the import it looks like the folder creating  is expecting a root folder instead a datacenter

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

Yes, seems to work for me.


But there are a couple of restrictions on script 3 & 4.

Is there only 1 datacenter in the VCSA?
Are there multiple folders with the same name, but in different locations in the vSphere hierarchy?
Newer PowerCLI version need to use the InventoryLocation parameter instead of Destination to indicate a folder of type VMandTemplates


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

your kidding, it works? even with subfolders?

yes on the the destination vcsa are more datacenters (2)

I tried the inventorylocation parameter es well, but same problem

I do have some Folders with special characters like this, maye the problem comes from something like that?

Name ,                                   Path

ESQ R23 [RISO SAP ADS], DAS APP\ESQ R23 [RISO SAP ADS]

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

If you could give me some examples (anonymised) of folder paths and VM names, I could try to simulate


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

Folder Export CSV looks like this, here are some lines...  

NamePath
app P68 [RISO app ADS]app\app P68 [RISO app ADS]
CI1app\app PI1 [RISO PI]\CI1
QBWapp\app PBW [Firma BW]\QBW
PBWapp\app PBW [Firma BW]\PBW
ABWapp\app PBW [Firma BW]\ABW
VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

I assume there is also a line for "app"?
And why is the hidden "vm" folder not in the Path?


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

Hi,

thats all in the list...

for example. I guess it is hard to read because of the format

Foldername     DC\Foldername\Subfoldername

ABWapp\app PBW [Firma BW]\ABW
VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
vespavbb
Enthusiast
Enthusiast

but maybe the answer is, How could I export the foldernames special character awareness?

and for the import as well?

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

Are those the Folder exports or the VM exports?

The Folder exports should have the hidden 'vm' folder in the Path


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

thats the Folder exports. but there is no vm folder in the path. You mean the name of the VM where it belongs to? this is in the onther export file

Name of the FolderPath where the folder should be created
CI1app\app PI1 [RISO PI]\CI1
QBWapp\app PBW [Firma BW]\QBW
PBWapp\app PBW [Firma BW]\PBW
ABWapp\app PBW [Firma BW]\ABW
VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

No, I mean the hidden 'vm' folder at the base of every folder of type VMandTemplates.

Look at my earlier example, the 1st part, the Folder export.


You are talking about folders that contain VMs right?


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

good morning,

Ok, now I understand.

No there are no 'vm' folders at the base. even in folder where  vm are containing and empty folders as well,

Yes, I have empty folders in the structure, but I need all 🙂

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

Then it looks as if the Folder export already goes wrong.


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

mm ok, could be.

but I dont see any mistake

$report = @()

$report = get-datacenter $datacenter -Server $sourceVI| Get-folder vm | get-folder | Get-Folderpath

        ##Replace the top level with vm

        foreach ($line in $report) {

        $line.Path = ($line.Path).Replace($datacenter + "\","vm\")

        }

$report | Export-Csv "C:\folders\04-$($datacenter)-Folders-with-FolderPath-test.csv" -NoTypeInformation

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

From what you have shown me, it looks as if that Replace didn't work


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

0 Kudos
vespavbb
Enthusiast
Enthusiast

I´m not able to do it..

the challenge is, the import is on a different vcenter with different DC Name as well.

this is not working for me too.

Export/Import Folder Structure

maybe I give up

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos