Greetings all! I started using Alan Renouf’s script to stamp date & by info on our VMs (http://www.virtu-al.net/2010/02/23/who-created-that-vm/). It works great; but, of course it's limited to data stored in VC. Since our data purges after 90 days-- it hasn't given me much for the MANY older VMs.
That got me thinking about other places that info could be and I thought of the idea of the original VMDK file on the datastore. The first VMDK appears (in most, if not all cases) to have the original date stamp of that file and doesn't appear to get changed like the other files within the given VM folder on the datastore.
So, is it possible read the Time/Date stamp of the xxx.VMDK of each VM within all the datastores, and send that date/time to Alan's script?
I have changed Alan Renouf's "Who created that VM?" script to try to get the creation date from the Active Directory, if it can't be found in the events:
Connect-VIServer MYVISERVER # Uncomment the next line to test this script and tell you what it would do ! # $WhatIfPreference = $true if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) { Add-PSSnapin VMware.VimAutomation.Core } if (-not (Get-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue)) { Add-PSSnapin Quest.ActiveRoles.ADManagement } $VMs = Get-VM | Sort Name $VM = $VMs | Select -First 1 If (-not $vm.CustomFields.ContainsKey("CreatedBy")) { Write-Host "Creating CreatedBy Custom field for all VM's" New-CustomAttribute -TargetType VirtualMachine -Name CreatedBy | Out-Null } If (-not $vm.CustomFields.ContainsKey("CreatedOn")) { Write-Host "Creating CreatedOn Custom field for all VM's" New-CustomAttribute -TargetType VirtualMachine -Name CreatedOn | Out-Null } Foreach ($VM in $VMs){ If ($vm.CustomFields["CreatedBy"] -eq $null -or $vm.CustomFields["CreatedBy"] -eq ""){ Write-Host "Finding creator for $vm" $Event = $VM | Get-VIEvent -Types Info -MaxSamples 99999999 | Where { $_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent" -or $_.Gettype().Name -eq "VmClonedEvent"} If (($Event | Measure-Object).Count -eq 0){ $User = "Unknown" $Computer = Get-QADComputer -Identity $VM.Name if ($Computer) { $Created = $Computer.WhenCreated.ToString() } else { $Created = "Unknown" } } Else { If ($Event.Username -eq "" -or $Event.Username -eq $null) { $User = "Unknown" } Else { $User = (Get-QADUser -Identity $Event.Username).DisplayName if ($User -eq $null -or $User -eq ""){ $User = $Event.Username } $Created = $Event.CreatedTime } } Write "Adding info to $($VM.Name)" Write-Host -ForegroundColor Yellow "CreatedBy $User" $VM | Set-CustomField -Name "CreatedBy" -Value $User | Out-Null Write-Host -ForegroundColor Yellow "CreatedOn $Created" $VM | Set-CustomField -Name "CreatedOn" -Value $Created | Out-Null } }
Regards, Robert
Added the -MaxSamples parameter to the Get-VIEvent cmdlet.
Message was edited by: RvdNieuwendijk
I thought it was a brilliant idea. But when I tried to get the creation date of a file in the datastore it was not possible. The only date/time in the DatastoreFileImpl object is the LastWriteTime. So I think it is not possible to use this technique.
Regards, Robert
Hi
You are already loading the quest cmdlets in alan's script.
Although not 100% accurtae, you could at least find the creation date of the AD objects for the VMs:
Get-QADComputer $mycomputername | %{$_.WhenCreated}
so in instances where VMs do not have the info you are looking for anymore you can at least get a cross referenced date for VM that do have correlating AD objects?
That's not a bad idea! It's not perfect, like you said-- since ADObjects existed long before the VM (ie, the older P2Vs); but, hell, that may even be better as it'll let me know how old the "system" is and not just the VM.
Where would I enter this? I'm assuming it'll go after the first IF $EVENT and before the setting $User/$Created unknowns; but, I can't get the syntax right.
Write-Host "Finding creator for $vm"
$Event = $VM | Get-VIEvent -Types Info | Where { $_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent" -or $_.Gettype().Name -eq "VmClonedEvent"}
If (($Event | Measure-Object).Count -eq 0){
$User = "Unknown"
$Created = "Unknown"
} Else {
If ($Event.Username -eq "" -or $Event.Username -eq $null) {
$User = "Unknown"
} Else {
$User = (Get-QADUser -Identity $Event.Username).DisplayName
if ($User -eq $null -or $User -eq ""){
$User = $Event.Username
}
$Created = $Event.CreatedTime
}
}
Thanks!
I have changed Alan Renouf's "Who created that VM?" script to try to get the creation date from the Active Directory, if it can't be found in the events:
Connect-VIServer MYVISERVER # Uncomment the next line to test this script and tell you what it would do ! # $WhatIfPreference = $true if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) { Add-PSSnapin VMware.VimAutomation.Core } if (-not (Get-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue)) { Add-PSSnapin Quest.ActiveRoles.ADManagement } $VMs = Get-VM | Sort Name $VM = $VMs | Select -First 1 If (-not $vm.CustomFields.ContainsKey("CreatedBy")) { Write-Host "Creating CreatedBy Custom field for all VM's" New-CustomAttribute -TargetType VirtualMachine -Name CreatedBy | Out-Null } If (-not $vm.CustomFields.ContainsKey("CreatedOn")) { Write-Host "Creating CreatedOn Custom field for all VM's" New-CustomAttribute -TargetType VirtualMachine -Name CreatedOn | Out-Null } Foreach ($VM in $VMs){ If ($vm.CustomFields["CreatedBy"] -eq $null -or $vm.CustomFields["CreatedBy"] -eq ""){ Write-Host "Finding creator for $vm" $Event = $VM | Get-VIEvent -Types Info -MaxSamples 99999999 | Where { $_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent" -or $_.Gettype().Name -eq "VmClonedEvent"} If (($Event | Measure-Object).Count -eq 0){ $User = "Unknown" $Computer = Get-QADComputer -Identity $VM.Name if ($Computer) { $Created = $Computer.WhenCreated.ToString() } else { $Created = "Unknown" } } Else { If ($Event.Username -eq "" -or $Event.Username -eq $null) { $User = "Unknown" } Else { $User = (Get-QADUser -Identity $Event.Username).DisplayName if ($User -eq $null -or $User -eq ""){ $User = $Event.Username } $Created = $Event.CreatedTime } } Write "Adding info to $($VM.Name)" Write-Host -ForegroundColor Yellow "CreatedBy $User" $VM | Set-CustomField -Name "CreatedBy" -Value $User | Out-Null Write-Host -ForegroundColor Yellow "CreatedOn $Created" $VM | Set-CustomField -Name "CreatedOn" -Value $Created | Out-Null } }
Regards, Robert
Added the -MaxSamples parameter to the Get-VIEvent cmdlet.
Message was edited by: RvdNieuwendijk
I think you better add the -MaxSamples parameter to the Get-VIEvent cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc thanks for the tip.
Thanks guys-- that's got it.
Question on the max samples though... I can see perf benefits to limiting it for on-going uses; but, if I want to use the script the first time to get as much as I can, do I ignore this or use it and set something higher?
It looks like it defaults to 100.
The default for MaxSamples is indeed 100.
The problem, in my opinion, is that you have to specify the MaxSamples parameter, even when you use the Start and Finish parameters.
I find it a bit contradictory 🙂
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
In the script I set the MaxSamples to 99.999.999. If you have more records in your eventlog, you can set it higher.
I would love to see that Get-VIEvent -MaxSamples 0 or -MaxSamples -1 set it to infinite. Maybe in a future release of PowerCLI.
You mean 99,999,999 and not 99.999.999 correct?
99,999,999 in the British Empire countries, 99.999.999 for the rest of the world 😉
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Now you know that I Iive in the rest of the world.
The addition of checking active directory for the creation date is extremely helpful. Since I have set my vCenter Database Retention Policy to retain events for only 90 days, I miss all VMs created over 90 days ago. I also made a few changes/improvements:
I have attached the updated script.
I have changed Alan Renouf's "Who created that VM?" script to try to get the creation date from the Active Directory, if it can't be found in the events
1. If i have 3 months old as retension in vcenter still it can find it ?
2. so we need to run this every 3 months to get updated info ?