Skip to content

Instantly share code, notes, and snippets.

@thost96
Created October 1, 2026 06:04
Show Gist options
  • Select an option

  • Save thost96/fb6c5272e93d0b81b94c4bf3fb466f2c to your computer and use it in GitHub Desktop.

Select an option

Save thost96/fb6c5272e93d0b81b94c4bf3fb466f2c to your computer and use it in GitHub Desktop.
import-custom-vm-attributes.ps1
Connect-VIServer -Server vc-dest.example.com
$data = Import-Csv ".\exported-attributes-value.csv" -Delimiter ";" -Encoding UTF8
$existing = Get-CustomAttribute -TargetType VirtualMachine -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Name
$data.FieldKey | Sort-Object -Unique | Where-Object { $_ -notin $existing } | ForEach-Object {
New-CustomAttribute -Name $_ -TargetType VirtualMachine | Out-Null
Write-Host "Created attribute: $_"
}
foreach ($vmGroup in ($data | Where-Object { $_.FieldValue } | Group-Object VMname)) {
$vm = Get-VM -Name $vmGroup.Name -ErrorAction SilentlyContinue
if (-not $vm) {
Write-Warning "VM not found: $($vmGroup.Name)"
continue
}
foreach ($line in $vmGroup.Group) {
Set-Annotation -Entity $vm -CustomAttribute $line.FieldKey -Value $line.FieldValue | Out-Null
}
Write-Host "Done: $($vm.Name)"
}
Disconnect-VIServer -Server * -Confirm:$false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment