
Quando si gestiscono molte macchine virtuali su un host VMware ESXi, l’organizzazione e la documentazione diventano fondamentali.
Annotare informazioni come proprietario, funzione della VM, reparto di riferimento o altre note operative permette di mantenere ordine e velocizzare le attività di amministrazione.
Tuttavia, farlo manualmente su decine (o centinaia) di VM può essere noioso e soggetto a errori.
In questo articolo vedremo come automatizzare il caricamento delle note nelle Virtual Machine partendo da un semplice file CSV e utilizzando PowerCLI, lo strumento di VMware che consente di gestire l’infrastruttura vSphere tramite PowerShell.
PREREQUISITI
- Sistema operativo Windows con PowerShell installato
- Modulo VMware.PowerCLI installato (Install-Module VMware.PowerCLI)
- Accesso al vCenter (utente con permessi di scrittura)
- File CSV strutturato con i seguenti campi:
- Name,Team, Owner, Ambiente, OS, Ticket,Descrizione, Descrizione Lunga
NOTA BENE: per installare il modulo Modulo PowerCLI eseguire da una Console Powershell con diritti Amministrativi il seguente comando:
|
0 |
Install-Module VMware.PowerCLI -Scope CurrentUser
|
DESCRIZIONE DELLO SCRIPT
Lo script esegue le seguenti operazioni:
- Chiede il nome del vCenter a cui collegarsi
- Legge un file CSV contenente i nomi delle VM e i relativi metadati
Per ogni VM trovata nel vCenter:
- Recupera la data di creazione della VM (CreateDate) dal vSphere API
- Crea una sezione di note con i dati
- Se sono presenti note esistenti, aggiunge la nuova sezione (append)
- Altrimenti scrive la sezione ex novo
- Mostra un report delle VM modificate
SCRIPT COMPLETO
|
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# Richiesta del vCenter all'utente
$vCenter = Read-Host "Inserisci il nome o l'indirizzo del vCenter"
Connect-VIServer -Server $vCenter
# Percorso del file CSV
$csvPath = "C:\Temp\vm_info.csv"
# Importa i dati
$vms = Import-Csv -Path $csvPath
# Lista delle VM su cui è stato fatto append
$appendList = @()
# INIZIO del ciclo foreach
foreach ($entry in $vms) {
$vmName = $entry.Name.Trim()
$vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
if (-not $vm) {
Write-Host "VM non trovata, salto: $vmName" -ForegroundColor Yellow
continue
}
# Mappatura con fallback
$installationDate = if ($vm.ExtensionData.Config.CreateDate) { $vm.ExtensionData.Config.CreateDate } else { "N/A" }
$requestor = if ($entry.Team) { $entry.Team } else { "N/A" }
$owner = if ($entry.Owner) { $entry.Owner } else { "N/A" }
$env = if ($entry.Ambiente) { $entry.Ambiente } else { "N/A" }
$os = if ($entry.OS) { $entry.OS } else { "N/A" }
$ticket = if ($entry.Ticket) { $entry.Ticket } else { "N/A" }
# Campo Role derivato da Descrizione + Descrizione Lunga
$role1 = if ($entry.Descrizione) { $entry.Descrizione } else { "" }
$role2 = if ($entry.'Descrizione Lunga') { $entry.'Descrizione Lunga' } else { "" }
if ($role1 -and $role2) {
$role = "$role1 - $role2"
} elseif ($role1) {
$role = $role1
} elseif ($role2) {
$role = $role2
} else {
$role = "N/A"
}
# Nuova sezione da aggiungere alle note
$newSection = @"
---
Installation Date: $installationDate
Requestor: $requestor
Owner: $owner
Env: $env
Role: $role
O.S.: $os
Ticket: $ticket
"@.Trim()
# Note attuali (se presenti)
$currentNote = $vm.ExtensionData.Summary.Config.Annotation
if ($currentNote) {
# Append
$combinedNote = $currentNote.Trim() + "`n`n" + $newSection
$appendList += $vmName
Write-Host "Note esistenti trovate: append effettuato su $vmName" -ForegroundColor Cyan
} else {
# Prima scrittura
$combinedNote = $newSection
Write-Host "Note scritte ex novo su: $vmName" -ForegroundColor Green
}
# Aggiorna il campo Notes
Set-VM -VM $vm -Notes $combinedNote -Confirm:$false
} # ← QUESTA È LA GRAFFA DI CHIUSURA DEL foreach
# Disconnessione dal vCenter
Disconnect-VIServer -Server $vCenter -Confirm:$false
# Report finale
if ($appendList.Count -gt 0) {
Write-Host "`nVM con note aggiornate in append:" -ForegroundColor Magenta
$appendList | ForEach-Object { Write-Host "- $_" }
} else {
Write-Host "`nNessuna VM con note preesistenti. Nessun append effettuato." -ForegroundColor Gray
}
|
Download “Scripts_Caricamento_delle_note_nelle_Virtual_Machine_da_CSV_su_VMware_ESXi_con_PowerCLI” Scripts_Caricamento_delle_note_nelle_Virtual_Machine_da_CSV_su_VMware_ESXi_con_PowerCLI.zip – Scaricato 600 volte – 1,42 KB
ESEMPIO DI BLOCCO NOTE FINALE
Installation Date: 2024-12-01T10:25:00Z
Requestor: TeamA
Owner: Mario Rossi
Env: PROD
Role: Web Server – Frontend NodeJS
O.S.: Ubuntu 24.04
Ticket: SR123456
CONSIGLI
ATTENZIONE: Eseguire lo script in test prima di usarlo in produzione…
Per preparare un backup delle note esistenti seguire l’articolo:
Esportazione in CSV del campo Note delle Virtual Machine su VMware ESXi tramite PowerCLI

0 commenti