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.
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
Did you already check out Maish's migration script.
See his vCenter PowerCLI Migration Script post.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That looks really nice!
But i have 1 Qestion to that script. I didn't undersatnd this line:
$datacenter = Read-Host "Please give the name of the datacenter you would like to run against"
I want all datacenters (witch are locatet under my folders) to be in the csv file.
What do i have to fill in there?
Thanks a lot up to now.
Maish developed the script to do the migration one datacenter at the time.
It's not straightforward to change the script to handle all datacenters in one run I'm afraid.
But you could create a stub around Maish's script where you loop through all the datacenters.
And replace the interactive prompts with assignments
Something like this:
Get-Datacenter | %{ # Maish's script ... $sourceVI = "srvViName" $destVI = "dstViName" ... $datacenter = $_.Name ... # End of Maish's script }
Does this make sense ?
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Okay i tried it with just 1 Datacenter now.
There are 4 Datacenters with the same name in different folders.
Two of them are direct under 1 folder (that one does the script get)
And the other two are under 2 folders locatet and that one aren't listed in the Folders-with-FolderPath sheet.
Do you have an solution for that?
EDIT: Okay i am sorry. It does get the datacenter which is located under 2 folders. But there are 4 of them and it just gets 2.
There is no differents. And i don't know atm why it doesn't list me the other 2.
Message was edited by: MoRobin
You can go to each specific datacenter by using the entity just above (provided that one is unique).
Assume that there are 2 datacenters with the same name "dc1" , one under the folder "folder1" and the other under the folder "folder2".
Then you can do
# Get the DC under folder1 Get-Datacenter -Folder "folder1" -Name "dc1" # Get the DC under folder2 Get-Datacenter -Folder "folder2" -Name "dc1"
But I'm afraid it won't be straightforward to adapt the script accordingly.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I didn't really get what you mean.
That is my code ATM:
$sourceVI = "xxxx"; $datacenter = "yyyy" 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:\new\Folders-with-FolderPath-$($datacenter).csv" -NoTypeInformation {color:#339966}That list that is exportet here is just showing me the first and the last Datacenter with the name yyyy{color} ##Export all VM locations $report = @() $report = get-datacenter $datacenter -Server $sourceVI| get-vm | Get-Folderpath $report | Export-Csv "c:\new\vms-with-FolderPath-$($datacenter).csv" -NoTypeInformation
Where do i have do edit something when i just want all datacenters to be shown in that list?
That would require an important re-write of Maish's script I'm afraid.
I checked again, and the first script in this thread does include the datacenter in the path.
Or didn't that work for you ?
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If you want to list all the datacenters you should change:
get-datacenter $datacenter -Server $sourceVI
into:
get-datacenter -Server $sourceVI
on two places in the script.
The very first script i posted is just showing me the folders and the VMs under that folders but no datacenters.
That's strange, in my environment the datacenter name is in the path for each vm.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
okay some strange things:
I done the whole script know in with all datacenters (delete that one variable)
It worked without any error.
BUT: after every folder it post me xxxvm\yyyvm\
I see the line in my script which does that but i dont know for what that is important.
Much more annoying is that it doesnt list me all folders.
And it always is saying Discovered Virtual Machine.
In the Folders-with-FolderPath file every line is looking like that:
Discovered Virtual Machine,"XXXXvm\YYYYvm\Discovered Virtual Machine" |
And of course there are just 15 entries and i have much more than 15 folders.
Could it be that you are talking about the yellow folders (the Hosts and Clusters view) while the script lists the blue folders (the VMs and Templaes view) ?
Have a look at my script in , it shows both lists (yellow and blue folders).
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The folders are looking like a blue building and called datacenter.
Structure:
Folder1-> Folder2-> Datacenter-> Cluster-> Hosts/VMs
Even if that Datacenters are such blue folder shouldn't the script list me all folders without that blue ones?
OK
No Datacenters fine...
But could that script just list me the folders which are containing Datacenters and creat them in my new VIServer?
Questions to the code:
Why does it write "Discovered Virtual Machine"in the Folders-with-FolderPath csv?
For what is that VM\ at the end?
The most important question is: Why does it just list me a few Folderpaths and not all?
Where i am ATM:
#load Vmware Module Add-PSSnapin VMware.VimAutomation.Core #Change to multi-mode vcenter management Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false #Get vCenter Server Names $sourceVI = "VIServer"; $creds = get-credential $datacenter = "OneDatacenter" #Connect to Source vCenter 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-with-FolderPath-$($datacenter).csv" -NoTypeInformation ##Export all VM locations $report = @() $report = get-datacenter $datacenter -Server $sourceVI| get-vm | Get-Folderpath $report | Export-Csv "c:\vms-with-FolderPath-$($datacenter).csv" -NoTypeInformation
The Export for the vms-with-folderpath is fine.
That one with Folders-with-FolderPath isn't.
I have 4 datacenters with the same name in different folders and i just get 2 in a format like that:
Name,Path |
Discovered Virtual Machine,"YYYY\XXXX\vm\Discovered Virtual Machine" |
Discovered Virtual Machine,"YYYY\vm\Discovered Virtual Machine" |
It would be really great if you can tell me my fail!
Can you give the script I referred to in my previous answer a go ?
I suspect that will produce a better list.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Okay
when i paste that code into my console it shows me a nice list of all Datacenters with all VMs under it. (The # Virtual machines & Templates part in your script)
Now i just need that with the folder witch are befor that Datacenters and without the VMs at the end in an csv sheet.
That would be really great. If that works my last problem would just be to creat the folders an datacenters in the new VIServer.
It would be sooo awesome if that is possible.
Ok, try this.
It's a slightly adapted version of that script
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"){ Write-Host ($path + "\" + $childfld.Name) } get-children $childfld $path } "VirtualMachine"{ # $vm = $path + "\" + $childfld.Name # Write-Host $vm } "Datacenter"{ Write-Host ($path + "\" + $childfld.Name) get-children $childfld $path } } } } Get-Folder -NoRecursion | Get-View | % { get-children $_ "" }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Works fine!
But there is also the "bug" that the script is showing me "Discoverd Virtual Machine"
Like that:
\"folder"\"datacenter"
\"folder"\"datacenter"\Discoverd Virtual Machine
But that is not really important.
Could you help me to export that output and to creat the folders and datancenters in my new VIServer?
It would be really great if someone could help me with my problem.
I don't know how to export that data into an csv file and how to creat folders and datacenters under other folders in my new VIServer.