forked from v-team/powercli-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-Tags.ps1
More file actions
137 lines (126 loc) · 5.53 KB
/
Set-Tags.ps1
File metadata and controls
137 lines (126 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<#
.SYNOPSIS
This script will give you a way to handle customField by using a source VM model
.DESCRIPTION
The script will replicate annotation and custom field
from a source VM to a destination VM
(by default it will not overwrite existing custom field)
.NOTES
Author : Frederic Martin - www.vmdude.fr
.LINK
http://www.vmdude.fr
.EXAMPLE
C:\foo> .\Set-Tags.ps1 -vmSourceName VM01 -vmDestinationName VM02
Description
-----------
Replicate custom field from VM01 to VM02 (without overwriting destination)
.EXAMPLE
C:\foo> .\Set-Tags.ps1 -vmSourceName VM01 -vmDestinationName VM02 -force
Description
-----------
Replicate custom field from VM01 to VM02 (with overwriting destination)
.EXAMPLE
C:\foo> .\Set-Tags.ps1 -vmDestinationName VM02
Description
-----------
Fill custom field from VM02 with "N/A" string (without overwriting destination)
.EXAMPLE
C:\foo> .\Set-Tags.ps1 -vmDestinationName VM02 -force
Description
-----------
Fill custom field from VM02 with "N/A" string (with overwriting destination)
.PARAMETER vmSourceName
Name of the VM used for source replication
.PARAMETER vmDestinationName
Name of the VM used for destination replication
.PARAMETER force
This switch allows you to overwrite destination custom field.
#>
param (
[string]$vmSourceName,
[Parameter(Mandatory=$True)]
[string]$vmDestinationName,
[switch]$force
)
# Only handle single vCenter connection, mess it up if not
if (($global:DefaultVIServers | Measure-Object).Count -gt 1) {
Write-Host -Foreground Red "[ERROR] Multiple vCenter connection detected."
Break
}
# Building hash table for speeding late process
$htCustomFields = @{}
Get-View CustomFieldsManager-CustomFieldsManager | %{$_.field} | ?{$_.managedObjectType -match "VirtualMachine|^$"} | %{$htCustomFields.Add($_.Key,$_.Name)}
# Check for source VM
if ($vmSourceName) {
$vmSource = Get-View -ViewType VirtualMachine -Filter @{"Name"="^$vmSourceName$"} -Property Config.Annotation,Value
if (($vmSource | Measure-Object).Count -eq 0) {
Write-Host -Foreground Red "[ERROR] Source VM $vmSourceName doesn't exist."
Break
}
}
# Check for destination VM
$vmDestination = Get-View -ViewType VirtualMachine -Filter @{"Name"="^$vmDestinationName$"} -Property Config.Annotation,Value,Name
if (($vmDestination | Measure-Object).Count -eq 0) {
Write-Host -Foreground Red "[ERROR] Destination VM $vmDestinationName doesn't exist."
Break
}
# Annotation handler
if (-Not $vmSourceName) {
if ($vmDestination.Config.Annotation -And -Not $force) {
Write-Host -Foreground Red "[ERROR] Annotation in destination VM"($vmDestination.Name)"is not empty. Use -force switch to overwrite."
} else {
try {
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Annotation = "N/A"
$vmDestination.ReconfigVM($vmConfigSpec)
Write-Host -Foreground Green "[SUCCESS] Annotation in destination VM"($vmDestination.Name)"has been filled up with 'N/A'."
} catch [System.Exception] {
Write-Host -Foreground Red "[ERROR] Annotation in destination VM"($vmDestination.Name)"cannot be filled: "($_.Exception.ToString())
}
}
} else {
if ($vmSource.Config.Annotation.length -gt 0 -And $vmSource.Config.Annotation -ne $vmDestination.Config.Annotation) {
if ($vmDestination.Config.Annotation -And -Not $force) {
Write-Host -Foreground Red "[ERROR] Annotation in destination VM"($vmDestination.Name)"is not empty. Use -force switch to overwrite."
} else {
try {
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Annotation = $vmSource.Config.Annotation
$vmDestination.ReconfigVM($vmConfigSpec)
Write-Host -Foreground Green "[SUCCESS] Annotation in destination VM"($vmDestination.Name)"has been cloned."
} catch [System.Exception] {
Write-Host -Foreground Red "[ERROR] Annotation in destination VM"($vmDestination.Name)"cannot be cloned: "($_.Exception.ToString())
}
}
}
}
# Custom fields handler
if (-Not $vmSourceName) {
foreach ($vmField in $htCustomFields.Keys) {
if (($vmDestination.Value | ?{$_.Key -eq $vmField}).Value -And -Not $force) {
Write-Host -Foreground Red "[ERROR] Custom Field"($htCustomFields[$vmField])"in destination VM"($vmDestination.Name)"is not empty. Use -force switch to overwrite."
} else {
try {
$vmDestination.setCustomValue($htCustomFields[$vmField],"N/A")
Write-Host -Foreground Green "[SUCCESS] Custom Field"($htCustomFields[$vmField])"in destination VM"($vmDestination.Name)"has been filled up with 'N/A'."
} catch [System.Exception] {
Write-Host -Foreground Red "[ERROR] Custom Field"($htCustomFields[$vmField])"in destination VM"($vmDestination.Name)"cannot be filled:"($_.Exception.ToString())
}
}
}
} else {
foreach ($vmField in $vmSource.Value) {
if ($vmField.Value.length -gt 0 -And $vmField.Value -ne ($vmDestination.Value | ?{$_.Key -eq $vmField.Key}).Value) {
if (($vmDestination.Value | ?{$_.Key -eq $vmField.Key}).Value -And -Not $force) {
Write-Host -Foreground Red "[ERROR] Custom Field"($htCustomFields[$vmField.Key])"in destination VM"($vmDestination.Name)"is not empty. Use -force switch to overwrite."
} else {
try {
$vmDestination.setCustomValue($htCustomFields[$vmField.Key],$vmField.Value)
Write-Host -Foreground Green "[SUCCESS] Custom Field"($htCustomFields[$vmField.Key])"in destination VM"($vmDestination.Name)"has been cloned."
} catch [System.Exception] {
Write-Host -Foreground Red "[ERROR] Custom Field"($htCustomFields[$vmField.Key])"in destination VM"($vmDestination.Name)"cannot be cloned:"($_.Exception.ToString())
}
}
}
}
}