Automation

 View Only
Expand all | Collapse all

Script to find when and who created a VM

nareshunik

nareshunikMar 16, 2012 01:14 PM

  • 1.  Script to find when and who created a VM

    Posted Mar 11, 2012 05:12 PM

    Need a script to find when&who created a VM in "ClusterA"&"ClusterB"



  • 2.  RE: Script to find when and who created a VM

    Posted Mar 11, 2012 05:44 PM

    Take a look at Re: Get VM Create Date & Time. To run the script you need to connect to the vCenter Server first. The script produces an output like the following for each VM.

    Date    : 04.03.2012 14:32:48
    Msg     : Created virtual machine vMA-5.0.0.0-472630 on esxi5-01.lab.local in Datacenter
    User    : Administrator
    Cluster : Cluster1
    Host    : esxi5-01.lab.local

    André



  • 3.  RE: Script to find when and who created a VM

    Posted Mar 12, 2012 11:45 AM
    ---> .\test1.ps1
    Add-PSSnapin : The Windows PowerShell snap-in 'Quest.ActiveRoles.ADManagement'
    is not installed on this machine.
    At C:\users\cz3m7g\desktop\scripts\test1.ps1:7 char:14
    +     Add-PSSnapin <<<<  Quest.ActiveRoles.ADManagement
        + CategoryInfo          : InvalidArgument: (Quest.ActiveRoles.ADManagement
       :String) [Add-PSSnapin], PSArgumentException
        + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
       dPSSnapinCommand
    Finding creator for xxxxxxxxxxxx
    Adding info to xxxxxxxxxxxxxxx
    CreatedBy Unknown
    CreatedOn Unknown
    Finding creator for xxxxxxxxxxxxx
    Adding info to xxxxxxxxxxxxxxxx
    CreatedBy Unknown
    CreatedOn Unknown
    Finding creator for xxxxxxxxxxxxxxxxxx
    Adding info to xxxxxxxxxxxxxxxxxxxxx
    CreatedBy Unknown
    CreatedOn Unknown
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


  • 4.  RE: Script to find when and who created a VM

    Posted Mar 12, 2012 11:49 AM

    You need to install the Quest snapin first.



  • 5.  RE: Script to find when and who created a VM

    Posted Mar 12, 2012 11:52 AM

    without installing this Quest snapin is there any script to get the information?



  • 6.  RE: Script to find when and who created a VM

    Posted Mar 12, 2012 11:55 AM

    Which script are you using in fact ?

    The script Andre pointed you to doesn't use the Quest snapin.



  • 7.  RE: Script to find when and who created a VM

    Posted Mar 14, 2012 08:33 PM

    # How many days in the past to start from
    $start = (Get-Date).AddDays(-10)

    # 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, Msg, User, Cluster, Host
         $row.Date = $_.createdTime
         $row.Msg = $_.fullFormattedMessage
         $row.User = $_.userName
        
         $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

    ==============

    Can you give me correct script..

    I need a script to find who&when was the Vms created on "ClusterA" and "ClusterB"



  • 8.  RE: Script to find when and who created a VM

    Posted Mar 14, 2012 08:45 PM

    Just to make sure. Did you start the script from a PowerCLI or PowerShell window?

    Before running the script you need to connect to vCenter Server using the Connect-VIServer command.

    André



  • 9.  RE: Script to find when and who created a VM

    Posted Mar 14, 2012 08:50 PM

    Does this bring what you are looking for ?

    # How many days in the past to start from 
    $start
    = (Get-Date).AddDays(-10) # 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, Msg, User, Cluster, Host
         $row.Date = $_.createdTime      $row.Msg = $_.fullFormattedMessage      $row.User = $_.userName      $row.Cluster = (Get-View $_.computeResource.computeResource).Name      $row.Host = (Get-View $_.Host.Hostt).Name      $report += $row
    }
    $report


  • 10.  RE: Script to find when and who created a VM

    Posted Mar 14, 2012 09:10 PM

    I get the below error message

    i have connected to the vCenter using connect-viserver

    Get-VIEvent : Cannot validate argument on parameter 'MaxSamples'. The argument
    is null, empty, or an element of the argument collection contains a null value.
    Supply a collection that does not contain any null values and then try the com
    mand again.
    At C:\users\xxxxxx\desktop\test.ps1:4 char:38
    + Get-VIEvent -Start $start -MaxSamples <<<<  $eventNr | `Where-Object {$_.GetT
    ype().Name -eq "VmCreatedEvent"} | % {
        + CategoryInfo          : InvalidData: (:) [Get-VIEvent], ParameterBinding
       ValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
       ation.ViCore.Cmdlets.Commands.GetEvent



  • 11.  RE: Script to find when and who created a VM

    Posted Mar 14, 2012 09:13 PM

    You need to run the $eventNr = 9999 in your session.  Or replace it w/ 9999 in the cmdlet.



  • 12.  RE: Script to find when and who created a VM

    Posted Mar 14, 2012 09:18 PM

    i ran this command $eventNr = 9999. it worked

    Then when i run the script i don't get any error and no output it ends....



  • 13.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 01:14 PM

    anyone can help??????????????



  • 14.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 01:16 PM

    How does your script looks like right now?



  • 15.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 01:40 PM
    # How many days in the past to start from
    $start = (Get-Date).AddDays(-10)
    # 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, Msg, User, Cluster, Host
         $row.Date = $_.createdTime
         $row.Msg = $_.fullFormattedMessage
         $row.User = $_.userName
         $row.Cluster = (Get-View $_.computeResource.computeResource).Name
         $row.Host = (Get-View $_.Host.Hostt).Name
         $report += $row
    }
    $report

    ================

    First i connected to vCenter connect-viserver

    Second ran this command $eventNr = 9999 (works)

    Third when i ran the above script no error and no output...

    Kindly help



  • 16.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 01:53 PM

    There seems to be a back-tick before the Where-clause.

    Remove the back-tick and try again



  • 17.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 08:47 PM

    PowerCLI C:\users\xxxxxx\desktop\scripts\VM_created _date> $eventNr = 9999
    PowerCLI C:\users\xxxxxxxx\desktop\scripts\VM_created _date> & '.\vm created date.
    ps1'
    Unexpected token '}' in expression or statement.
    At C:\users\xxxxxxx\desktop\scripts\VM_created _date\vm created date.ps1:11 char
    :2
    + } <<<<
        + CategoryInfo          : ParserError: (}:String) [], ParseException
        + FullyQualifiedErrorId : UnexpectedToken

    PowerCLI C:\users\xxxxx\desktop\scripts\VM_created _date>

    ========

    After making changes i get the above error...



  • 18.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 08:50 PM

    It seems something went wrong with your copy, you're missing a closing curly brace somewhere (according to the error message)

    Can you attach the script you are actually using ?



  • 19.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 09:54 PM

    Below error

    PowerCLI C:\users\cz3m7g\desktop\scripts\VM_created _date> $eventNr = 9999
    PowerCLI C:\users\cz3m7g\desktop\scripts\VM_created _date> & '.\vm created date.
    ps1'
    Select-Object : A positional parameter cannot be found that accepts argument '$
    null'.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:5 char:
    24
    +      $row = "" | Select <<<<  Date, Msg, User, Cluster, Host     $row.Date =
    $_.createdTime
        + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterB
       indingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
       .Commands.SelectObjectCommand

    Property 'Msg' cannot be found on this object; make sure it exists and is setta
    ble.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:6 char:
    11
    +      $row. <<<< Msg = $_.fullFormattedMessage
        + CategoryInfo          : InvalidOperation: (Msg:String) [], RuntimeExcept
       ion
        + FullyQualifiedErrorId : PropertyNotFound

    Property 'User' cannot be found on this object; make sure it exists and is sett
    able.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:7 char:
    11
    +      $row. <<<< User = $_.userName
        + CategoryInfo          : InvalidOperation: (User:String) [], RuntimeExcep
       tion
        + FullyQualifiedErrorId : PropertyNotFound

    Property 'Cluster' cannot be found on this object; make sure it exists and is s
    ettable.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:8 char:
    11
    +      $row. <<<< Cluster = (Get-View $_.computeResource.computeResource).Name
        + CategoryInfo          : InvalidOperation: (Cluster:String) [], RuntimeEx
       ception
        + FullyQualifiedErrorId : PropertyNotFound

    Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
    ll or empty. Supply an argument that is not null or empty and then try the comm
    and again.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:9 char:
    27
    +      $row.Host = (Get-View <<<<  $_.Host.Hostt).Name
        + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
       idationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
       ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

    Select-Object : A positional parameter cannot be found that accepts argument '$
    null'.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:5 char:
    24
    +      $row = "" | Select <<<<  Date, Msg, User, Cluster, Host     $row.Date =
    $_.createdTime
        + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterB
       indingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
       .Commands.SelectObjectCommand

    Property 'Msg' cannot be found on this object; make sure it exists and is setta
    ble.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:6 char:
    11
    +      $row. <<<< Msg = $_.fullFormattedMessage
        + CategoryInfo          : InvalidOperation: (Msg:String) [], RuntimeExcept
       ion
        + FullyQualifiedErrorId : PropertyNotFound

    Property 'User' cannot be found on this object; make sure it exists and is sett
    able.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:7 char:
    11
    +      $row. <<<< User = $_.userName
        + CategoryInfo          : InvalidOperation: (User:String) [], RuntimeExcep
       tion
        + FullyQualifiedErrorId : PropertyNotFound

    Property 'Cluster' cannot be found on this object; make sure it exists and is s
    ettable.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:8 char:
    11
    +      $row. <<<< Cluster = (Get-View $_.computeResource.computeResource).Name
        + CategoryInfo          : InvalidOperation: (Cluster:String) [], RuntimeEx
       ception
        + FullyQualifiedErrorId : PropertyNotFound

    Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
    ll or empty. Supply an argument that is not null or empty and then try the comm
    and again.
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\vm created date.ps1:9 char:
    27
    +      $row.Host = (Get-View <<<<  $_.Host.Hostt).Name
        + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
       idationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
       ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

    PowerCLI C:\users\cz3m7g\desktop\scripts\VM_created _date>

    =============

    script

    # How many days in the past to start from $start = (Get-Date).AddDays(-10)
    # 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, Msg, User, Cluster, Host     $row.Date = $_.createdTime
         $row.Msg = $_.fullFormattedMessage
         $row.User = $_.userName
         $row.Cluster = (Get-View $_.computeResource.computeResource).Name
         $row.Host = (Get-View $_.Host.Hostt).Name
         $report += $row
    }
    $report



  • 20.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 10:33 PM

    Try the attached script



  • 21.  RE: Script to find when and who created a VM

    Posted Mar 16, 2012 11:56 PM

    The scripts works.... you are great.... :smileyhappy:

    It gives me output for vm created last 10days.. if i need for the VM it was created for the last 400days and Only for two cluster "ClusterA" and "ClusterB", export in csv or xls...

    That will great.. :smileyhappy:



  • 22.  RE: Script to find when and who created a VM

    Posted Mar 17, 2012 01:09 AM

    Try the attached script



  • 23.  RE: Script to find when and who created a VM

    Posted Mar 19, 2012 02:20 PM

    I get the below error message

    Get-View : 3/19/2012 7:41:50 AM    Get-View        View with Id  'HostSystem-ho
    st-6712' was not found on the server(s).
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\who-created-vm.ps1:15 char:
    28
    +     $row.VMHost = (Get-View <<<<  $_.Host.Host).Name
        + CategoryInfo          : ObjectNotFound: (:) [Get-View], VimException
        + FullyQualifiedErrorId : Core_GetView_WriteNotFoundError,VMware.VimAutoma
       tion.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

    Get-VIEvent : 3/19/2012 9:02:36 AM    Get-VIEvent        The object has already
    been deleted or has not been completely created
    At C:\users\cz3m7g\desktop\scripts\VM_created _date\who-created-vm.ps1:8 char:1
    2
    + Get-VIEvent <<<<  -Start $start -MaxSamples $eventNr | Where-Object {$_.GetTy
    pe().Name -eq "VmCreatedEvent"} | % {
        + CategoryInfo          : NotSpecified: (:) [Get-VIEvent], VimException
        + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
       n.ViCore.Cmdlets.Commands.GetEvent



  • 24.  RE: Script to find when and who created a VM

    Posted Mar 19, 2012 03:00 PM

    It looks like you found some events for VMs that were created on ESX(i) hosts that do not exist anymore.



  • 25.  RE: Script to find when and who created a VM

    Posted Mar 19, 2012 03:22 PM

    yes,

    we have only ESXi servers. No esx. is it possible to get the Vm created date?



  • 26.  RE: Script to find when and who created a VM

    Posted Mar 19, 2012 06:09 PM

    What is the final soulution!!!!!

    Waiting for the solution ?

    abirhasan   


  • 27.  RE: Script to find when and who created a VM

    Posted Mar 19, 2012 06:19 PM

    @abirhasan

    One of the characteristics an IT person needs is patience. So just be patient an monitor this thread!

    André



  • 28.  RE: Script to find when and who created a VM

    Posted Nov 09, 2012 05:09 PM

    Thanks man. The script is very valuable.



  • 29.  RE: Script to find when and who created a VM

    Posted Dec 19, 2012 07:00 PM

    Script worked great for me...

    Is there a way to output the data to excel or a csv file? It produces a high number and I cant see all the info....

    Thanks!

    And when I say script, Im referring to this one:

    # How many days in the past to start from
    $start = (Get-Date).AddMonths(-1)
    $events = "VmCreatedEvent","VMClonedEvent","VMDeployedEVent"
    $eventNr = [int]::MaxValue

    $report = @()
    Get-VIEvent -Start $start -MaxSamples $eventNr |
        Where-Object {$events -contains $_.GetType().Name} | % {
        $row = "" | Select Date, Msg, User, Cluster, VMHost 
        $row.Date = $_.createdTime  
        $row.Msg = $_.fullFormattedMessage  
        $row.User = $_.userName  
        $row.Cluster = (Get-View $_.computeResource.computeResource).Name  
        $row.VMHost = (Get-View $_.Host.Host).Name  
        $report += $row
    }
    $report


  • 30.  RE: Script to find when and who created a VM

    Posted Dec 19, 2012 07:14 PM

    To export the ouput of the script to a .csv file you can change the last line of the script into:

    $report | Export-CSV -Path VMInfo.csv -NoTypeInformation -UseCulture


  • 31.  RE: Script to find when and who created a VM

    Posted Feb 21, 2014 06:43 AM

    I am running the scripts.. in my env its taking  more than 1 hr to get the o/p

    Is the same for all of you ?

    I need the o/p to a html .. i tried "| ConvertTo-Html | Set-Content c:\scripts\test.html".. but html does not have the info i need ...

    my script

    PowerCLI C:\Windows\system32> $CDT = Get-Date

    PowerCLI C:\Windows\system32> $vm = Get-VM -Location ABC

    PowerCLI C:\Windows\system32> $vm | Get-VIEvent -Types Info -Start $CDT.AddDays(-30) -Finish $CDT |Where { $_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent"} | Select UserName, CreatedTime, FullFormattedMessage | Format-Table -AutoSize |fl * | ConvertTo-Html | Set-Content c:\scripts\test.html

    My 1 more need is .. next is to get the VM's cpu/memory/disk info in same table

    can anyone help for the html & extended attributes needed info pls

    thanks in advance