Skip to content

Commit f22f175

Browse files
committed
Convert xOfflineDomainJoin to HQRM
1 parent d382be3 commit f22f175

4 files changed

Lines changed: 110 additions & 107 deletions

File tree

DSCResources/MSFT_xOfflineDomainJoin/MSFT_xOfflineDomainJoin.psm1

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Scope="Function")]
1+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Scope = "Function")]
22
param
33
(
44
)
55

66
Import-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+
#>
1020
function 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+
#>
4363
function 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+
#>
85109
function 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
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[ClassVersion("1.0.0.0"), FriendlyName("xOfflineDomainJoin")]
22
class MSFT_xOfflineDomainJoin : OMI_BaseResource
33
{
4-
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'"), ValueMap{"Yes"}, Values{"Yes"}] String IsSingleInstance;
4+
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'."), ValueMap{"Yes"}, Values{"Yes"}] String IsSingleInstance;
55
[Required, Description("The full path to the Offline Domain Join Request file to use.")] String RequestFile;
66
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ xVirtualMemory has the following properties:
211211

212212
### Unreleased
213213

214+
* xOfflineDomainJoin:
215+
* Updated to meet HQRM guidelines.
216+
214217
### 3.0.0.0
215218

216219
* xComputer: Added parameter to set the local computer description along with documentation
@@ -233,7 +236,6 @@ xVirtualMemory has the following properties:
233236
* Converted calls to `throw` to use `New-InvalidOperationException`
234237
in CommonResourceHelper.
235238
* Improved unit test coverage.
236-
* Updated to meet HQRM guidelines.
237239

238240
### 2.1.0.0
239241

0 commit comments

Comments
 (0)