Hello,
I'm searching for the code the get the 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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Lucd..... I'm not sure whether I'm doing correct way..
I'M sure last 7 days many VMs would have been created..
I copied this script to text file and saved as .ps1 format using ppercli i had run the script..
How do you start the .ps1 script ? From the PowerCLI prompt ?
Any error messages ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yea...I had run script from PowerCLI prompt. No error messages, Excuted completly produced CSV file also but if you open the file nothing will be there...
Thanks
Jithin
If you do the following from the PowerCLI prompt, no .ps1 file, are there any events returned ?
You should see a count different from 0.
Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples ([int]::MaxValue) | Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} | Measure-Object
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
I got the below error...
Get-VIEvent : Cannot bind parameter 'MaxSamples'. Cannot convert value "[int]::
MaxValue" to type "System.Int32". Error: "Input string was not in a correct for
mat."
At no.ps1:3 char:54
+ Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples <<<< [int]::MaxValue |
+ CategoryInfo : InvalidArgument: (:) [Get-VIEvent], ParameterBin
dingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
ion.ViCore.Cmdlets.Commands.GetEvent
My mistake, that should have been between parenthesis.
I updated the code, please try again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks very much Luc...it works fore me...
Hi,
I’m also getting the CSV created but it’s empty. I ran the below and got a positive response indicating 4 counts which I assume are 4 VMs created.
PowerCLI C:\> Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples ([int]::MaxValue) |
Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} |
Measure-Object
Count : 4
Average :
Sum :
Maximum :
Minimum :
Property :
This is the script Im running in a .PS1 file. Does it matter if this is run on 5.1 as was written when 4.1 was the most current?
# How many days in the past to start from
$start = (Get-Date).AddDays(-30)
# 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
Any ideas why this is drawing a blank when I know there are servers that were created less than 7 days ago?
That is most probably due to the MaxSamples parameter.
Try changing this line
$eventNr = 9999
into this
$eventNr = [int]::MaxValue
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Spot on LucD! That did the trick...Thank you.