VMware Cloud Community
nareshunik
Enthusiast
Enthusiast

Script to find when and who created a VM

Need a script to find when&who created a VM in "ClusterA"&"ClusterB"

Reply
0 Kudos
30 Replies
a_p_
Leadership
Leadership

Take a look at Re: Get VM Create Date & Time. To run the script you need to connect to the vCenter Server first. The script produces an output like the following for each VM.

Date    : 04.03.2012 14:32:48
Msg     : Created virtual machine vMA-5.0.0.0-472630 on esxi5-01.lab.local in Datacenter
User    : Administrator
Cluster : Cluster1
Host    : esxi5-01.lab.local

André

Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

---> .\test1.ps1
Add-PSSnapin : The Windows PowerShell snap-in 'Quest.ActiveRoles.ADManagement'
is not installed on this machine.
At C:\users\cz3m7g\desktop\scripts\test1.ps1:7 char:14
+     Add-PSSnapin <<<<  Quest.ActiveRoles.ADManagement
    + CategoryInfo          : InvalidArgument: (Quest.ActiveRoles.ADManagement
   :String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
   dPSSnapinCommand
Finding creator for xxxxxxxxxxxx
Adding info to xxxxxxxxxxxxxxx
CreatedBy Unknown
CreatedOn Unknown
Finding creator for xxxxxxxxxxxxx
Adding info to xxxxxxxxxxxxxxxx
CreatedBy Unknown
CreatedOn Unknown
Finding creator for xxxxxxxxxxxxxxxxxx
Adding info to xxxxxxxxxxxxxxxxxxxxx
CreatedBy Unknown
CreatedOn Unknown
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Reply
0 Kudos
LucD
Leadership
Leadership

You need to install the Quest snapin first.


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

Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

without installing this Quest snapin is there any script to get the information?

Reply
0 Kudos
LucD
Leadership
Leadership

Which script are you using in fact ?

The script Andre pointed you to doesn't use the Quest snapin.


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

Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

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

# 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, Msg, User, Cluster, Host
     $row.Date = $_.createdTime
     $row.Msg = $_.fullFormattedMessage
     $row.User = $_.userName
    
     $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

==============

Can you give me correct script..

I need a script to find who&when was the Vms created on "ClusterA" and "ClusterB"

Reply
0 Kudos
a_p_
Leadership
Leadership

Just to make sure. Did you start the script from a PowerCLI or PowerShell window?

Before running the script you need to connect to vCenter Server using the Connect-VIServer command.

André

Reply
0 Kudos
LucD
Leadership
Leadership

Does this bring what you are looking for ?

# How many days in the past to start from 
$start
= (Get-Date).AddDays(-10) # 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, Msg, User, Cluster, Host
     $row.Date = $_.createdTime      $row.Msg = $_.fullFormattedMessage      $row.User = $_.userName      $row.Cluster = (Get-View $_.computeResource.computeResource).Name      $row.Host = (Get-View $_.Host.Hostt).Name      $report += $row
}
$report


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

Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

I get the below error message

i have connected to the vCenter using connect-viserver

Get-VIEvent : Cannot validate argument on parameter 'MaxSamples'. The argument
is null, empty, or an element of the argument collection contains a null value.
Supply a collection that does not contain any null values and then try the com
mand again.
At C:\users\xxxxxx\desktop\test.ps1:4 char:38
+ Get-VIEvent -Start $start -MaxSamples <<<<  $eventNr | `Where-Object {$_.GetT
ype().Name -eq "VmCreatedEvent"} | % {
    + CategoryInfo          : InvalidData: (:) [Get-VIEvent], ParameterBinding
   ValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.GetEvent

Reply
0 Kudos
Zsoldier
Expert
Expert

You need to run the $eventNr = 9999 in your session.  Or replace it w/ 9999 in the cmdlet.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

i ran this command $eventNr = 9999. it worked

Then when i run the script i don't get any error and no output it ends....

Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

anyone can help??????????????

Reply
0 Kudos
nielse
Expert
Expert

How does your script looks like right now?

@nielsengelen - http://foonet.be - VCP4/5
Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

# How many days in the past to start from
$start = (Get-Date).AddDays(-10)
# 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, Msg, User, Cluster, Host
     $row.Date = $_.createdTime
     $row.Msg = $_.fullFormattedMessage
     $row.User = $_.userName
     $row.Cluster = (Get-View $_.computeResource.computeResource).Name
     $row.Host = (Get-View $_.Host.Hostt).Name
     $report += $row
}
$report

================

First i connected to vCenter connect-viserver

Second ran this command $eventNr = 9999 (works)

Third when i ran the above script no error and no output...

Kindly help

Reply
0 Kudos
LucD
Leadership
Leadership

There seems to be a back-tick before the Where-clause.

Remove the back-tick and try again


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

Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

PowerCLI C:\users\xxxxxx\desktop\scripts\VM_created _date> $eventNr = 9999
PowerCLI C:\users\xxxxxxxx\desktop\scripts\VM_created _date> & '.\vm created date.
ps1'
Unexpected token '}' in expression or statement.
At C:\users\xxxxxxx\desktop\scripts\VM_created _date\vm created date.ps1:11 char
:2
+ } <<<<
    + CategoryInfo          : ParserError: (}:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

PowerCLI C:\users\xxxxx\desktop\scripts\VM_created _date>

========

After making changes i get the above error...

Reply
0 Kudos
LucD
Leadership
Leadership

It seems something went wrong with your copy, you're missing a closing curly brace somewhere (according to the error message)

Can you attach the script you are actually using ?


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

Reply
0 Kudos
nareshunik
Enthusiast
Enthusiast

Below error

PowerCLI C:\users\cz3m7g\desktop\scripts\VM_created _date> $eventNr = 9999
PowerCLI C:\users\cz3m7g\desktop\scripts\VM_created _date> & '.\vm created date.
ps1'
Select-Object : A positional parameter cannot be found that accepts argument '$
null'.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:5 char:
24
+      $row = "" | Select <<<<  Date, Msg, User, Cluster, Host     $row.Date =
$_.createdTime
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterB
   indingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.SelectObjectCommand

Property 'Msg' cannot be found on this object; make sure it exists and is setta
ble.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:6 char:
11
+      $row. <<<< Msg = $_.fullFormattedMessage
    + CategoryInfo          : InvalidOperation: (Msg:String) [], RuntimeExcept
   ion
    + FullyQualifiedErrorId : PropertyNotFound

Property 'User' cannot be found on this object; make sure it exists and is sett
able.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:7 char:
11
+      $row. <<<< User = $_.userName
    + CategoryInfo          : InvalidOperation: (User:String) [], RuntimeExcep
   tion
    + FullyQualifiedErrorId : PropertyNotFound

Property 'Cluster' cannot be found on this object; make sure it exists and is s
ettable.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:8 char:
11
+      $row. <<<< Cluster = (Get-View $_.computeResource.computeResource).Name
    + CategoryInfo          : InvalidOperation: (Cluster:String) [], RuntimeEx
   ception
    + FullyQualifiedErrorId : PropertyNotFound

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:9 char:
27
+      $row.Host = (Get-View <<<<  $_.Host.Hostt).Name
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
   idationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Select-Object : A positional parameter cannot be found that accepts argument '$
null'.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:5 char:
24
+      $row = "" | Select <<<<  Date, Msg, User, Cluster, Host     $row.Date =
$_.createdTime
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterB
   indingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.SelectObjectCommand

Property 'Msg' cannot be found on this object; make sure it exists and is setta
ble.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:6 char:
11
+      $row. <<<< Msg = $_.fullFormattedMessage
    + CategoryInfo          : InvalidOperation: (Msg:String) [], RuntimeExcept
   ion
    + FullyQualifiedErrorId : PropertyNotFound

Property 'User' cannot be found on this object; make sure it exists and is sett
able.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:7 char:
11
+      $row. <<<< User = $_.userName
    + CategoryInfo          : InvalidOperation: (User:String) [], RuntimeExcep
   tion
    + FullyQualifiedErrorId : PropertyNotFound

Property 'Cluster' cannot be found on this object; make sure it exists and is s
ettable.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:8 char:
11
+      $row. <<<< Cluster = (Get-View $_.computeResource.computeResource).Name
    + CategoryInfo          : InvalidOperation: (Cluster:String) [], RuntimeEx
   ception
    + FullyQualifiedErrorId : PropertyNotFound

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:9 char:
27
+      $row.Host = (Get-View <<<<  $_.Host.Hostt).Name
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
   idationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

PowerCLI C:\users\cz3m7g\desktop\scripts\VM_created _date>

=============

script

# How many days in the past to start from $start = (Get-Date).AddDays(-10)
# 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, Msg, User, Cluster, Host     $row.Date = $_.createdTime
     $row.Msg = $_.fullFormattedMessage
     $row.User = $_.userName
     $row.Cluster = (Get-View $_.computeResource.computeResource).Name
     $row.Host = (Get-View $_.Host.Hostt).Name
     $report += $row
}
$report

Reply
0 Kudos
LucD
Leadership
Leadership

Try the attached script


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

Reply
0 Kudos