1- [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSAvoidGlobalVars" , " " , Scope= " Function" )]
1+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSAvoidGlobalVars" , " " , Scope = " Function" )]
22param
33(
44)
55
66Import-Module - Name (Join-Path - Path (Split-Path - Path $PSScriptRoot - Parent) `
7- - ChildPath ' CommonResourceHelper.psm1' )
7+ - ChildPath ' CommonResourceHelper.psm1' )
88$script :localizedData = Get-LocalizedData - ResourceName ' MSFT_xOfflineDomainJoin'
99
10+ <#
11+ . SYNOPSIS
12+ Joins the computer to a domain with a domain join file.
13+
14+ . PARAMETER IsSingleInstance
15+ Specifies the resource is a single instance, the value must be 'Yes'.
16+
17+ . PARAMETER RequestFile
18+ The full path to the Offline Domain Join Request file to use.
19+ #>
1020function Get-TargetResource
1121{
1222 [CmdletBinding ()]
@@ -25,7 +35,7 @@ function Get-TargetResource
2535 )
2636
2737 Write-Verbose - Message ( @ ( " $ ( $MyInvocation.MyCommand ) : "
28- $ ($script :localizedData.GettingOfflineDomainJoinMessage )
38+ $ ($script :localizedData.GettingOfflineDomainJoinMessage )
2939 ) -join ' ' )
3040
3141 <#
@@ -34,12 +44,22 @@ function Get-TargetResource
3444 #>
3545 $returnValue = @ {
3646 IsSingleInstance = ' Yes'
37- RequestFile = ' '
47+ RequestFile = ' '
3848 }
3949
4050 return $returnValue
4151} # Get-TargetResource
4252
53+ <#
54+ . SYNOPSIS
55+ Sets the current state of the offline domain join.
56+
57+ . PARAMETER IsSingleInstance
58+ Specifies the resource is a single instance, the value must be 'Yes'.
59+
60+ . PARAMETER RequestFile
61+ The full path to the Offline Domain Join Request file to use.
62+ #>
4363function Set-TargetResource
4464{
4565 [CmdletBinding ()]
@@ -57,22 +77,15 @@ function Set-TargetResource
5777 )
5878
5979 Write-Verbose - Message ( @ ( " $ ( $MyInvocation.MyCommand ) : "
60- $ ($script :localizedData.ApplyingOfflineDomainJoinMessage )
80+ $ ($script :localizedData.ApplyingOfflineDomainJoinMessage )
6181 ) -join ' ' )
6282
6383 # Check the ODJ Request file exists
6484 if (-not (Test-Path - Path $RequestFile ))
6585 {
66- $errorId = ' RequestFileNotFoundError'
67- $errorCategory = [System.Management.Automation.ErrorCategory ]::ObjectNotFound
68- $errorMessage = $ ($script :localizedData.RequestFileNotFoundError ) `
69- -f $RequestFile
70- $exception = New-Object - TypeName System.ArgumentException `
71- - ArgumentList $errorMessage
72- $errorRecord = New-Object - TypeName System.Management.Automation.ErrorRecord `
73- - ArgumentList $exception , $errorId , $errorCategory , $null
74-
75- $PSCmdlet.ThrowTerminatingError ($errorRecord )
86+ New-InvalidArgumentException `
87+ - Message ($script :localizedData.RequestFileNotFoundError -f $RequestFile ) `
88+ - ArgumentName ' RequestFile'
7689 } # if
7790
7891 <#
@@ -82,6 +95,17 @@ function Set-TargetResource
8295 Join-Domain - RequestFile $RequestFile
8396} # Set-TargetResource
8497
98+ <#
99+ . SYNOPSIS
100+ Tests the current state of the machine joining a domain using
101+ an offline domain join file.
102+
103+ . PARAMETER IsSingleInstance
104+ Specifies the resource is a single instance, the value must be 'Yes'.
105+
106+ . PARAMETER RequestFile
107+ The full path to the Offline Domain Join Request file to use.
108+ #>
85109function Test-TargetResource
86110{
87111 [CmdletBinding ()]
@@ -103,66 +127,59 @@ function Test-TargetResource
103127 [System.Boolean ] $desiredConfigurationMatch = $true
104128
105129 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
106- $ ($script :localizedData.CheckingOfflineDomainJoinMessage )
130+ $ ($script :localizedData.CheckingOfflineDomainJoinMessage )
107131 ) -join ' ' )
108132
109133 # Check the ODJ Request file exists
110134 if (-not (Test-Path - Path $RequestFile ))
111135 {
112- $errorId = ' RequestFileNotFoundError'
113- $errorCategory = [System.Management.Automation.ErrorCategory ]::ObjectNotFound
114- $errorMessage = $ ($script :localizedData.RequestFileNotFoundError ) `
115- -f $RequestFile
116- $exception = New-Object - TypeName System.ArgumentException `
117- - ArgumentList $errorMessage
118- $errorRecord = New-Object - TypeName System.Management.Automation.ErrorRecord `
119- - ArgumentList $exception , $errorId , $errorCategory , $null
120-
121- $PSCmdlet.ThrowTerminatingError ($errorRecord )
136+ New-InvalidArgumentException `
137+ - Message ($script :localizedData.RequestFileNotFoundError -f $RequestFile ) `
138+ - ArgumentName ' RequestFile'
122139 } # if
123140
124- $CurrentDomainName = Get-DomainName
141+ $currentDomainName = Get-DomainName
125142
126- if ( $CurrentDomainName )
143+ if ( $currentDomainName )
127144 {
128145 # Domain is already joined.
129146 Write-Verbose - Message ( @ (
130- " $ ( $MyInvocation.MyCommand ) : "
131- $ ($script :localizedData.DomainAlreadyJoinedMessage ) `
132- -f $CurrentDomainName `
147+ " $ ( $MyInvocation.MyCommand ) : "
148+ $ ($script :localizedData.DomainAlreadyJoinedMessage -f $CurrentDomainName ) `
133149 ) -join ' ' )
134150 }
135151 else
136152 {
137153 # Domain is not joined, so change is required.
138154 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
139- $ ($script :localizedData.DomainNotJoinedMessage )
155+ $ ($script :localizedData.DomainNotJoinedMessage )
140156 ) -join ' ' )
141157
142158 $desiredConfigurationMatch = $false
143159 } # if
160+
144161 return $desiredConfigurationMatch
145162} # Test-TargetResource
146163
147164<#
148165 . SYNOPSIS
149166 Uses DJoin.exe to join a Domain using a ODJ Request File.
150167#>
151- function Join-Domain {
168+ function Join-Domain
169+ {
152170 [CmdletBinding ()]
153171 param (
154- [Parameter (Mandatory = $true )]
172+ [Parameter (Mandatory = $true )]
155173 [System.String ]
156174 $RequestFile
157175 )
158176
159177 Write-Verbose - Message ( @ (
160- " $ ( $MyInvocation.MyCommand ) : "
161- $ ($script :localizedData.AttemptingDomainJoinMessage ) `
162- -f $RequestFile `
178+ " $ ( $MyInvocation.MyCommand ) : "
179+ $ ($script :localizedData.AttemptingDomainJoinMessage -f $RequestFile ) `
163180 ) -join ' ' )
164181
165- $Result = & djoin.exe @ (
182+ $djoinResult = & djoin.exe @ (
166183 ' /REQUESTODJ'
167184 ' /LOADFILE'
168185 $RequestFile
@@ -177,24 +194,15 @@ function Join-Domain {
177194 }
178195 else
179196 {
180- Write-Verbose - Message $Result
181-
182- $errorId = ' DjoinError'
183- $errorCategory = [System.Management.Automation.ErrorCategory ]::ObjectNotFound
184- $errorMessage = $ ($script :localizedData.DjoinError ) `
185- -f $LASTEXITCODE
186- $exception = New-Object - TypeName System.ArgumentException `
187- - ArgumentList $errorMessage
188- $errorRecord = New-Object - TypeName System.Management.Automation.ErrorRecord `
189- - ArgumentList $exception , $errorId , $errorCategory , $null
190-
191- $PSCmdlet.ThrowTerminatingError ($errorRecord )
197+ Write-Verbose - Message $djoinResult
198+
199+ New-InvalidOperationException `
200+ - Message ($script :localizedData.DjoinError -f $LASTEXITCODE )
192201 } # if
193202
194203 Write-Verbose - Message ( @ (
195- " $ ( $MyInvocation.MyCommand ) : "
196- $ ($script :localizedData.DomainJoinedMessage ) `
197- -f $RequestFile `
204+ " $ ( $MyInvocation.MyCommand ) : "
205+ $ ($script :localizedData.DomainJoinedMessage -f $RequestFile ) `
198206 ) -join ' ' )
199207} # function Join-Domain
200208
@@ -211,6 +219,7 @@ function Get-DomainName
211219
212220 # Use CIM to detect the domain name so that this will work on Nano Server.
213221 $computerSystem = Get-CimInstance - ClassName ' Win32_ComputerSystem' - Namespace root\cimv2
222+
214223 if ($computerSystem.Workgroup )
215224 {
216225 return $null
0 commit comments