VMware Cloud Community
hrlinuxdt0312
Contributor
Contributor

Script to retrieve VMs Created-On Date & Created-By date & add to existing CustomFields

Recently required an automation script which obtains VM(s) CreatedOn & CreatedBy date and inputs as a VM(s) CustomField/Annotation, which is viewable on the VM's Summary page. Hopefully this helps others in need of such a script.

Please post comment and concerns and if it was successful.

As a Note, you will need to have Admin-writes on vSphere to be able to configure CustomFields.

####Script_ Start###

##Script to retrieve VMs Created-On Date & Created-By date & add to existing CustomFields

function Get-BuiltInfo {

<#

   .Synopsis

     Retrieve CreatedOn & CreatedBy Information

   .Description

    This function contains a "WhatIfPreference" which needs to be changed from '$true' to "$false" in order to

     successfully configure the CustomFields properties: "CreatedOn & CreatedBy". If these do NOT exist, then

    those 2 properties need to be created or renamed to exsiting property name

   .Parameter name

    This parameter allows single or multiple string representing vm name by cmdlet "Get-VM -Name "HMR,HMR2,HMAR2," or "Get-VM BillyBoB".

   

   .Parameter types

    Obtains results for each VMs via Get-ViEvent and returns results based on only events with selected types:

    "VmBeingDeployedEvent", "VmCreatedEvent", "VmRegisteredEvent" & "VmClonedEvent"

    

   .Parameter category

    Possible categories are : warning, info, error. Please use this parameter if you

    want to filter events.

   .Notes

     Caution on "WhatIfPreference=$false", if no VM is specified, it will input attempt to retrieve for ALL VMs

    NAME:  Get-BuiltInfo

   

    AUTHOR: Hasan Rasheed

    LASTEDIT: 08/22/2014

   .Examples

       Get-VM Test1 | Get-BuiltInfo

    --> Will retrieve information for specified single VM & Sets the CustomFields for CreatedOn & CreatedBy

   

    Get-VM Test1,2,3 | Get-BuiltInfo

    --> Will retrieve information for specified Multple VM(s) & Sets the CustomFields for CreatedOn & CreatedBy

   

    Get-VM | Get-BuiltInfo

    --> Will retrieve information for ALL VMs for each of the vCenters Connected on PowerCLI session & Sets the CustomFields for CreatedOn & CreatedBy

#>

    

    param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]

        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vms)

         

        BEGIN {

            }       

              process {

            

          $WhatIfPreference = $true

         

                 Foreach ($vm in $vms){

                    $Event = Get-VIEvent -Entity $vm -Types Info -Start 01/01/2005 -MaxSamples 30000 | Where { $_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent" -or $_.Gettype().Name -eq "VmClonedEvent" }

                     If (($Event | Measure-Object).Count -eq 0){

                            #$User = "Unknown"

                            #$Created = "Unknown"

                        }

                        Else {

                            $User = ($Event.Username.Split('\')[1]).ToLower()

                            $Created = ($Event.CreatedTime.ToString('MM/dd/yyyy'))

                        }

                    Write "Updating CustomFields for $($vm.Name)"

                    Write-Host -ForegroundColor DarkMagenta "CreatedBy $User"

                    $vm | Set-CustomField -Name "CreatedBy" -Value $User | Out-Null

                    Write-Host -ForegroundColor DarkMagenta "CreatedOn $Created"

                    $vm | Set-CustomField -Name "CreatedOn" -Value $Created | Out-Null

                                }

                            }

                         END {

                        # Returns Info

                        Return $User

                        Return $Created

            }   

}

####Script_END###

0 Kudos
0 Replies