Month: November 2019

Template and Powered OFF VM’s Report from vCenter server

Template and Powered OFF VM’s Report from vCenter server

This powershell script will help you to pull report of powered off VM’s and the old templates. You can run this script from your desktop directly or from your vCenter server. You need to provide vCenter server list as input. Script will ask for credentials during runtime and output will be stored in csv file.

$VCServers = Get-Content "C:\vclist.txt"

$vcUSERNAME = Read-Host 'Enter user name'
$vcPassword = Read-Host 'Enter password' -AsSecureString
$logincred = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)

$date = Get-Date -Format "yyyy_MM_dd_hh_mm"

#---------------------------- VC Report generation-------------------------------------------#

foreach ($VCServer in $VCServers) {

Write-Host -ForegroundColor DarkYellow "Working on $VCServer"
Connect-VIServer -Server $VCServer -Credential $logincred # -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null

#----------------- Template Report -----------------#

$csvfile_template = "C:\VC_reports\$($VCServer + "_" +$date + "_" + "template.csv")"
Write-Host -ForegroundColor Yellow "File name - $csvfile_template"

Get-Template -Name spwdfvm* | Sort-Object |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}} |
Export-Csv $csvfile_template -NoTypeInformation -UseCulture

#------------------- OLD VM Report -------------------#

$csvfile_vm = "C:\VC_reports\$($VCServer + "_" +$date + "_" + "OLD_VM.csv")"
Write-Host -ForegroundColor Yellow "File name - $csvfile_vm"

Get-VMHost | % { Get-VM -Location $_.Name } | where {$_.PowerState -eq "PoweredOff"}  |
Select Name, PowerState,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="UsedSpaceGB";E={[math]::Round($_.UsedSpaceGB,1)}},
@{N="ProvisionedSpaceGB";E={[math]::Round($_.ProvisionedSpaceGB,1)}},
@{N="Folder";E={$_.Folder.Name}} |
Export-Csv $csvfile_vm -NoTypeInformation -UseCulture

disconnect-VIserver -Server $VCServer -confirm:$false

}