Skip navigation
VMware

This Question is Answered (go to answer)

2 "helpful" answers available (6 pts)
6,483 Views 36 Replies Last post: Feb 8, 2012 4:32 AM by jithinraj RSS
wingnut76 Enthusiast 31 posts since
May 4, 2006
Currently Being Moderated
15. May 1, 2009 1:17 PM in response to: LucD
Re: Get VM Create Date & Time

That was it! Thanks!

wingnut76 Enthusiast 31 posts since
May 4, 2006
Currently Being Moderated
16. May 1, 2009 1:57 PM in response to: LucD
Re: Get VM Create Date & Time

Is it possible to get this into a format like a CSV to use in Excel? It appears to output the information in a tabular form grouped together like this:

 

Date    : 4/27/2009 2:14:31 PM

Msg     : Created virtual machine SERVERA on MyHost in MyDatacenter

User    : username_here

Cluster : ESX Server Farm

Host    : Myhost

 

I'd like to have:

 

Date,     User, VM Name, Cluster, Host, Full Message

 

Something like that...

LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
17. May 1, 2009 5:06 PM in response to: wingnut76
Re: Get VM Create Date & Time

Sure, no problem.

# 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

$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

 

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
jamunoz Novice 3 posts since
Jul 14, 2009
Currently Being Moderated
18. Jul 14, 2009 10:18 AM in response to: vmdavinci
Re: Get VM Create Date & Time

I saw the script and going to try it out.  Except, where do you run the script (on each esx host, etc) ?

LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
19. Jul 14, 2009 10:27 AM in response to: jamunoz
Re: Get VM Create Date & Time

You run this script on a PC that has PowerCLI installed.

You connect first to your Virtual Center (with the Connect-ViServer cmdlet).

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
jamunoz Novice 3 posts since
Jul 14, 2009
Currently Being Moderated
20. Jul 14, 2009 12:02 PM in response to: LucD
Re: Get VM Create Date & Time

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Attachments:
LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
21. Jul 14, 2009 12:13 PM in response to: jamunoz
Re: Get VM Create Date & Time

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  VMware Infrastructure Toolkit (for Windows) *A certification chain processed correctly, but terminated in a root certificate which isn't trusted by the trust provider.

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
jamunoz Novice 3 posts since
Jul 14, 2009
Currently Being Moderated
22. Jul 14, 2009 1:24 PM in response to: LucD
Re: Get VM Create Date & Time

 

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

 

 

 

 

 

Attachments:
LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
23. Jul 14, 2009 1:49 PM in response to: jamunoz
Re: Get VM Create Date & Time

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: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
jingleharish Enthusiast 42 posts since
Jun 4, 2009
Currently Being Moderated
24. Apr 16, 2011 8:35 AM in response to: LucD
Re: Get VM Create Date & Time

HI Luc,

 

I am using this script on a daily basis which is yielding seperate csv file. I need the data to be appended to a single csv file rather than a having seperating csv file for each day.

 

Your help us much appriciated.

RvdNieuwendijk Virtuoso User Moderators vExpert 1,261 posts since
Aug 3, 2009
Currently Being Moderated
25. Apr 16, 2011 9:57 AM in response to: jingleharish
Re: Get VM Create Date & Time

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

jithinraj Enthusiast 77 posts since
Oct 2, 2007
Currently Being Moderated
26. Feb 2, 2012 4:13 AM in response to: LucD
Re: Get VM Create Date & Time

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

LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
27. Feb 2, 2012 4:47 AM in response to: jithinraj
Re: Get VM Create Date & Time

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: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
jithinraj Enthusiast 77 posts since
Oct 2, 2007
Currently Being Moderated
28. Feb 2, 2012 7:33 PM in response to: LucD
Re: Get VM Create Date & Time

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

LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
29. Feb 3, 2012 4:49 AM in response to: jithinraj
Re: Get VM Create Date & Time

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: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com

Bookmarked By (0)

Share This Page

Communities