That was it! Thanks! ![]()
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...
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
I saw the script and going to try it out. Except, where do you run the script (on each esx host, etc) ?
You run this script on a PC that has PowerCLI installed.
You connect first to your Virtual Center (with the Connect-ViServer cmdlet).
New to all the scripting and using the utilities. I believe I have everything installed, but i'm getting errors when I execute
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.
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
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.
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.
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
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
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 ?
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
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 ?