Hello,
I would like to know when each VMs was created in my vcenter, output file have some duplicate records, for example there is one VM with multiple creation date as Connected:
| Born on | Creator | Creation Type |
| 06/07/17 | Connected | |
| 06/04/17 | Connected | |
| 06/03/17 | Connected | |
| 06/01/17 | Connected | |
| 05/26/17 | Connected | |
| 05/20/17 | Connected | |
| 05/18/17 | Connected | |
| 04/30/17 | Connected | |
| 04/29/17 | Connected | |
| 04/25/17 | Connected | |
| 04/23/17 | Connected | |
| 04/22/17 | Connected | |
| 04/20/17 | Connected | |
| 04/19/17 | Connected | |
| 04/18/17 | Connected | |
| 04/16/17 | Connected | |
| 04/15/17 | Connected |
///////////////////////////////////////////////////////////////////////////////////////////////////////
#Enter your vCenter Host below
$vcenter = "0.0.0.0"
#Enter the CSV file to be created
$csvfile = "VM_Birthdays.CSV"
################################
#Load the VMware Powershell snapin if the script is being executed in PowerShell
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'
#Connect to the vCenter server defined above. Ignore certificate errors
Write-Host "Connecting to vCenter"
Connect-VIServer $vcenter -wa 0
Write-Host "Connected"
Write-Host ""
#Check to see if the file exists, if it does then overwrite it.
if (Test-Path $csvfile) {
Write-Host "Overwriting $csvfile"
del $csvfile
}
#Create the CSV title header
Add-Content $csvfile "VM,Born on,Creator,Creation Type,Event Message"
#Gather all VM's from vCenter
$vms = Get-VM | sort Name
foreach ($VM in $vms) {
Write-Host "Gathering info for $VM"
#Search for events where the VM was deployed from a template
$vmevents = Get-VIEvent $VM -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "Deploying*"} |Select CreatedTime, UserName, FullFormattedMessage
if ($vmevents)
{
$type = "From Template"
}
#If no events were found, search for events where the VM was created from scratch
if (!$vmevents) {
$vmevents = Get-VIEvent $VM -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "Created*"} |Select CreatedTime, UserName, FullFormattedMessage
Write-Host "Searching by Created"
$type = "From Scratch"
}
#If no events were found, search for events where the VM was cloned
if (!$vmevents) {
$vmevents = Get-VIEvent $VM -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "Clone*"} |Select CreatedTime, UserName, FullFormattedMessage
Write-Host "Searching by Cloned"
$type = "Cloned"
}
#If no events were found, search for events where the VM was discovered
if (!$vmevents) {
$vmevents = Get-VIEvent $VM -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "Discovered*"} |Select CreatedTime, UserName, FullFormattedMessage
Write-Host "Searching by Discovered"
$type = "Discovered"
}
#If no events were found, search for events where the VM was connected (typically from Backup Restores)
if (!$vmevents) {
$vmevents = Get-VIEvent $VM -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "* connected"} |Select CreatedTime, UserName, FullFormattedMessage
Write-Host "Searching by Connected"
$type = "Connected"
}
#I have no idea how this VM came to be.
if (!$vmevents) {
Write-Host "No clue how this VM got here!"
$type = "Immaculate Conception"
}
#In some cases there may be more than one event found (typically from VM restores). This will include each event in the CSV for the user to interpret.
foreach ($event in $vmevents) {
#Prepare the entries
$birthday = $event.CreatedTime.ToString("MM/dd/yy")
$parent = $event.Username
$message = $event.FullFormattedMessage
#Add the entries to the CSV
$write = "$VM, $birthday, $parent, $type, $message"
Add-Content $csvfile $write
}
}
TRY THIS: > Get-VMCreationTimes -Get Time -Name -IP -Created By
function Get-VMCreationTimes {
$vms = get-vm
$vmevts = @()
$vmevt = new-object PSObject
foreach ($vm in $vms) {
#Progress bar:
$foundString = " Found: "+$vmevt.name+" "+$vmevt.createdTime+" "+$vmevt.IPAddress+" "+$vmevt.createdBy
$searchString = "Searching: "+$vm.name
$percentComplete = $vmevts.count / $vms.count * 100
write-progress -activity $foundString -status $searchString -percentcomplete $percentComplete
$evt = get-vievent $vm | sort createdTime | select -first 1
$vmevt = new-object PSObject
$vmevt | add-member -type NoteProperty -Name createdTime -Value $evt.createdTime
$vmevt | add-member -type NoteProperty -Name name -Value $vm.name
$vmevt | add-member -type NoteProperty -Name IPAddress -Value $vm.Guest.IPAddress
$vmevt | add-member -type NoteProperty -Name createdBy -Value $evt.UserName
#uncomment the following lines to retrieve the datastore(s) that each VM is stored on
#$datastore = get-datastore -VM $vm
#$datastore = $vm.HardDisks[0].Filename | sed 's/\[\(.*\)\].*/\1/' #faster than get-datastore
#$vmevt | add-member -type NoteProperty -Name Datastore -Value $datastore
$vmevts += $vmevt
#$vmevt #uncomment this to print out results line by line
}
$vmevts | sort createdTime
}
Get-VMCreationTimes
((( For better answers post your quest on )))
Home > VMTN > Automation Tools > VMware PowerCLI
LINK: VMware PowerCLI
Raul.
VMware VDI Administrator.
Stay Connected :smileyplus: Don't forget to:
Like > Share > Comment > Reply > Helpful > Answered
