Monday 14 March 2016

vCenter VM Report



#The script is executed from PowerCLI or PowerShell with PowerCLI module.
#It connects to a vCenter server and creates a CSV report in D:\ drive 
#or you may set your preferred location.
#The output file is named "VM_Report_currentDate.csv.".
#The report lists VM names, Windows names (not FQDNs), 
#IPv4 addresses and whatever is written in the Notes filed.

#Set the input value for vCenter variable to string. 

param ([string] $VIServer)

Write-Host
Write-Host

#Get the vCenter server name if PowerCLI is already connected.

$VIServer = $global:DefaultVIServers.Name

#If not already connected to a vCenter server, 
#prompt for vCenter Server FQDN or IP.

If ($VIServer -eq "")
  {
    $serverName = Read-Host 'Please enter the vCenter Server FQDN or IP address'
    $VIServer = $serverName

    #Connect to vCenter Server

    Write-Host
    Write-Host "Connecting to vCenter Server..."
    Write-Host
    Connect-VIServer $VIServer
  }

Else {}

#Create a VM report including VM names, Windows computer names (not FQDN/DNS names),
#IPv4 addresses and comments from the Notes field. Sort by VM names.

Write-Host
Write-Host
Write-Host "Building VM report..."
Write-Host

$VMs = Get-VM
$i = 0
$Output = ForEach ($VM in $VMs)

  {
    $i++
    Write-Progress -Activity "Creating VM report" -Status "Status:" -PercentComplete (($i/$VMs.count)*100) 

    "" | select @{Name = "VM Name"; Expression = {$VM.Name.ToLower()}},
    @{Name = "Windows Name"; Expression = {$WinName = $VM.Guest.HostName -Split'\.' $WinName[0]}},
    @{Name = "IP Address"; Expression = {$VM.Guest.IPaddress[0]}},
    @{Name = "Group"; Expression = {$VM.Notes}}
  }

#Set the output file format, location and date.

Write-Host
Write-Host "Creating report file..."
Write-Host

#Save the report and point the file location.

$Day = (Get-Date).Day
$Month = (Get-Date).Month
$Year = (Get-Date).Year
$File = "VM_Report_" + $Day + "." + $Month + "." + $Year + ".csv"
$Output | Sort-Object "VM Name" | Export-CSV -path "D:\$File" -NoTypeInformation

Write-Host
Write-Host "Your report is here: D:\$File"

No comments:

Post a Comment