Automation

 View Only
  • 1.  Copy Custom Attributes to Replicas

    Posted Mar 31, 2023 02:58 PM

    I am attempting to copy a custom attribute field from my production vms to replica vms with the same name, just appended with _replica.

    I am terrible at doing loops and not sure how to make it work.

    This pulls the field but not sure how to apply them correctly after that.

    Get-VM | $VM.CustomFields.Item("Creation Date")


  • 2.  RE: Copy Custom Attributes to Replicas
    Best Answer

    Posted Mar 31, 2023 04:06 PM

    You could do something like this.
    I added the Where-clause after the Get-VM to exclude the replicas.

    $caName = 'Creation Date'
    $ca = Get-CustomAttribute -Name $caName
    
    Get-VM -PipelineVariable vm | 
    where{$vm.Name -notmatch "_replica$"} |
    Get-Annotation -CustomAttribute $ca -PipelineVariable caObj |
    ForEach-Object -Process {
      Get-VM -Name "$($vm.Name)_replica" |
      Set-Annotation -CustomAttribute $ca -Value $caObj.Value -Confirm:$false
    }