VMware Cloud Community
pamiller21
Enthusiast
Enthusiast
Jump to solution

Copy Custom Attributes to Replicas

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")
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

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
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos