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
}
}