11 Replies Latest reply on Mar 30, 2013 6:03 PM by LucD

    Log of storage vmotions

    htwnrver Novice

      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,

        • 1. Re: Log of storage vmotions
          LucD Guru
          vExpertUser ModeratorsCommunity Warriors

          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).

          • 2. Re: Log of storage vmotions
            htwnrver Novice

            Looks like a good start.  With powercli 5.0 I get the following


            Get-VIEvent : Cannot bind parameter 'MaxSamples'. Cannot convert value "[int]::
            MaxValue" to type "System.Int32". Error: "Input string was not in a correct for
            At line:1 char:34
            • 3. Re: Log of storage vmotions
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              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"}
              • 4. Re: Log of storage vmotions
                Shoganator Enthusiast

                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.

                • 5. Re: Log of storage vmotions
                  LucD Guru
                  vExpertUser ModeratorsCommunity Warriors

                  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].

                  • 6. Re: Log of storage vmotions
                    Shoganator Enthusiast

                    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...

                    • 7. Re: Log of storage vmotions
                      Shoganator Enthusiast

                      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!

                      • 8. Re: Log of storage vmotions
                        htwnrver Novice

                        Ah - we are only licensed for Enteprise so havne't got to play with StorageDRS yet.

                        • 9. Re: Log of storage vmotions
                          Shoganator Enthusiast

                          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!

                          • 10. Re: Log of storage vmotions
                            prabhurj Lurker

                            This looks good, But if we get the "Target" & "VMname" also that would be great.. can some one help me..

                            • 11. Re: Log of storage vmotions
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

                              Have a look at my Get the vMotion/svMotion history post.