PowerCLI

 View Only
Expand all | Collapse all

Where was my VMs located in the past?

  • 1.  Where was my VMs located in the past?

    Posted Jun 22, 2010 12:17 PM

    I need to know on which ESX host the VMs are located in the past.

    I found a script who lists the current esx host where the VMs are located:

    $vms = Get-VM | Sort Name

    $vms | Select Name, @{N="VMHost";E={Get-VMHost -VM $_}}

    How I can List the VMs history location? I.e. for the last 3 months?

    Thanks in advance

    Oliver



  • 2.  RE: Where was my VMs located in the past?

    Posted Jun 22, 2010 12:54 PM

    You will have to investigate the events for that.

    Have a look at . It only lists DRS initiated vMotions but you can easily adapt this for other types of vMotions.

    Is this waht you were looking for ?

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 3.  RE: Where was my VMs located in the past?

    Posted Jun 22, 2010 01:14 PM

    LucD,

    I've got some errors when running the script. What does this line of code do?

    http://Reflection.Assembly::LoadWithPartialName("vmware.vim")

    Thank in advance

    Oliver



  • 4.  RE: Where was my VMs located in the past?

    Posted Jun 22, 2010 01:36 PM

    Use the attached script from that thread.

    The forum SW has problems with square brackets.

    You can in fact leave out that line, it was used to make sure the PowerCLI assemblies were loaded, but that is now done automatically.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 5.  RE: Where was my VMs located in the past?

    Posted Jun 22, 2010 03:35 PM

    LucD,

    thank you for your answer. Unfortunately I´ve got additional errors if i run the script on the vSphere architecture:

    Unable to find type [http://VMware.Vim.TaskHistoryCollector........

    Regards

    Oliver



  • 6.  RE: Where was my VMs located in the past?

    Posted Jun 22, 2010 04:36 PM

    Strange.

    From where are you running the script ? From the PowerCLI prompt ? Or something else ?

    And what version of PowerCLI do you have installed ? Use

    Get-PowerCLIVersion
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 7.  RE: Where was my VMs located in the past?

    Posted Jun 22, 2010 09:49 PM

    Have a look at Events – Part 8 – vMotion history.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 8.  RE: Where was my VMs located in the past?

    Posted Jun 23, 2010 07:50 AM

    LucD,

    great! I´ve run your script "Events – Part 8 – vMotion history" and got all the informations I need to know in a csv file. Thank you for supporting me.

    But as I tryed to run it a second time the operation failed with this error:

    A parameter cannot be found that matches parameter name 'Property'.

    At :line:38 char:14

    + $events | % <<<< {

    I don´t know whats going on.

    I am using VMware vSphere PowerCLI 4.0 U1 build 208462

    and the PowerGuiScript Editor 1.8.0.815

    Regards

    LarsOliver



  • 9.  RE: Where was my VMs located in the past?

    Posted Jun 23, 2010 04:13 PM

    Do you get that same error when you run the script a 2nd time from the PowerCLI prompt ?

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 10.  RE: Where was my VMs located in the past?

    Posted Jun 25, 2010 04:43 PM

    LucD,

    thanks, now it works :smileyhappy: , great script !

    But I,ve got only the following colums for my 445 VMs:

    VM Time1 Host1 DRS1

    The DRS1 Colum shows only FALSE entrys. It seems that no VM has moved in the last 30 hours. I will check this again with a longer time period at monday.

    Thanks

    LarsOliver



  • 11.  RE: Where was my VMs located in the past?

    Posted Jun 25, 2010 06:50 PM

    Or move 1 or 2 guests before you run the script.

    Make sure that the guests selection (line 11-13) contains these VMs.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 12.  RE: Where was my VMs located in the past?

    Posted Jun 28, 2010 07:29 PM

    LucD,

    thanks for your reply.

    Same status as Friday. I moved one VM. The script detects the new esx, but do not show the esx, where the vm came from, in the table.

    I´ve done a little modification to selected the cluster with this command:

    Get-Cluster -name "mycluster" | Get-VM | %{

    $vmHash[http://$_.Name|http://$_.Name] = $_.Host

    }

    The correct VMs are displayed in the table, but no movements. Independently if I set the $hours variable to 24, 240 or 750 (1 month). The Size of the csv doesn´t change, it is the same (6KB) and also the number of the VMs.

    I wrote the messages into a log file and found the following message three times.

    New-Object : A parameter cannot be found that matches parameter name 'Property'.

    At F:\CPM\vm_motion_history.ps1:62 char:51

    +

    The line nr. 62 does not match to the line nr. of your original script, because I added some lines ontop of the script for automatic logon. Do you know what this message means. Is -proterty possibly not available in Powershell V1.0 (We use this version on our servers).

    I hope you can help me, because we need the information where the VMs reside in the past for an audit. Because Oracle Licencing :smileysad: depends on how many ESX-hosts the VM runs in total.

    Thanks

    LarsO



  • 13.  RE: Where was my VMs located in the past?

    Posted Jun 28, 2010 07:38 PM

    No, PowerShell v1 doesn't know the hash -Property.

    But you can easily adapt this for PowerShell v1.

    Replace these lines

    ...
                     $migrations  += New-Object PSObject -Property @{
                     VMname = $task.EntityName
                     Source = $event.Host.Name
                     Destination = $event.DestHost.Name
                     Start = $task.StartTime
                     Finish = $task.CompleteTime
                     Result = $task.State
                     User = $task.Reason.UserName
                     DRS = &{if($task.DescriptionId -like "Drm.*")
    ...
    

    by these lines

    ..
                     $row = "" | Select VMname,Source,Destination,Start,Finish,Result,User,DRS
                     $row.VMname = $task.EntityName
                     $row.Source = $event.Host.Name
                     $row.Destination = $event.DestHost.Name
                     $row.Start = $task.StartTime
                     $row.Finish = $task.CompleteTime
                     $row.Result = $task.State
                     $row.User = $task.Reason.UserName
                     $row.DRS = &{if($task.DescriptionId -like "Drm.*")
                     $migrations += $row
    ...
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 14.  RE: Where was my VMs located in the past?

    Posted Jun 29, 2010 10:08 AM

    LucD,

    I´ve exactly replaced that lines you suggest, but a new error occured:

    Missing statement block after if ( condition ). At :line:72 char:14+ $ <<<< migrations += $row

    while($tasks){

    $tasks | where {$tgtTaskDescriptions -contains $_.DescriptionId} | % {

    $task = $_

    $eFilter = New-Object VMware.Vim.EventFilterSpec

    $eFilter.eventChainId = $task.EventChainId

    $eCollector = Get-View ($eventMgr.CreateCollectorForEvents($eFilter))

    $events = $eCollector.ReadNextEvents($eventnumber)

    while($events){

    $events | % {

    $event = $_

    switch($event.GetType().Name){

    "VmBeingHotMigratedEvent" {

    $row = "" | Select VMname,Source,Destination,Start,Finish,Result,User,DRS

    $row.VMname = $task.EntityName

    $row.Source = $event.Host.Name

    $row.Destination = $event.DestHost.Name

    $row.Start = $task.StartTime

    $row.Finish = $task.CompleteTime

    $row.Result = $task.State

    $row.User = $task.Reason.UserName

    $row.DRS = &{if($task.DescriptionId -like "Drm.*")

    $migrations += $row

    }

    }

    Default {}

    }

    }

    $events = $eCollector.ReadNextEvents($eventnumber)

    }

    $ecollection = $eCollector.ReadNextEvents($eventnumber)

    1. By default 32 event collectors are allowed. Destroy this event collector.

    $eCollector.DestroyCollector()

    }

    $tasks = $tCollector.ReadNextTasks($tasknumber)

    }



  • 15.  RE: Where was my VMs located in the past?
    Best Answer

    Posted Jun 29, 2010 11:01 AM

    My mistake, the new line for the DRS property was not complete.

    Attached the script with the PS v1 way of creating the object.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 16.  RE: Where was my VMs located in the past?

    Posted Jun 29, 2010 04:00 PM

    LucD,

    Great Script :smileyhappy:

    Now it also run on Powershell V1. I´ve done some little modifications, but the main reason was that it was not compatible to Powershell V1.

    Thank you very much!

    Regards

    LarsO



  • 17.  RE: Where was my VMs located in the past?

    Posted Jun 23, 2010 07:50 AM

    LucD,

    A second Question:

    $report | Export-Csv "C:\vMotion-history.csv" -NoTypeInformation -UseCulture

    What does the parameter -UseCulture do?

    I cannot found a description using:

    get-help Export-Csv -detailed

    Thanks and Reggards

    LarsOliver



  • 18.  RE: Where was my VMs located in the past?

    Posted Jun 23, 2010 04:13 PM

    The -UseCulture parameter is new in PowerShell v2.

    The help says "Use the list separator for the current culture as the item delimiter. The default is a comma (,).".

    I would strongly advice you to upgrade to PowerShell v2 (if you haven't done so already).

    ____________

    Blog: LucD notes

    Twitter: lucd22