list_vm_cust_attribute.ps1
#!/usr/bin/pwsh
# Read VM custom attributes and substitute , into the values by ;
$VMvCenterName = 'vcenter01'
$VMvCenterUser = 'vmware_RO'
$VMvCenterPass = 'Mypasswd'
###
### Connect to vCenter
###
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -WarningAction SilentlyContinue -ParticipateInCeip $false -DisplayDeprecationWarnings $false -confirm:$false
$VCSession = Connect-VIServer -Server $VMvCenterName -User $VMvCenterUser -Password $VMvCenterPass
# Report file
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm"
$outfile = "vm_inventory_report_$timestamp.csv"
function CleanValue($v) {
if ($null -eq $v) { return $null }
return ($v.ToString() -replace ',', ';')
}
Write-Host "Loading custom attributes..."
$attrDefs = Get-CustomAttribute
# Key → Name lookup
$attrKeyToName = @{}
foreach ($a in $attrDefs) {
$attrKeyToName[$a.Key] = $a.Name
}
Write-Host "Caching clusters..."
$clusters = Get-View -ViewType ClusterComputeResource -Property Name
$clusterMap = @{}
foreach ($c in $clusters) {
$clusterMap[$c.MoRef.Value] = $c.Name
}
Write-Host "Caching hosts..."
$hosts = Get-View -ViewType HostSystem -Property Parent
$hostClusterMap = @{}
foreach ($h in $hosts) {
$hostClusterMap[$h.MoRef.Value] = $clusterMap[$h.Parent.Value]
}
Write-Host "Collecting VM data..."
$vms = Get-View -ViewType VirtualMachine -Property `
Name,
Runtime.PowerState,
Runtime.Host,
Summary.Config.Annotation,
CustomValue
$results = foreach ($vm in $vms) {
$clusterName = CleanValue($hostClusterMap[$vm.Runtime.Host.Value])
$row = [ordered]@{
"VM Name" = CleanValue($vm.Name)
"Power State" = CleanValue($vm.Runtime.PowerState)
"Cluster Name" = $clusterName
"VM Notes" = CleanValue($vm.Summary.Config.Annotation)
}
# initialize custom attributes
foreach ($attr in $attrDefs) {
$row["CA: $($attr.Name)"] = $null
}
# populate attributes
foreach ($cv in $vm.CustomValue) {
$attrName = $attrKeyToName[$cv.Key]
$row["CA: $attrName"] = CleanValue($cv.Value)
}
[PSCustomObject]$row
}
Write-Host "Writing CSV report..."
$results | Export-Csv $outfile -NoTypeInformation
Write-Host "Report created: $outfile"
Write-Host "##### Collect Vcenter Finished"
###
### Disconnect from vCenter
###
Disconnect-VIServer -Server $VMvCenterName -Confirm:$false
exit $RC
list_vm_cust_attribute.ps1
#!/usr/bin/pwsh
$VMvCenterName = 'vcenter01'
$VMvCenterUser = 'vmware_RO'
$VMvCenterPass = 'Mypasswd'
###
### Connect to vCenter
###
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -WarningAction SilentlyContinue -ParticipateInCeip $false -DisplayDeprecationWarnings $false -confirm:$false
$VCSession = Connect-VIServer -Server $VMvCenterName -User $VMvCenterUser -Password $VMvCenterPass
function Get-FolderPath {
param($VM)
$folder = $VM.ExtensionData.Parent
$path = @()
while ($folder) {
$folderObj = Get-View $folder
if ($folderObj.Name -ne "vm") {
$path += $folderObj.Name
}
$folder = $folderObj.Parent
}
[array]::Reverse($path)
return ($path -join "\")
}
$report = Get-VM | ForEach-Object {
$cluster = Get-Cluster -VM $_
$folderPath = Get-FolderPath -VM $_
$esxHost = $_.VMHost.Name
$VMNotes = $_.VMHost.Notes
# OS detection
$runningOS = $_.ExtensionData.Guest.GuestFullName
$configuredOS = $_.ExtensionData.Config.GuestFullName
if ([string]::IsNullOrWhiteSpace($runningOS)) {
$Running_OS = $configuredOS
}
else {
$Running_OS = $runningOS
}
# IPv4 only
$ipv4 = $_.Guest.IPAddress |
Where-Object { $_ -match '^\d{1,3}(\.\d{1,3}){3}$' }
[PSCustomObject]@{
Name = $_.Name
DNSName = $_.Guest.HostName
AssetType = "virtual"
NumCpu = $_.NumCpu
MemoryGB = [int][Math]::Round($_.MemoryGB)
PowerState = $_.PowerState
ESXHost = $esxHost
Running_OS = $Running_OS
Configured_OS = $configuredOS
IPAddress = ($ipv4 -join ", ")
Cluster = $cluster.Name
FolderPath = $folderPath
CreateDate = $_.ExtensionData.Config.CreateDate
$VMNotes = $VMNotes
}
}
# Export to CSV for CMDB import
$report | Export-Csv "/inventory/list_vm.csv" -NoTypeInformation
# Get OS version on ESX
Get-VMHost | Select-Object Name, Version, Build, @{N="FullName";E={$_.ExtensionData.Config.Product.FullName}}
Write-Host "##### Collect Vcenter Finished"
###
### Disconnect from vCenter
###
Disconnect-VIServer -Server $VMvCenterName -Confirm:$false
exit $RC