Created
October 1, 2026 06:04
-
-
Save thost96/fb6c5272e93d0b81b94c4bf3fb466f2c to your computer and use it in GitHub Desktop.
import-custom-vm-attributes.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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