As I wanted to retain all the configuration and Information about VMs from previous vCenter and apply the same information on another vCenter, This script solves the purpose. (I was doing some Esxi migration to another vCenter and earlier I had written 2 script to Get Virtual machine folder Path from and VMs and Templates and Move them to the exact same folder hierarchy on another vcenter),
function Export-VMAnnotation {
<#
.SYNOPSIS
Export VM information Folderpath, Annotation Notes
.DESCRIPTION
The function retrives folderPath, Annotation, Notes from vcenter, it can be exported to csv file.
.NOTES
Author: Kunal Udapi
http://kunaludapi.blogspot.com
.PARAMETER N/a
No Parameters Required
.EXAMPLE
PS> Export-VMAnnotation -Datacenter DatacenterName
.EXAMPLE
PS> Export-VMAnnotation -Datacenter DatacenterName | Export-CSV -Path c:\Temp\VMCMDB.csv
#>
[CmdletBinding()]
#####################################
## ## Version: 1
## Tested this script on successfully
## 1) Powershell v4
## 2) Windows 8.1
## 3) vSphere 5.5 (vcenter, esxi)
## 4) powercli 6.0
#####################################
Param (
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[Alias("Name")]
[string]$DataCenter
)
Begin {
if (-not(Get-PSSnapin vmware.vimautomation.core -ErrorAction SilentlyContinue)) {
Add-PSSnapin vmware.vimautomation.core
} #if
function Get-VMFolderPath {
<#
.SYNOPSIS
Get folder path of Virtual Machines
.DESCRIPTION
The function retrives complete folder Path from vcenter (Inventory >> Vms and Templates)
.NOTES
Author: Kunal Udapi
http://kunaludapi.blogspot.com
.PARAMETER N/a
No Parameters Required
.EXAMPLE
PS> Get-VM vmname | Get-VMFolderPath
.EXAMPLE
PS> Get-VM | Get-VMFolderPath
.EXAMPLE
PS> Get-VM | Get-VMFolderPath | Out-File c:\vmfolderPathlistl.txt
#>
#####################################
## http://kunaludapi.blogspot.com
## Version: 1
## Windows 8.1
## Tested this script on
## 1) Powershell v4
## 2) VMware vSphere PowerCLI 6.0 Release 1 build 2548067
## 3) Vsphere 5.5
#####################################
Begin {} #Begin
Process {
foreach ($vm in $Input) {
$DataCenter = $vm | Get-Datacenter
$DataCenterName = $DataCenter.Name
$VMname = $vm.Name
$VMParentName = $vm.Folder
if ($VMParentName.Name -eq "vm") {
$FolderStructure = "{0}\{1}" -f $DataCenterName, $VMname
$FolderStructure
Continue
}#if ($VMParentName.Name -eq "vm")
else {
$FolderStructure = "{0}\{1}" -f $VMParentName.Name, $VMname
$VMParentID = Get-Folder -Id $VMParentName.ParentId
do {
$ParentFolderName = $VMParentID.Name
if ($ParentFolderName -eq "vm") {
$FolderStructure = "$DataCenterName\$FolderStructure"
$FolderStructure
break
} #if ($ParentFolderName -eq "vm")
$FolderStructure = "$ParentFolderName\$FolderStructure"
$VMParentID = Get-Folder -Id $VMParentID.ParentId
} #do
until ($VMParentName.ParentId -eq $DataCenter.Id) #until
} #else ($VMParentName.Name -eq "vm")
} #foreach ($vm in $VMList)
} #Process
End {} #End
} #function Get-VMFolderPath
} #Begin
Process {
$VMList = Get-Datacenter $DataCenter | Get-VM
foreach ($vm in $VMList) {
$Annotation = $vm | Get-Annotation
$IPAddress = $vm.extensiondata.guest.ipaddress -Join ", "
$FolderPath = $vm | Get-VMFolderPath
$InfoObj = New-Object PSObject
$InfoObj | Add-Member -MemberType NoteProperty -Name VMNAME -Value $vm.Name
$InfoObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$InfoObj | Add-Member -MemberType NoteProperty -Name PowerState -Value $vm.Powerstate
$InfoObj | Add-Member -MemberType NoteProperty -Name FolderPath -Value $FolderPath
$InfoObj | Add-Member -MemberType NoteProperty -Name Notes -Value $vm.Notes
foreach ($Attr in $Annotation) {
$InfoObj | Add-Member -MemberType NoteProperty -Name $($Attr.Name) -Value $Attr.Value
}
$InfoObj
} #foreach $vm
} #Process
end {}
}#function Export-VMAnnotation
Kunal Udapi - vExpert 2014, 2015
http://kunaludapi.blogspot.com