Virtually everything is poshable
Who deleted my VM ?
Today I had a colleague come to me and say someone had deleted his VM, he didn’t know when and thinks it may have been a couple of months ago, he didn’t know which host or which datastore it was in. could I tell him when and who. Hmmmm, time to start trawling through the logs I thought.
A quick Twit from @stahler told me that this would be in the Virtual Center Database so, here is a quick powershell which I used to find the culprit…
# Fill in the following information:$SqlServer = "MYDBSERVER";$SqlDB = "VMwareDataBase";$MYVM = "TESTSERVER1"$TypeofEvent = "vim.event.VmRemovedEvent"# The vim.event.VmRemovedEvent is a Removed action from VC you can also use :# vim.event.VmGuestShutdownEvent# vim.event.VmPoweredOffEvent# vim.event.VmConnectedEventFunction Read-VIDB ($SqlQuery){ # Setup SQL Connection $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlDB; Integrated Security = True" # Setup SQL Command $SqlCmd = New-Object System.Data.SqlClient.SqlCommand $SqlCmd.CommandText = $SqlQuery $SqlCmd.Connection = $SqlConnection # Setup .NET SQLAdapter to execute and fill .NET Dataset $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $SqlAdapter.SelectCommand = $SqlCmd $DataSet = New-Object System.Data.DataSet #Execute and Get Row Count $nRecs = $SqlAdapter.Fill($DataSet) if ($nRecs -gt 0) { # Do Stuff $dataSet.Tables | Select-Object -Expand Rows }} $SqlQuery = "SELECT CREATE_TIME, USERNAME, VM_NAME, HOST_NAME, EVENT_TYPE FROM VMWareDS.VPX_EVENT WHERE (VM_NAME = N'$MYVM') AND (EVENT_TYPE = '$TypeofEvent')"$MyResults = Read-VIDB $SqlQuery$MyResults
| Print article | This entry was posted by Virtu-Al on December 10, 2008 at 15:04, and is filed under powershell, vmware. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |







(3)
(13)
(0)
about 5 months ago
Is it possible to read the information from the vc directly ? Like:
$results = Get-VM | Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-1) | where {$_.fullformattedmessage -eq “Aufgabe: Virtuelle Maschine ausschalten” -or $_.fullformattedmessage -eq “Aufgabe: Herunterfahren des Gastbetriebssystems initiiert.”} | Sort CreatedTime -Descending | select createdtime, username, vm, fullformattedmessage
foreach ($result in $Results) {
$Details=”" | select createdtime, username, vm, fullformattedmessage
$Details.createdtime = $result.createdtime
$Details.username = $result.username
$Details.vm = $result.vm.name
$Details.FullFormattedMessage = $result.fullformattedMessage
}
$details | Export-csv -NoTypeInformation ‘C:\TEMP\shutdown.csv’
I want to read out which user shutdown which vm at which time
about 5 months ago
Yes it would
about 5 months ago
Thanks for the quick answer…
This script like posted before did not run.
Have it done now with an direct script into excel..