PowerCLI

 View Only
  • 1.  script to add 'last modified date' and 'last modifed by' to VM annotations

    Posted Sep 21, 2011 03:55 PM

    Unfortunately had a HDD fail on me that contained a script I found once that would update VM annotations for "Last Modified By" and "Last Modified Date", does anyone else use somethinig similar or can point me in the right direction?  google seems to be failing me.

    It was similar to the wonderful "who created that VM" script by Alan

    http://www.virtu-al.net/2010/02/23/who-created-that-vm/

    Also, is there an easy way to mass clear out annotations from a range of VM's?

    thanks!



  • 2.  RE: script to add 'last modified date' and 'last modifed by' to VM annotations

    Posted Sep 21, 2011 04:50 PM

    You can use Alan's Who Created that VM ? script with a some minor modifications.

    Connect-VIServer MYVISERVER
    # Uncomment the next line to test this script and tell you what it would do !
    # $WhatIfPreference = $true
    if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {      Add-PSSnapin VMware.VimAutomation.Core
    } if (-not (Get-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue)) {     Add-PSSnapin Quest.ActiveRoles.ADManagement
    }
    $VMs = Get-VM | Sort Name
    $VM = $VMs | Select -First 1
    if (-not $vm.CustomFields.ContainsKey("Last Modified Date")) {     Write-Host "Creating Last Modified Date Custom field for all VM's"
        New-CustomAttribute -TargetType VirtualMachine -Name CreatedBy | Out-Null} if (-not $vm.CustomFields.ContainsKey("Last Modified By")) {     Write-Host "Creating Last Modified By Custom field for all VM's"
        New-CustomAttribute -TargetType VirtualMachine -Name CreatedOn | Out-Null} foreach ($VM in $VMs){     $event = $VM | Get-VIEvent -Types Info |     where { $_.Gettype().Name -eq "VmReconfiguredEvent"} |     Sort-Object -Property CreatedTime -Descending |     select -First 1
       
    if($event){         $ModUser = "Unknown"
            $ModDate = $event.CreatedTime         if ($Event.Username -ne "" -and $Event.Username -ne $null) {             $ModUser = (Get-QADUser -Identity $Event.Username).DisplayName             if ($ModUser -eq $null -or $ModUser -eq ""){                 $ModUser = $Event.Username             }         }         Write "Adding info to $($VM.Name)"
           
    Write-Host -ForegroundColor Yellow "Last Modified By $ModUser"
            $VM | Set-CustomField -Name "Last Modified By" -Value $ModUser | Out-Null
            Write-Host -ForegroundColor Yellow "Last Modified Date $ModDate"
           
    $VM | Set-CustomField -Name "Last Modified Date" -Value $ModDate | Out-Null
        } }


  • 3.  RE: script to add 'last modified date' and 'last modifed by' to VM annotations

    Posted Sep 21, 2011 05:11 PM

    Wonderful Luc, thanks!

    I get the below error using powercli version 4.1.1, and doesn't update the  "last modified on" field.

    Set-CustomField : Cannot validate argument on parameter 'Value'. The argument  is null. Supply a non-null argument and try the command again.
    At  D:\new\modified_new.ps1:38 char:60
    +         $VM | Set-CustomField -Name  "LastModifiedOn" -Value <<<<  $Created | Out-Null
        +  CategoryInfo          : InvalidData: (:) [Set-CustomField],  ParameterBindingValidationException
        + FullyQualifiedErrorId :  ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetCustomField

    I modified the custom field name to match mine so i don't end up with double  entries, unless there's an easy way to mass erase annotations.

    i only see set-annotation and get-annotation commands.

    Message was edited by: monderick

    ________________________________________________________________________________________________________________________________

    found the problem :smileyhappy:  then i saw your 2nd post.

    fixed a few variables and working great with the below script.

    also found that      get-vm | Remove-CustomField -Name %name%    will mass delete annotations.

    cheers, Luc!  appreciate the support.

    Connect-VIServer MYVISERVER

    # Uncomment the next line to test this script and tell you what it would do !
    # $WhatIfPreference = $true
    if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
         Add-PSSnapin VMware.VimAutomation.Core
    }
    if (-not (Get-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue)) {
        Add-PSSnapin Quest.ActiveRoles.ADManagement
    }

    $VMs = Get-VM | Sort Name
    $VM = $VMs | Select -First 1
    if (-not $vm.CustomFields.ContainsKey("Last Modified Date")) {
        Write-Host "Creating Last Modified Date Custom field for all VM's"
        New-CustomAttribute -TargetType VirtualMachine -Name "Last Modified Date" | Out-Null}
    if (-not $vm.CustomFields.ContainsKey("Last Modified By")) {
        Write-Host "Creating Last Modified By Custom field for all VM's"
        New-CustomAttribute -TargetType VirtualMachine -Name "Last Modified By"| Out-Null}
    foreach ($VM in $VMs){
        $event = $VM | Get-VIEvent -Types Info |
        where { $_.Gettype().Name -eq "VmReconfiguredEvent"} |
        Sort-Object -Property CreatedTime -Descending |
        select -First 1
        if($event){
            $ModUser = "Unknown"
            $ModDate = $event.CreatedTime
            if ($Event.Username -ne "" -and $Event.Username -ne $null) {
                $ModUser = (Get-QADUser -Identity $Event.Username).DisplayName
                if ($ModUser -eq $null -or $ModUser -eq ""){
                    $ModUser = $Event.Username
                }
            }

            Write "Adding info to $($VM.Name)"
            Write-Host -ForegroundColor Yellow "Last Modified By $ModUser"
            $VM | Set-CustomField -Name "Last Modified By" -Value $ModUser | Out-Null
            Write-Host -ForegroundColor Yellow "Last Modified Date $ModDate"
            $VM | Set-CustomField -Name "Last Modified Date" -Value $ModDate | Out-Null
        }
    }



  • 4.  RE: script to add 'last modified date' and 'last modifed by' to VM annotations

    Posted Sep 21, 2011 05:48 PM

    My mistake, that should have said $modDate instead of $Created.

    I updated the script above.



  • 5.  RE: script to add 'last modified date' and 'last modifed by' to VM annotations

    Posted Sep 22, 2011 02:21 PM

    Is it possible to have the script only update the VM annotations only if they are not current?

    Would love to schedule this daily but not stomp over existing annotations if the VM wasn't modified since the last run.

    Thanks again, Luc.

    I also rememebered where i got the original code from for maintaining custom attributes, your PowerCLI reference book :smileyhappy:

    Message was edited by: monderick



  • 6.  RE: script to add 'last modified date' and 'last modifed by' to VM annotations

    Posted Sep 22, 2011 07:59 PM

    You can add a test to the last part to check if any of  the 2 retrieved values is different from the current ones.

    Something like this

    ...
    if
    ($VM.CustomFields["Last Modified By"] -ne $ModUser -or $VM.CustomFields["Last Modified Date"] -ne $ModDate){     Write "Adding info to $($VM.Name)"
        Write-Host -ForegroundColor Yellow "Last Modified By $ModUser"
        $VM | Set-CustomField -Name "Last Modified By" -Value $ModUser | Out-Null
        Write-Host -ForegroundColor Yellow "Last Modified Date $ModDate"
        $VM | Set-CustomField -Name "Last Modified Date" -Value $Created | Out-Null
    }


  • 7.  RE: script to add 'last modified date' and 'last modifed by' to VM annotations

    Posted Sep 22, 2011 08:58 PM

    Thanks, Luc, seems to be working great after a slight modification below to fix a typo.

    I'm presuming if it does not populate an annotation, it's because it cannot find the last mod date of the VM.

    Connect-VIServer MYVISERVER

    # Uncomment the next line to test this script and tell you what it would do !
    # $WhatIfPreference = $true
    if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
         Add-PSSnapin VMware.VimAutomation.Core
    }
    if (-not (Get-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue)) {
        Add-PSSnapin Quest.ActiveRoles.ADManagement
    }

    $VMs = Get-VM | Sort Name
    $VM = $VMs | Select -First 1
    if (-not $vm.CustomFields.ContainsKey("Last Modified On")) {
        Write-Host "Creating Last Modified On Custom field for all VM's"
        New-CustomAttribute -TargetType VirtualMachine -Name "Last Modified On" | Out-Null}
    if (-not $vm.CustomFields.ContainsKey("Last Modified By")) {
        Write-Host "Creating Last Modified By Custom field for all VM's"
        New-CustomAttribute -TargetType VirtualMachine -Name "Last Modified By"| Out-Null}
    foreach ($VM in $VMs){
        $event = $VM | Get-VIEvent -Types Info |
        where { $_.Gettype().Name -eq "VmReconfiguredEvent"} |
        Sort-Object -Property CreatedTime -Descending |
        select -First 1
        if($event){
            $ModUser = "Unknown"
            $ModDate = $event.CreatedTime
            if ($Event.Username -ne "" -and $Event.Username -ne $null) {
                $ModUser = (Get-QADUser -Identity $Event.Username).DisplayName
                if ($ModUser -eq $null -or $ModUser -eq ""){
                    $ModUser = $Event.Username
                }
            }
    if($VM.CustomFields["Last Modified By"] -ne $ModUser -or $VM.CustomFields["Last Modified On"] -ne $ModDate){
            Write "Adding info to $($VM.Name)"
            Write-Host -ForegroundColor Yellow "Last Modified By $ModUser"
            $VM | Set-CustomField -Name "Last Modified By" -Value $ModUser | Out-Null
            Write-Host -ForegroundColor Yellow "Last Modified On $ModDate"
            $VM | Set-CustomField -Name "Last Modified On" -Value $ModDate | Out-Null
            }
        }
    }



  • 8.  RE: script to add 'last modified date' and 'last modifed by' to VM annotations

    Posted Sep 06, 2023 03:31 AM

    Hi,

        i ma getting the below error while running the script. My requirement is to get  "recent access vmdk/ modified/written on vmdk date with vm name should be exported to CSV

    Please help me

    Set-CustomField : The term 'Set-CustomField' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At H:\lakshmanawork\VMuptime.ps1:29 char:15
    + $VM | Set-CustomField -Name "Last Modified By" -Value $ModUse ...
    + ~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Set-CustomField:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Last Modified Date 08/23/2023 01:50:16
    Set-CustomField : The term 'Set-CustomField' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At H:\lakshmanawork\VMuptime.ps1:31 char:15
    + $VM | Set-CustomField -Name "Last Modified Date" -Value $ModD ...
    + ~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Set-CustomField:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException