param([string]$VmAlone, [string]$Action = "export", [int]$Confirm="0", [string]$XmlPath="VmFieldList.xml") add-pssnapin -name "VMware.VimAutomation.Core" Connect-VIServer localhost if ($VmAlone -and $Action -eq "export") {$VMs = Get-View -ViewType VirtualMachine -Filter @{"Name"="^$VmAlone$"}} else {$VMs = Get-View -ViewType VirtualMachine|sort name} if ($Action -eq "export"){ $VmFieldList = @() Foreach ($VM in $VMs){ $myObj = "" |Select VmName $myObj.VmName = $VM.Name if ($VM.Summary.CustomValue){ foreach ($vmField in $VM.Summary.CustomValue){ $myObj|Add-Member -MemberType noteproperty -Name ($VM.AvailableField|?{$vmField.Key -eq $_.Key}).name -Value $vmField.value } } if ($VM.Config.Annotation.length -gt 0) {$myObj|Add-Member -MemberType noteproperty -Name "Annotation" -Value $VM.Config.Annotation} if (($myObj|gm -MemberType NoteProperty|Measure-Object).count -gt "1"){$VmFieldList += $myObj} } if (Get-ChildItem $XmlPAth) {Rename-Item $XmlPAth "$($XmlPAth.Split(".")[0])-$(get-date -Format yyyyMMddHHmmss).xml" -Force} $VmFieldList|Export-Clixml $XmlPAth } elseif ($VmAlone -and $Action -eq "import" -and $Confirm -eq "1" -and (Get-ChildItem $XmlPAth)){ $VmFieldList = Import-Clixml $XmlPAth if (($VmFields = $VmFieldList|?{$_.VmName -match "^$VmAlone$"})) {Foreach ($VmField in ($VmFields|Get-Member -MemberType NoteProperty)) { if ($VmField.name -match "^VmName$"){} elseif ($VmField.name -match "^Annotation$"){Set-VM -VM $VmAlone -Description $VmFields.Annotation -Confirm:$false|out-null} else {Set-Annotation -Entity $VmAlone -CustomAttribute $VmField.name -Value $VmFields."$($VmField.name)" -Confirm:$false|out-null} } } }