Need a script to find when&who created a VM in "ClusterA"&"ClusterB"
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é
You need to install the Quest snapin first.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
without installing this Quest snapin is there any script to get the information?
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
# 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"
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é
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
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
You need to run the $eventNr = 9999 in your session. Or replace it w/ 9999 in the cmdlet.
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....
anyone can help??????????????
How does your script looks like right now?
================
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
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
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...
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
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
Try the attached script
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference