Windows Powershell Script to export vmware vcenter machines’ data in a CSV file

Posted on 29 luglio 2014 di

0


Commentate o Decommentate le voci del report che vi occorrono aggiungendo o togliendo il carattere # ad inizio riga;

Copy & Paste il seguente codice in un file.PS1 e poi lanciatelo da vmware powercli;

Alla fine dello script è specificato il path di export (ad esempio C:\export.csv).

=====================START =====================

Connect-VIServer -Server “vcenter-fqdn” -User “domain\vmwareuser” -Password “xxxxx”

Get-VM | `
ForEach-Object {
$VM = $_
$VMview = $VM | Get-View
$VMResourceConfiguration = $VM | Get-VMResourceConfiguration
$VMHardDisks = $VM | Get-HardDisk
$HardDisksSizesGB = @()
$Temp = $VMHardDisks | ForEach-Object { $HardDisksSizesGB += [Math]::Round($_.CapacityKB/1MB) }
$VmdkSizeGB = “”
$Temp = $HardDisksSizesGB | ForEach-Object { $VmdkSizeGB += “$_+” }
$VmdkSizeGB = $VmdkSizeGB.TrimEnd(“+”)
$TotalHardDisksSizeGB = 0
$Temp = $HardDisksSizesGB | ForEach-Object { $TotalHardDisksSizeGB += $_ }
$Snapshots = $VM | Get-Snapshot
$Report = “” | Select-Object VMname,Status,Hostname,IPaddresses,GuestOS,Description,ESXname,MemoryGB,vCPUcount,vNICcount,VmdkSizeGB,TotalVmdkSizeGB
$Report.VMName = $VM.name
$Report.Status = $VM.Guest.State
$Report.Hostname = $VM.guest.hostname
$Report.IPaddresses = [String]$VM.Guest.IPAddress
$Report.GuestOS = $VM.Guest.OSFullName
$Report.Description = $VM.Notes
$Report.ESXname = $VM.VMHost
#$Report.ClusterName = ($VM | Get-Cluster).Name
$Report.MemoryGB = $VM.MemoryMB/1024
$Report.vCPUcount = $VM.NumCpu
$Report.vNICcount = $VM.Guest.Nics.Count
$Report.VmdkSizeGB = $VmdkSizeGB
$Report.TotalVmdkSizeGB = $TotalHardDisksSizeGB
#$Report.DatastoreName = $VMview.Config.DatastoreUrl
#$Report.ToolsVersion = $VMview.Config.Tools.ToolsVersion
#$Report.ToolsUpdate = $VMview.Guest.ToolsStatus
#$Report.NumCpuShares = $VMResourceConfiguration.NumCPUShares
#$Report.CpuLimitMHZ = $VMResourceConfiguration.CpuLimitMhz
#$Report.CpuReservationMHZ = $VMResourceConfiguration.CpuReservationMhz
#$Report.NumMemShares = $VMResourceConfiguration.NumMemShares
#$Report.ReservationsMB = $VMResourceConfiguration.MemReservationMB
#$Report.LimitMB = $VMResourceConfiguration.MemLimitMB
#$Report.SnapshotCount = (@($VM | Get-Snapshot)).Count
Write-Output $Report
} | sort $VM.name | Export-Csv “C:\export.csv” -NoTypeInformation

=====================END =====================

Annunci
Posted in: Vmware