I'm looking for a powerCLI script that will create a log of storage vmotions done for the month. I think it will involve using Get-ViEvent but not sure how to corelate the VM name, source datastore, destination datastore, and date into one report. Ideally I would like to see it like this.
Date, VM Name, Source_datastore, Destination_datastore
Thank you,
You can fetch the svMotions like this
$events = Get-VIEvent -MaxSamples [int]::MaxValue -Start (Get-Date).AddMonths(-1) | where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate"}
but afaik the source and destination datastores are not in the event.
It could be that this is changed in vSphere 5 (don't have access to a vSphere 5 box right now).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can fetch the svMotions like this
$events = Get-VIEvent -MaxSamples [int]::MaxValue -Start (Get-Date).AddMonths(-1) | where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate"}
but afaik the source and destination datastores are not in the event.
It could be that this is changed in vSphere 5 (don't have access to a vSphere 5 box right now).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Looks like a good start. With powercli 5.0 I get the following
That expression should be in parenthesis
$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMonths(-1) | where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate"}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi htwnrver,
I am not sure why [int]::MaxValue is not working - I believe this is specifying to the cmdlet to use the maximum supported value for this type of query. So instead of "[int]::MaxValue" you could try replacing that with a standard integer value such as 500. This should in theory get the last 500 samples then and the cmdlet should work for you.
*edit* - thanks LucD Parenthesis explains why it wasn't working!
Also another small typo correction for LucD's script - in the DescriptionId part - it is missing the "s". Furthermore, for vSphere 5 I believe the DescriptionId is a bit different. Try the following:
$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMonths(-1) |where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "StorageResourceManager.applyRecommendation"}
StorageResourceManager.applyRecommendation is what I get as a DescriptionId for tasks when I storage vMotion a VM in my vSphere 5 lab environment, so the above should work.
Thanks for catching the typo, I corrected the missing 's'.
The [int]::MaxValue is not working because parameter type checking. The cmdlet sees this coming in as a [string], hence the message.
With the parenthesis we force the PS engine to first convert the expression to an actual [int].
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ah that makes sense - the same then in mathematics where the parenthesis are handled first in calculations? MaxValue is working nicely now with those parenthesis added - thanks.
I have tested in a vSphere 4.0 environment - Info.DescriptionId has a value of VirtualMachine.relocate in 4.0 (and 4.1 too I assume). In my vSphere 5 lab, it appears to be Info.DescriptionId with a value of "StorageResourceManager.applyRecommendation" for storage vMotions...
I was curious to know why I was getting different results with storage vMotion using vSphere 5 and using the above examples to fetch svMotion events, so I had a bit of a further play around with this in my lab setup this evening.
The reason why I saw svMotions only when searching for events using this cmdlet:
$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMonths(-1) | Where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "StorageResourceManager.applyRecommendation"} | Select CreatedTime, UserName, FullFormattedMessage
is because of SDRS and Datastore Clusters it would seem. When you svMotion a VM between Datastore Clusters, the event seems to be come through under Recent Tasks as "Apply Storage DRS recommendations". However, if you svMotion the VM into a normal Datastore, which is not part of a Datastore cluster, then it comes through as "Relocate virtual machine". This is the difference, and hence the reason when I first tried earlier that I saw the event only when searching for "StorageResourceManager.applyRecommendation". (I was testing svMotion by moving a VM between two datastore clusters).
Now, I disassembled a datastore cluster to create a couple of normal datastores and migrated some VMs around these - the events now come up as expected as "Relocate virtual machine".
So what that means is if you are using Datastore Clusters with vSphere 5 only, then you should use the example above in this post. If you are using normal datastores in vSphere 4 or 5, then use Luc's example in his first post. Or, if you want to cover all your bases and are using both normal Datastores and Datastore clusters, then the following should work:
$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMonths(-1) | Where { $_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate" -or $_.Info.DescriptionId -eq "StorageResourceManager.applyRecommendation"} | Select CreatedTime, UserName, FullFormattedMessage
This will give you a list of both types of storage vMotion that have occurred.
Here is an example in my environment (vSphere 5) where I have svMotioned between DS clusters and normal Datastores:
Message was edited by: Shoganator - Syntax highlighting!
Ah - we are only licensed for Enteprise so havne't got to play with StorageDRS yet.
Aha, that answers which one you need to use then. Anyway, hopefully the above is helpful for others looking at what you were after that have a StorageDRS / Datastore cluster environment!
This looks good, But if we get the "Target" & "VMname" also that would be great.. can some one help me..
Have a look at my Get the vMotion/svMotion history post.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I'm trying to fetch the storage vmtion logs to find out what caused the storage exhausted, but it throws an error. Need your help
PS D:\vmk> .\svmlogs.ps1
Get-VIEvent : Cannot bind parameter 'MaxSamples'. Cannot convert value "[int]::MaxValue" to type "System.Int32".
Error: "Input string was not in a correct format."
At D:\vmk\svmlogs.ps1:1 char:35
+ $events = Get-VIEvent -MaxSamples [int]::MaxValue -Start (Get-Date).A ...
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-VIEvent], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetEvent
Thanks
vmk
Place brackets around the value
$events = Get-VIEvent -MaxSamples ([int]::MaxValue)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Can we fetch for last 1 day or 1 week first ? I want see whether its really able to collect data because 1 months is long data.
Week and day both want to try
thanks
vmk
In my post Get The VMotion/SvMotion History, I created the function Get-MotionHistory.
That function accepts Days, Hours or Minutes as a parameter.
Have a look at the Sample Usage section in that post.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Tried, but it runs forever. Can it be faster ?
$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) |
where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate"}
You could use my Get-VIEventPlus function, which is included in that post I referenced in my previous reply.
But retrieving events from vCenter is not very fast I'm afraid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello @LucD I have been trying to get the following information from currently running s-vMotion tasks
Start Time, SrcHost, SrcStorage, DstHost DstStorage & Percent complete when s-vMotioning to a datastore cluster.
might you be able to point me in the right direction?
You can check the TaskEvent related to the svMotion task.
You might give my Get-TaskPlus form Task Data Mining – An improved Get-Task a try.
Use the Realtime switch.
Not sure if all the info you are after is in there though.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference