DSCResources/POSHOrigin_vSphere_VM/Invoke.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<#
    This script expects to be passed a psobject with all the needed properties
    in order to invoke a 'VirtualMachine' DSC resource.
#>

[cmdletbinding()]
param(
    [parameter(mandatory)]
    [psobject]$Options,

    [bool]$Direct = $false
)

# Ensure we have a valid 'ensure' property
if ($null -eq $Options.options.Ensure) {
    $Options.Options | Add-Member -MemberType NoteProperty -Name Ensure -Value 'Present' -Force
}

# Get the resource type
$type = $Options.Resource.split(':')[1]

switch ($type) {
    'vm' {
        $provJson = ''
        if ($null -ne $Options.options.provisioners) {
            $provJson = ConvertTo-Json -InputObject $Options.options.provisioners -Depth 100
        }

        if ($null -eq $provJson) {
            $provJson = ''
        }

        if ($Direct) {
            $hash = @{
                Ensure = $Options.options.Ensure.ToString()
                Name = $Options.Name
                PowerOnAfterCreation = $Options.options.PowerOnAfterCreation
                vCenter = $Options.options.vCenter
                VMTemplate = $Options.options.VMTemplate
                TotalvCPU = $Options.options.TotalvCPU
                CoresPerSocket = $Options.options.CoresPerSocket
                vRAM = $Options.options.vRAM
                Datacenter = $Options.options.Datacenter
                Cluster = $Options.options.Cluster
                ResourcePool = $Options.options.ResourcePool
                VMHost = $Options.options.VMHost
                vApp = $Options.options.vApp
                VMFolder = $Options.options.VMFolder
                Tags = $Options.options.Tags
                Description = $Options.options.Description
                UpdateTools = $Options.options.UpdateTools
                InitialDatastore = $Options.options.InitialDatastore
                Disks = ConvertTo-Json -InputObject $Options.options.disks -Depth 100
                CustomizationSpec = $Options.options.CustomizationSpec
                Networks = ConvertTo-Json -InputObject $Options.options.networks -Depth 100
                Provisioners = $provJson
            }

            # Credentials may be specified in line. Test for that
            if ($Options.Options.vCenterCredentials -is [pscredential]) {
                $hash.vCenterCredentials = $Options.Options.vCenterCredentials
            }
            if ($Options.Options.GuestCredentials -is [pscredential]) {
                if ($Options.Options.GuestCredentials.UserName -notcontains '\') {
                    $userName = "$($Options.Name)`\$($Options.Options.GuestCredentials.UserName)"
                    $tCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($userName, $Options.Options.GuestCredentials.Password)
                    $hash.GuestCredentials = $tCred
                } else {
                    $hash.GuestCredentials = $Options.Options.GuestCredentials
                }
            }
            if ($Options.Options.DomainJoinCredentials -is [pscredential]) {
                $hash.DomainJoinCredentials = $Options.Options.DomainJoinCredentials
            }
            if ($Options.Options.IPAMCredentials -is [pscredential]) {
                $hash.IPAMCredentials = $Options.Options.IPAMCredentials
            }
            if ($Options.Options.IPAMFqdn -is [string]) {
                $hash.IPAMFqdn = $Options.Options.IPAMFqdn
            }

            # Credentials may be listed under secrets. Test for that
            if ($Options.options.secrets.vCenter) {
                $hash.vCenterCredentials = $Options.options.secrets.vCenter.credential
            }
            if ($Options.options.secrets.guest) {
                $hash.GuestCredentials = $Options.options.secrets.guest.credential
            }
            if ($Options.options.secrets.domainJoin) {
                $hash.DomainJoinCredentials = $Options.options.secrets.domainJoin.credential
            }
            if ($Options.options.secrets.ipam) {
                $hash.IPAMCredentials = $Options.options.secrets.ipam.credential
                $hash.IPAMFqdn = $Options.options.secrets.ipam.options.fqdn
            }

            # If the guest credential doesn't have a domain or computer name
            # as part of the username, make sure to add it
            if ($hash.GuestCredentials.UserName -notlike '*\*') {
                $userName = "$($Options.Name)`\$($hash.GuestCredentials.UserName)"
                $cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($userName, $hash.GuestCredentials.Password)
                $hash.GuestCredentials = $cred
            }

            return $hash
        } else {
            $configName = $Options.Name.Replace('-', '')
            $confName = "$type" + '_' + $configName
            #Write-Verbose -Message "Returning configuration function for resource: $confName"
            Configuration $confName {
                Param (
                    [psobject]$ResourceOptions
                )

                Import-DscResource -Name VM -ModuleName POSHOrigin_vSphere -ModuleVersion 1.4.3

                # Credentials may be specified in line. Test for that
                if ($ResourceOptions.Options.vCenterCredentials -is [pscredential]) {
                    $vcCred = $ResourceOptions.Options.vCenterCredentials
                }
                if ($ResourceOptions.Options.GuestCredentials -is [pscredential]) {
                    $guestCred = $ResourceOptions.Options.GuestCredentials
                }
                if ($ResourceOptions.Options.IPAMCredentials -is [pscredential]) {
                    $ipamCred = $ResourceOptions.Options.IPAMCredentials
                }
                if ($ResourceOptions.Options.DomainJoinCredentials -is [pscredential]) {
                    $djCred = $ResourceOptions.Options.DomainJoinCredentials
                }

                # Credentials may be listed under secrets. Test for that
                if ($ResourceOptions.options.secrets.vCenter -or $ResourceOptions.options.secrets.vCenterCredentials ) {
                    if ($ResourceOptions.options.secrets.vCenter) {
                        $vcCred = $ResourceOptions.options.secrets.vCenter.credential
                    } else {
                        $vcCred = $ResourceOptions.options.secrets.vCenterCredentials.credential
                    }
                }
                if ($ResourceOptions.options.secrets.guest -or $ResourceOptions.options.secrets.GuestCredentials ) {
                    if ($ResourceOptions.options.secrets.guest) {
                        $tCred = $ResourceOptions.options.secrets.guest.credential
                    } else {
                        $tCred = $ResourceOptions.options.secrets.guestCredentials.credential
                    }
                    # If the guest credential doesn't have a domain or computer name
                    # as part of the username, make sure to add it
                    if ($tCred.UserName -notcontains '\') {
                        $userName = "$($ResourceOptions.Name)`\$($tCred.UserName)"
                        $tCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($userName, $tCred.Password)
                    }
                    $guestCred = $tCred
                }
                if ($ResourceOptions.options.secrets.ipam -or $ResourceOptions.options.secrets.IPAMCredentials ) {
                    if ($ResourceOptions.options.secrets.ipam) {
                        $ipamCred = $ResourceOptions.options.secrets.ipam.credential
                    } else {
                        $ipamCred = $ResourceOptions.options.secrets.IPAMCredentials.credential
                    }
                }
                if ($ResourceOptions.options.secrets.domainjoin -or $ResourceOptions.options.secrets.DomainJoinCredentials ) {
                    if ($ResourceOptions.options.secrets.domainjoin) {
                        $djCred = $ResourceOptions.options.secrets.domainjoin.credential
                    } else {
                        $djCred = $ResourceOptions.options.secrets.DomainJoinCredentials.credential
                    }
                }

                $provJson = [string]::empty
                if ($ResourceOptions.options.provisioners) {
                    $provJson = ConvertTo-Json -InputObject $ResourceOptions.options.provisioners -Depth 100
                }

                if (-Not $provJson) {
                    $provJson = [string]::empty
                }

                if ($ResourceOptions.options.Tags) {
                    $tags = ConvertTo-Json -InputObject $ResourceOptions.options.Tags
                } else {
                    $tags = @()
                }

                VM $ResourceOptions.Name {
                    Ensure = $ResourceOptions.options.Ensure
                    Name = $ResourceOptions.Name
                    PowerOnAfterCreation = $ResourceOptions.options.PowerOnAfterCreation
                    vCenter = $ResourceOptions.options.vCenter
                    vCenterCredentials = $vcCred
                    VMTemplate = $ResourceOptions.options.VMTemplate
                    TotalvCPU = $ResourceOptions.options.TotalvCPU
                    CoresPerSocket = $ResourceOptions.options.CoresPerSocket
                    vRAM = $ResourceOptions.options.vRAM
                    Datacenter = $ResourceOptions.options.Datacenter
                    Cluster = $ResourceOptions.options.Cluster
                    VMHost = $ResourceOptions.options.VMHost
                    ResourcePool = $ResourceOptions.options.ResourcePool
                    vApp = $ResourceOptions.options.vApp
                    VMFolder = $ResourceOptions.options.VMFolder
                    UpdateTools = $ResourceOptions.options.UpdateTools
                    Tags = $tags
                    Description = $ResourceOptions.options.Description
                    InitialDatastore = $ResourceOptions.options.InitialDatastore
                    Disks = ConvertTo-Json -InputObject $ResourceOptions.options.disks
                    CustomizationSpec = $ResourceOptions.options.CustomizationSpec
                    GuestCredentials = $guestCred
                    IPAMCredentials = $ipamCred
                    IPAMFqdn = $ResourceOptions.options.secrets.ipam.options.fqdn
                    DomainJoinCredentials = $djCred
                    Networks = ConvertTo-Json -InputObject $ResourceOptions.options.networks
                    Provisioners = $provJson
                }
            }
        }
    }
}