VMware Cloud Community
vmdavinci
Contributor
Contributor
Jump to solution

Get VM Create Date & Time

Hello,

I'm searching for the code the get the VM Create Date & Time.

0 Kudos
38 Replies
jamunoz
Contributor
Contributor
Jump to solution

New to all the scripting and using the utilities. I believe I have everything installed, but i'm getting errors when I execute

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is a well know error message.

If you uncheck the option "Check host certificates" in the VIC under , the message will still be there but the connection will succeed.

See also


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

0 Kudos
jamunoz
Contributor
Contributor
Jump to solution

Ok. 1) I'm connected

2) i'm at the Vsphere PowerCLI prompt

3) issue command : d:\getdate1.bat

and get errors$report is not recognized, Get-VIEvent is not recongnized etc

I reaaly do appriciate all the help

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to save the script in a .ps1 file instead of a .bat file.

Then you can start the script from the PowerCLI prompt provided the PowerShell executionpolicy is configured correctly.

Have a look at this PowerShell Day 1 document.


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

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I have modified Luc's script to append the csv file instead of overwriting it.

Because the Export-CSV cmdlet doesn't have an append option, I decided to import the csv file first. Then I add the new content and export the old and new content. So the file is actualy new created with the old and added content.

# How many days in the past to start from
$start = (Get-Date).AddDays(-7)

# The more days back, the higher this number should be.
$eventNr = 9999

$FileName = "C:\VMCreated.csv"
if (Test-Path $FileName) {
  $report = Import-Csv $FileName
}
else {
  $report = @()
}

Get-VIEvent -Start $start -MaxSamples $eventNr |`
  Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} | % {
     $row = "" | Select Date, User, VmName, Cluster, Host, Msg
     $row.Date = $_.createdTime
     $row.Msg = $_.fullFormattedMessage
     $row.User = $_.userName
     $row.VMName = $_.vm.name
     $t = New-Object VMware.Vim.ManagedObjectReference
     $t.type = $_.computeResource.computeResource.type
     $t.Value = $_.computeResource.computeResource.Value
     $row.Cluster = (Get-View $t).Name
     $t.type = $_.host.host.type
     $t.Value = $_.host.host.Value
     $row.Host = (Get-View $t).Name
     $report += $row
}
$report | Export-Csv $FileName -NoTypeInformation


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
jithinraj
Contributor
Contributor
Jump to solution

Hi Lucd, I used below script but I'm not getting any output and I'm getting blank output...I have VC4.1..

Thanks

Jithin

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The results are stored in the CSV file. Do you mean the CSV file is empty ?

Could you perhaps attach the script so I can have a look ?


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

0 Kudos
jithinraj
Contributor
Contributor
Jump to solution

Hi  LucD,

Yes, the CSV file is Empty..I used same script in this discussion..

# How many days in the past to start from
$start = (Get-Date).AddDays(-7)

# The more days back, the higher this number should be.
$eventNr = 9999

$FileName = "C:\VMCreated.csv"
if (Test-Path $FileName) {
  $report = Import-Csv $FileName
}
else {
  $report = @()
}

Get-VIEvent -Start $start -MaxSamples $eventNr |`
  Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} | % {
     $row = "" | Select Date, User, VmName, Cluster, Host, Msg
     $row.Date = $_.createdTime
     $row.Msg = $_.fullFormattedMessage
     $row.User = $_.userName
     $row.VMName = $_.vm.name
     $t = New-Object VMware.Vim.ManagedObjectReference
     $t.type = $_.computeResource.computeResource.type
     $t.Value = $_.computeResource.computeResource.Value
     $row.Cluster = (Get-View $t).Name
     $t.type = $_.host.host.type
     $t.Value = $_.host.host.Value
     $row.Host = (Get-View $t).Name
     $report += $row
}
$report | Export-Csv $FileName

Can you have a look please...thanks in advance...

-Jithin

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just tested the script and it produced a CSV file with a number of VMs that were created during the last 7 days.

Are you sure you created some VMs during the last 7 days ?

How do you run the script ?


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

0 Kudos
jithinraj
Contributor
Contributor
Jump to solution

Thanks Lucd..... I'm not sure whether I'm doing correct way..

I'M sure last 7 days many VMs would have been created..

I copied this script to text file and saved as .ps1 format using ppercli i had run the script..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

How do you start the .ps1 script ? From the PowerCLI prompt ?

Any error messages ?


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

0 Kudos
jithinraj
Contributor
Contributor
Jump to solution

Yea...I had run script from PowerCLI prompt. No error messages, Excuted completly produced CSV file also but if you open the file nothing will be there...

Thanks

Jithin

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you do the following from the PowerCLI prompt, no .ps1 file, are there any events returned ?

You should see a count different from 0.

Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples ([int]::MaxValue) |
Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} |
Measure-Object 


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

0 Kudos
jithinraj
Contributor
Contributor
Jump to solution

Hi Luc,

I got the below error...

Get-VIEvent : Cannot bind parameter 'MaxSamples'. Cannot convert value "[int]::
MaxValue" to type "System.Int32". Error: "Input string was not in a correct for
mat."
At no.ps1:3 char:54
+ Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples <<<<  [int]::MaxValue |
    + CategoryInfo          : InvalidArgument: (:) [Get-VIEvent], ParameterBin
   dingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.GetEvent

0 Kudos
LucD
Leadership
Leadership
Jump to solution

My mistake, that should have been between parenthesis.

I updated the code, please try again.


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

0 Kudos
jithinraj
Contributor
Contributor
Jump to solution

Thanks very much Luc...it works fore me...

0 Kudos
Viewaskew
Enthusiast
Enthusiast
Jump to solution

Hi,

I’m also getting the CSV created but it’s empty. I ran the below and got a positive response indicating 4 counts which I assume are 4 VMs created.

PowerCLI C:\> Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples ([int]::MaxValue) |
Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} |

Measure-Object

Count    : 4
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

This is the script Im running in a .PS1 file. Does it matter if this is run on 5.1 as was written when 4.1 was the most current?

# How many days in the past to start from
$start = (Get-Date).AddDays(-30)

# The more days back, the higher this number should be.
$eventNr = 9999

$report = @()

Get-VIEvent -Start $start -MaxSamples $eventNr |`
  Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} | % {
     $row = "" | Select Date, User, VmName, Cluster, Host, Msg
     $row.Date = $_.createdTime
     $row.Msg = $_.fullFormattedMessage
     $row.User = $_.userName
     $row.VMName = $_.vm.name
     $t = New-Object VMware.Vim.ManagedObjectReference
     $t.type = $_.computeResource.computeResource.type
     $t.Value = $_.computeResource.computeResource.Value
     $row.Cluster = (Get-View $t).Name
     $t.type = $_.host.host.type
     $t.Value = $_.host.host.Value
     $row.Host = (Get-View $t).Name
     $report += $row
}
$report | Export-Csv "C:\VMCreated.csv" -NoTypeInformation


Any ideas why this is drawing a blank when I know there are servers that were created less than 7 days ago?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is most probably due to the MaxSamples parameter.

Try changing this line

$eventNr = 9999

into this

$eventNr = [int]::MaxValue


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

0 Kudos
Viewaskew
Enthusiast
Enthusiast
Jump to solution

Spot on LucD! That did the trick...Thank you.

0 Kudos