Examples/05/BenGelens_CWindowsContainer.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
enum Ensure {
    Absent
    Present
}

enum ContainerType {
    Default
    HyperV
}

enum AccessMode {
    ReadWrite
    ReadOnly
}

[DscResource()]
class cWindowsContainer {
    [DscProperty(Key)]
    [String] $Name

    [DscProperty(Mandatory)]
    [Ensure] $Ensure

    [DscProperty(Mandatory)]
    [String] $ContainerImageName

    [DscProperty()]
    [String] $ContainerImagePublisher

    [DscProperty()]
    [String] $ContainerImageVersion

    [DscProperty()]
    [String] $SwitchName

    [DscProperty()]
    [String] $StartUpScript

    [DscProperty(NotConfigurable)]
    [String] $IPAddress

    [DscProperty(NotConfigurable)]
    [String] $ContainerId

    [DscProperty()]
    [String] $ContainerComputerName

    [DscProperty()]
    [String] $SourcePath

    [DscProperty()]
    [String] $DestinationPath

    [DscProperty()]
    [AccessMode] $AccessMode = 'ReadOnly'

    [DscProperty()]
    [ContainerType] $ContainerType = [ContainerType]::Default

    [void] Set () {
        try {
            if ($this.Ensure -eq [Ensure]::Present) {
                Write-Verbose -Message 'Starting creation of new Container'

                #region start build New-Container parameters
                $ContainerNewParams = [System.Collections.Hashtable]::new()
                $ContainerNewParams.Add('Name',$this.Name)
                $ContainerNewParams.Add('RuntimeType',$this.ContainerType)
                if ($null -ne $this.ContainerComputerName) {
                    $ContainerNewParams.Add('ContainerComputerName',$this.ContainerComputerName)
                }
                #endregion start build New-Container parameters

                #region ContainerImage
                $ContainerImageParams = [System.Collections.Hashtable]::new()
                $ContainerImageParams.Add('Name',$this.ContainerImageName)
                if ($null -ne $this.ContainerImagePublisher) {
                    $ContainerImageParams.Add('Publisher',$this.ContainerImagePublisher)
                }
                if ($null -ne $this.ContainerImageVersion) {
                    $ContainerImageParams.Add('Version',$this.ContainerImageVersion)
                }
                Write-Verbose -Message "Searching for image: $($ContainerImageParams | Out-String)"
                if ($null -eq ($Image = Get-ContainerImage @ContainerImageParams)) {
                    Write-Error -Message "ContainerImage with properties $($ContainerImageParams | Out-String) was not found" -ErrorAction Stop
                } else {
                    $ContainerNewParams.Add('ContainerImage',$Image)
                }
                #endregion ContainerImage

                #region Switch
                Write-Verbose -Message "Searching for specified switch: $($this.SwitchName)"
                if ($this.SwitchName -and ($null -ne (Get-VMSwitch -Name $this.SwitchName))) {
                    Write-Verbose -Message 'Switch was found and will be bound'
                    $ContainerNewParams.Add('SwitchName',$this.SwitchName)
                } elseif ($this.SwitchName -and ($null -eq (Get-VMSwitch -Name $this.SwitchName))) {
                    Write-Error -Message "Switch with name $($this.SwitchName) was not found" -ErrorAction Stop
                }
                #endregion Switch

                #region Create Container
                Write-Verbose -Message "Creating Container: $($ContainerNewParams | Out-String)"
                $Container = New-Container @ContainerNewParams
                #endregion Create Container

                #region add SharedFolder
                Write-Verbose -Message "Validating if a SharedFolder is needed"
                if ($this.SourcePath) {
                    if ((Test-Path $this.SourcePath) -and ($this.DestinationPath -ne "")) {
                        Write-Verbose -Message "Mapping $($this.sourcePath) to $($this.DestinationPath)"
                        $SharedFolderNewParams = [System.Collections.Hashtable]::new()
                        $SharedFolderNewParams.Add('ContainerName',$this.Name)
                        $SharedFolderNewParams.Add('SourcePath',$this.SourcePath)
                        $SharedFolderNewParams.Add('DestinationPath',$this.DestinationPath)
                        $SharedFolderNewParams.Add('AccessMode',$this.AccessMode)
                        Add-ContainerSharedFolder @SharedFolderNewParams
                    } else {
                        Write-Error -Message "$($this.SourcePath) isn't available or missing destination path in configuration"
                    }
                } else {
                    Write-Verbose -Message "No SharedFolder needed"
                }

                #region start Container
                Write-Verbose -Message "Starting Container $($this.Name)"
                $Container | Start-Container
                #endregion start container

                #region run startup script
                if ($null -ne $this.StartUpScript) {
                    Write-Verbose -Message 'Startup Script specified, passing script to InvokeScript method'
                    [void] $this.InvokeScript(([scriptblock]::Create($this.StartUpScript)),$Container.ContainerId)
                }
                #endregion run startup script
            } else {
                Write-Verbose -Message 'Removing Container'
                Get-Container -Name $this.Name | Stop-Container -Passthru | Remove-Container -Force

            }
        } catch {
            Write-Error -ErrorRecord $_ -ErrorAction Stop
        }
    }

    [bool] Test () {
        if ((Get-Container -Name $this.Name -ErrorAction SilentlyContinue) -and ($this.Ensure -eq [Ensure]::Present)) {
            $value = $true
            $SharedFolder = Get-ContainerSharedFolder -ContainerName $this.Name -ErrorAction SilentlyContinue
            if (($SharedFolder.SourcePath -eq $this.SourcePath) -and ($SharedFolder.DestinationPath -eq $this.DestinationPath) -and ($SharedFolder.AccessMode -eq $this.AccessMode)) {
                $Value = $true
            } else {
                $Value = $false
            }
            return $value
        } else {
            return $false
        }
    }

    [String] InvokeScript ([String] $Script, [String] $ContainerId) {
        $Output = Invoke-Command -ContainerId $ContainerId -RunAsAdministrator -ScriptBlock ([scriptblock]::Create($Script)) -ErrorAction Stop
        return $Output
    }

    [cWindowsContainer] Get () {
        $Configuration = [System.Collections.Hashtable]::new()
        $Configuration.Add('Name',$this.Name)
        $Configuration.Add('ContainerComputerName',$this.ContainerComputerName)
        $Configuration.Add('ContainerImageName',$this.ContainerImageName)
        $Configuration.Add('ContainerImagePublisher',$this.ContainerImagePublisher)
        $Configuration.Add('ContainerImageVersion',$this.ContainerImageVersion)
        $Configuration.Add('SwitchName',$this.SwitchName)
        $Configuration.Add('StartUpScript',$this.StartUpScript)
        $Configuration.Add('ContainerType',$this.ContainerType)
        if (($this.Ensure -eq [Ensure]::Present) -and ($this.Test())) {
            Write-Verbose -Message 'Acquiring ContainerId'
            $Configuration.Add('ContainerId',(Get-Container -Name $this.Name).ContainerId)
            $Configuration.Add('Ensure','Present')
            Write-Verbose -Message 'Acquiring IPAddress'
            if ($null -ne $this.SwitchName) {
                $Configuration.Add('IPAddress',$this.InvokeScript('(Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Manual).IPAddress',$Configuration.ContainerId))
            }
            if ($this.SourcePath) {
                $SharedFolder = Get-ContainerSharedFolder -ContainerName $this.Name
                $Configuration.Add('SourcePath',$SharedFolder.SourcePath)
                $Configuration.Add('DestinationPath',$SharedFolder.DestinationPath)
                $Configuration.Add('AccessMode',$SharedFolder.AccessMode)
            }
        } else {
            $Configuration.Add('Ensure','Absent')
        }
        return $Configuration
    }
}