-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-CustomAttributesInGuestinfo.ps1
70 lines (59 loc) · 2.62 KB
/
Set-CustomAttributesInGuestinfo.ps1
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
<#
.SYNOPSIS
Gets Custom Attributes assigned to a VM and makes them available to the guest OS.
.DESCRIPTION
Gets the custom attributes assigned to one or more VMs and sets their values in the
VM's 'guestinfo' advanced settings. This makes the attributes available within the
guest OS using VM tools (vmtoolsd.exe) and allows the attributes to be used as metadata
for applications or management agents that run inside the guest. If the attribute name
contains spaces they are removed in naming the advanced setting.
For example, if a VM has a custom attribute named 'Created On', the advanced setting
becomes:
'guestinfo.CreatedOn' = '08/08/2018 14:24:17'
This can be retrieved in the guest OS by running:
vmtoolsd.exe --cmd "info-get guestinfo.CreatedOn"
.PARAMETER VMs
One or more VMs returned from the Get-VM cmdlet.
.PARAMETER Attributes
The names of the Custom Attributes to get. If the names contain spaces they must be
enclosed in quotes. The spaces will be removed to name the advanced setting.
.PARAMETER vCenter
The vCenter server to connect to. Optional if you are already connected.
.EXAMPLE
.\Set-CustomAttributesInGuestInfo.ps1 -VM (get-vm testvm01) -Attributes 'Created On', 'Created By'
Gets the custom attributes 'Created On' and 'Created By' for 'testvm01' and sets their
values in 'guestinfo.CreatedOn' and 'guestinfo.CreatedBy'.
.EXAMPLE
.\Set-CustomAttributesInGuestInfo.ps1-VM (get-cluster Dev-01 | get-vm) -Attributes 'Created On'
Gets the custom attribute 'Created On' for all VMs in the Dev-01 cluster and sets 'guestinfo.CreatedOn'
on each VM.
#>
#Requires -modules VMware.VimAutomation.Core
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,Position=0)]
$VMs,
[Parameter(Mandatory=$true,Position=1)]
[string[]]$Attributes,
[string]$vCenter
)
if ($vCenter) {
Connect-VIServer $vCenter
}
ForEach ($vm in $VMs) {
ForEach ($attributeName in $Attributes) {
# Get the custom attribute with a matcing key name
$customField = $vm.CustomFields | Where-Object Key -eq $attributeName
if ($customField) {
# Remove white space from the attribute name because the advanced
# setting name cannot contain spaces
$attributeNameNoSpaces = $customField.Key -replace '\s',''
$guestinfoName = "guestinfo.$attributeNameNoSpaces"
$guestinfoValue = $customField.Value
Write-Host "$($vm): setting '$guestinfoName' = '$guestinfoValue'"
New-AdvancedSetting -Entity $vm -Name $guestinfoName -Value $guestinfoValue -Confirm:$false -Force | Out-Null
} else {
Write-Host "$($vm): custom attribute '$attributeName' not set on this VM"
}
}
}