Hello
I want to backup my folders and datacenter from my old VIServer.
I found a very usefull script for that but it dosent show me the Datacenters:
filter Get-FolderPath {
$_ | Get-View | % {
$row = "" | select Path
$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
}
}
$report = Get-VM | Get-Folderpath
$report | Export-Csv "c:\VM-with-FolderPath.csv" -NoTypeInformation
Is there a way to also write the datacenters in that csv sheet?
My next problem is that i want to creat new folders and also folders and Datacenters under these in my new VIServer.
There was also a usefull script i found: http://communities.vmware.com/message/1334740#1334740
But in that script "just" moves the already createt VMS in the createt folders.
And i have no idea how i can modify that script to creat folders and datacenters for me.
It would be great if there is an solution for my problem.
Hi,
You can try this to create datacenters and folders in other folder
$dc = New-Datacenter <nameof DC> -Location ( Get-Folder -Name <nameof folder>) $folder = New-folder <nameof folder> -Location ( Get-Folder -Name <nameof folder>)
And to export in csv file try this
$report = @() $report = Get-Folder -NoRecursion | Get-View | % { get-children $_ "" }|Export-Csv "c:\report.csv" -NoTypeInformation
Hope this helps.
Can you try the following scripts.
First the script that writes the paths to a CSV file
$global:report = @() function get-children($entity,$path){ if(!("Datacenters","vm" -contains $entity.Name)){ $path += ("\" + $entity.Name) } $children = $entity.ChildEntity if($entity.ChildEntity -eq $null){ $children = $entity.VmFolder } foreach($child in $children){ $childfld = Get-View -Id $child switch($childfld.gettype().name){ "Folder" { if($childfld.Name -ne "vm"){ $row = "" | select Path, Type $row.Path = ($path + "\" + $childfld.Name) $row.Type = "Folder" $global:report += $row } get-children $childfld $path } "VirtualMachine"{ } "Datacenter"{ $row = "" | select Path, Type $row.Path = ($path + "\" + $childfld.Name) $row.Type = "Datacenter" $global:report += $row get-children $childfld $path } } } } Get-Folder -NoRecursion | Get-View | % { get-children $_ "" } $global:report | Export-Csv "C:\Folder-Export.csv" -NoTypeInformation
This script will create the Datacenter and Folder entries from the CSV file.
function Get-ViLocation{ param($path) $loc = Get-Folder -NoRecursion $qualifiers = $path.Split("\") if($qualifiers.Count -gt 2){ for($i = 1; $i -lt ($qualifiers.Count - 1); $i++){ $loc = Get-Inventory -Name $qualifiers[$i] -Location $loc } } $loc } $list = Import-Csv "C:\Folder-Export.csv" | Sort-Object -Property Path foreach($entry in $list){ $operation = "New-" + $entry.Type $name = $entry.Path.Split("\")[-1] $loc = Get-ViLocation $entry.Path $dummy = & $operation -Name $name -Location $loc -WhatIf }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference