Update Custom Property Field in VMM with the Last Update Date

We recently added four custom fields to Virtual Machine Manager (VMM) to help us keep track of application owners, server owners, when in our patch schedule servers were being updated, and the last time a server was updated.  In order to keep the field for last update current, I wrote a script to get the date the server was last updated, compare it to the value currently in the field, and if the new date was later than the value in the custom property, to change it.  Now there are certainly some issues with this script that need improving.  The main issue being that if there are multiple updates with different update days, it doesn’t pick out each unique one, or the latest one, and so updates the property in VMM multiple times.  I have this script running as a scheduled task on the VMM server once a week and it is working great.

#Import VMM Module
Import-Module -Name "virtualmachinemanager"

#Get the list of all the VM's in VMM
$VMList = Get-SCVirtualMachine -All

ForEach ($VM in $VMList){
 
    #Get the Custom Property
    $CustomLastUpdated = Get-SCCustomProperty -Name "Last Updated"
 
    #Get the value of the custom property
    $VMLastUpdateDate = Get-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomLastUpdated

    #Get only the Value and not the other stuff
    $LastUpdateDate = $VMLastUpdateDate.Value

    #Changes a null value to this date since this custom property for most of the VM's is currently blank
    If($LastUpdateDate -eq $null){
        $LastUpdateDate = "01/01/2013"
        }
    
    #Get the date of any HotFixes installed after the last update date
    $VMHotFixDate = Get-HotFix -ComputerName $VM | Where-Object {$_.InstalledOn -GT $LastUpdateDate} | Select-Object InstalledOn -Unique | Sort-Object -Descending

    #Get the InstalledOn Date
    $NewDate = $VMHotFixDate.InstalledOn

    #Change the InstalledOn Date to a string so that the property value can be set
    $NewUpdateDate = $NewDate.ToString("MM/dd/yyyy")
 
    #Check to see if the NewUpdateDate is greater than the LastUpdateDate
    If($NewUpdateDate -gt $LastUpdateDate){
        #If it is greater, set the property value to the NewUpdateDate
        Set-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomLastUpdated -Value $NewUpdateDate
        }
        Else{
        #If it is not greater, set the property value to LastUpdateDate.  This will help populate the blank fields.
        Set-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomLastUpdated -Value $LastUpdateDate
        }
 
    }#End ForEach
  • Steve

    I am unable to get your script to run. Can you explain how you are running this

    Thanks

    • Jacob

      This script was never intended to be run in a different environment. It was just an example of something that could be done using VMM and PowerShell.