DSCResources/Grani_DomainJoin/Grani_DomainJoin.psm1

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#region Initialize

function Initialize
{
    # Enum for Ensure
    try
    {
    Add-Type -TypeDefinition @"
        public enum EnsureType
        {
            Present,
            Absent
        }
"@

    }
    catch
    {
    }
}

Initialize

#endregion

#region Message Definition

$debugMessage = DATA {
    ConvertFrom-StringData -StringData "
        JoinDomain = Join domain.
        JoinWorkGroup = Join workgroup.
        JoinNewWorkGroup = Join to new workgroup
        UnJoinDomain = Leaving old domain.
    "

}

$verboseMessage = DATA {
    ConvertFrom-StringData -StringData "
        DomainNameCheck = Checking if Domain name is {0}
        JoinDomain = Added computer to Domain {0}
        JoinWorkGroup = Added computer to new workgroup {0}
        UnJoinDomain = Leaved old domain {0} with other credential
        WorkGroupNameCheck = Checking if WorkGroup name is {0}
    "

}

$exceptionMessage = DATA {
    ConvertFrom-StringData -StringData "
        NoCredentialWithDomain = Need to specify credentials with domain
        BothWorkGroupAndDomainExist = Only one of either the domain name or the workgroup name can be set. Please edit your configuration to ensure that only one of these properties have a value.
    "

}

#endregion

#region *-TargetResource

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([Hashtable])]
    param
    (    
        [parameter(Mandatory = $true)]
        [string]$Identifier,

        [parameter(Mandatory = $false)]
        [string]$DomainName,

        [parameter(Mandatory = $false)]
        [string]$WorkGroupName,

        [parameter(Mandatory = $false)]
        [PSCredential]$Credential,

        [parameter(Mandatory = $false)]
        [PSCredential]$UnjoinCredential,

        [parameter(Mandatory = $false)]
        [Bool]$Restart = $false
    )

    # validating DomainName and WorkGroupName not in use at once
    ValidateDomainWorkGroup -DomainName $DomainName -WorkGroupName $WorkGroupName

    # get current computer system information
    $computerSystem = Get-WmiObject WIN32_ComputerSystem

    # return hash
    $returnValue = @{
        Identifier = $Identifier
        Name = $env:COMPUTERNAME
        DomainName =$computerSystem.Domain
        Credential = New-CimInstance -ClassName MSFT_Credential -Property @{Username=[string]$Credential.UserName; Password=[string]$null} -Namespace root/microsoft/windows/desiredstateconfiguration -ClientOnly
        UnjoinCredential = New-CimInstance -ClassName MSFT_Credential -Property @{Username=[string]$UnjoinCredential.UserName; Password=[string]$null} -Namespace root/microsoft/windows/desiredstateconfiguration -ClientOnly
        WorkGroupName= $computerSystem.WorkGroup
    }

    # ensure status check
    if(-not [string]::IsNullOrEmpty($DomainName))
    {
        if(!($Credential)){ throw New-Object System.NullReferenceException ($exceptionMessage.NoCredentialWithDomain) }

        Write-Verbose -Message ($verboseMessage.DomainNameCheck -f $DomainName)
        $ensure = if ($DomainName -eq $computerSystem.Domain)
        {
            [EnsureType]::Present.ToString()
        }
        else
        {
            [EnsureType]::Absent.ToString()
        }
    }
    elseif(-not [string]::IsNullOrEmpty($WorkGroupName))
    {
        Write-Verbose -Message ($verboseMessage.WorkGroupNameCheck -f $WorkGroupName)
        $ensure = if ($WorkGroupName -eq $computerSystem.WorkGroup)
        {
            [EnsureType]::Present.ToString()
        }
        else
        {
            [EnsureType]::Absent.ToString()
        }
    }

    $returnValue.Ensure = $ensure
    return $returnValue
}

function Set-TargetResource
{
    [CmdletBinding()]
    [OutputType([Void])]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$Identifier,

        [parameter(Mandatory = $false)]
        [string]$DomainName,

        [parameter(Mandatory = $false)]
        [string]$WorkGroupName,

        [parameter(Mandatory = $false)]
        [PSCredential]$Credential,

        [parameter(Mandatory = $false)]
        [PSCredential]$UnjoinCredential,

        [parameter(Mandatory = $false)]
        [Bool]$Restart = $false
    )

    ValidateDomainWorkGroup -DomainName $DomainName -WorkGroupName $WorkGroupName
   
    # get current computer system information
    $computerSystem = Get-WmiObject WIN32_ComputerSystem

    if($Credential)
    {
        # must be both domain and none-domain scenario
        if($DomainName)
        {
            if($DomainName -ne $computerSystem.Domain)
            {
                if ($UnjoinCredential)
                {
                    # unjoin and re-join to other domain
                    Write-Debug ($debugMessage.UnJoinDomain -f $WorkGroupName)
                    Add-Computer -DomainName $DomainName -Credential $Credential -UnjoinDomainCredential $UnjoinCredential -Force -Restart:$Restart
                    Write-Verbose -Message ($verboseMessage.UnJoinDomain -f $DomainName)
                    $global:DSCMachineStatus = 1
                }
                else
                {
                    # join Domain
                    Write-Debug ($debugMessage.JoinDomain -f $WorkGroupName)
                    Add-Computer -DomainName $DomainName -Credential $Credential -Force -Restart:$Restart
                    Write-Verbose -Message ($verboseMessage.JoinDomain -f $DomainName)
                    $global:DSCMachineStatus = 1
                }
            }
        }
        elseif ($WorkGroupName)
        {
            if($WorkGroupName -ne $computerSystem.WorkGroup)
            {
                # Join to workgroup
                Write-Debug ($debugMessage.JoinWorkGroup -f $WorkGroupName)
                Add-Computer -WorkGroupName $WorkGroupName -Credential $Credential -Force -Restart:$Restart
                Write-Verbose -Message ($verboseMessage.JoinWorkGroup -f $WorkGroupName)
                $global:DSCMachineStatus = 1
            }
        }
    }
    else
    {
        # must be non domain scenario
        if($DomainName){ throw New-Object System.NullReferenceException ($exceptionMessage.NoCredentialWithDomain) }

        # workgroup scenario
        if($WorkGroupName)
        {
            if($WorkGroupName -ne $computerSystem.Workgroup)
            {
                Write-Debug $debugMessage.JoinNewWorkGroup
                Add-Computer -WorkgroupName $WorkGroupName -Restart:$Restart
                Write-Verbose -Message ($verboseMessage.JoinWorkGroup -f $WorkGroupName)
                $global:DSCMachineStatus = 1
            }
        }
    }
            
    # Tell the DSC Engine to restart the machine
    $global:DSCMachineStatus = 1
}

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$Identifier,

        [parameter(Mandatory = $false)]
        [string]$DomainName,

        [parameter(Mandatory = $false)]
        [string]$WorkGroupName,

        [parameter(Mandatory = $false)]
        [PSCredential]$Credential,

        [parameter(Mandatory = $false)]
        [PSCredential]$UnjoinCredential,

        [parameter(Mandatory = $false)]
        [Bool]$Restart = $false
    )
    
    return (Get-TargetResource -Identifier $Identifier -DomainName $DomainName -Credential $Credential -UnjoinCredential $UnjoinCredential -WorkGroupName $WorkGroupName).Ensure -eq [EnsureType]::Present.ToString()
}

#endregion

#region helper

function ValidateDomainWorkGroup
{
    param
    (
        $DomainName,
        $WorkGroupName
    )

    if($DomainName -and $WorkGroupName)
    {
        throw New-Object System.InvalidOperationException $exceptionMessage.BothWorkGroupAndDomainExist
    }
}

#endregion

Export-ModuleMember -Function *-TargetResource