OsburnM
Hot Shot
Hot Shot

Script to Read Date Stamp of VMDK

Jump to solution

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?

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
14 Replies
RvdNieuwendijk
Leadership
Leadership

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
bulletprooffool
Champion
Champion

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?

One day I will virtualise myself . . .
OsburnM
Hot Shot
Hot Shot

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!

0 Kudos
RvdNieuwendijk
Leadership
Leadership

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
LucD
Leadership
Leadership

I think you better add the -MaxSamples parameter to the Get-VIEvent cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RvdNieuwendijk
Leadership
Leadership

Luc thanks for the tip.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
OsburnM
Hot Shot
Hot Shot

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.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
RvdNieuwendijk
Leadership
Leadership

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. Smiley Wink

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
OsburnM
Hot Shot
Hot Shot

You mean 99,999,999 and not 99.999.999 correct?

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
RvdNieuwendijk
Leadership
Leadership

Now you know that I Iive in the rest of the world. Smiley Happy

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
Matt_B1
Enthusiast
Enthusiast

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:

  • Modified the Get-VIEvent line to use a start date so you can determine how far back in the VI events you want to look
  • The script assumes the AD computer query will only find one object with that name so it can return 1 or more objects if the server name is part of other server names.  I fixed this using a filter
  • I moved the $Created entry up one level in the Else section to ensure if gets populated regardless of finding the user in AD
  • I changed the foreground color to blue so it is easier to see
  • Changed the Set-CustomField to Set-Annotation since the prior is obsolete
  • For all VMs that don't have events to provide creation info, save them to a CSV file for reporting

I have attached the updated script.

0 Kudos
esxi1979
Expert
Expert

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 ?

0 Kudos