Skip to content

Commit 22ee2df

Browse files
committed
Changes as per PR comments
1 parent 7a14234 commit 22ee2df

4 files changed

Lines changed: 119 additions & 89 deletions

File tree

DSCResources/CommonResourceHelper.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ function Get-LocalizedData
4848
#>
4949
function Remove-CommonParameter
5050
{
51-
[OutputType([hashtable])]
51+
[OutputType([System.Collections.Hashtable])]
5252
[cmdletbinding()]
5353
param
5454
(
5555
[Parameter(Mandatory = $true)]
56-
[hashtable]
56+
[System.Collections.Hashtable]
5757
$Hashtable
5858
)
5959

@@ -88,7 +88,7 @@ function Test-DscParameterState
8888
param
8989
(
9090
[Parameter(Mandatory = $true)]
91-
[hashtable]
91+
[System.Collections.Hashtable]
9292
$CurrentValues,
9393

9494
[Parameter(Mandatory = $true)]

DSCResources/MSFT_xComputer/MSFT_xComputer.psm1

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Scope="Function")]
1+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Scope = "Function")]
22
param
33
(
44
)
@@ -10,8 +10,8 @@ function Get-TargetResource
1010
param
1111
(
1212
[Parameter(Mandatory = $true)]
13-
[ValidateLength(1,15)]
14-
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
13+
[ValidateLength(1, 15)]
14+
[ValidateScript( {$_ -inotmatch '[\/\\:*?"<>|]' })]
1515
[System.String]
1616
$Name,
1717

@@ -24,11 +24,11 @@ function Get-TargetResource
2424
$JoinOU,
2525

2626
[Parameter()]
27-
[PSCredential]
27+
[System.Management.Automation.PSCredential]
2828
$Credential,
2929

3030
[Parameter()]
31-
[PSCredential]
31+
[System.Management.Automation.PSCredential]
3232
$UnjoinCredential,
3333

3434
[Parameter()]
@@ -38,17 +38,32 @@ function Get-TargetResource
3838

3939
Write-Verbose -Message "Getting computer state for '$($Name)'."
4040

41-
$convertToCimCredential = New-CimInstance -ClassName MSFT_Credential -Property @{Username=[System.String]$Credential.UserName; Password=[System.String]$null} -Namespace root/microsoft/windows/desiredstateconfiguration -ClientOnly
42-
$convertToCimUnjoinCredential = New-CimInstance -ClassName MSFT_Credential -Property @{Username=[System.String]$UnjoinCredential.UserName; Password=[System.String]$null} -Namespace root/microsoft/windows/desiredstateconfiguration -ClientOnly
41+
$convertToCimCredential = New-CimInstance `
42+
-ClassName MSFT_Credential `
43+
-Property @{
44+
Username = [System.String] $Credential.UserName
45+
Password = [System.String] $null
46+
} `
47+
-Namespace root/microsoft/windows/desiredstateconfiguration `
48+
-ClientOnly
49+
50+
$convertToCimUnjoinCredential = New-CimInstance `
51+
-ClassName MSFT_Credential `
52+
-Property @{
53+
Username = [System.String] $UnjoinCredential.UserName
54+
Password = [System.String]$null
55+
} `
56+
-Namespace root/microsoft/windows/desiredstateconfiguration `
57+
-ClientOnly
4358

4459
$returnValue = @{
45-
Name = $env:COMPUTERNAME
46-
DomainName = Get-ComputerDomain
47-
JoinOU = $JoinOU
48-
CurrentOU = Get-ComputerOU
49-
Credential = [ciminstance]$convertToCimCredential
60+
Name = $env:COMPUTERNAME
61+
DomainName = Get-ComputerDomain
62+
JoinOU = $JoinOU
63+
CurrentOU = Get-ComputerOU
64+
Credential = [ciminstance]$convertToCimCredential
5065
UnjoinCredential = [ciminstance]$convertToCimUnjoinCredential
51-
WorkGroupName = (Get-CimInstance -Class WIN32_ComputerSystem).Workgroup
66+
WorkGroupName = (Get-CimInstance -Class 'Win32_ComputerSystem').Workgroup
5267
}
5368

5469
$returnValue
@@ -60,8 +75,8 @@ function Set-TargetResource
6075
param
6176
(
6277
[Parameter(Mandatory = $true)]
63-
[ValidateLength(1,15)]
64-
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
78+
[ValidateLength(1, 15)]
79+
[ValidateScript( {$_ -inotmatch '[\/\\:*?"<>|]' })]
6580
[System.String]
6681
$Name,
6782

@@ -74,11 +89,11 @@ function Set-TargetResource
7489
$JoinOU,
7590

7691
[Parameter()]
77-
[PSCredential]
92+
[System.Management.Automation.PSCredential]
7893
$Credential,
7994

8095
[Parameter()]
81-
[PSCredential]
96+
[System.Management.Automation.PSCredential]
8297
$UnjoinCredential,
8398

8499
[Parameter()]
@@ -107,17 +122,19 @@ function Set-TargetResource
107122
{
108123
if ($Name -ne $env:COMPUTERNAME)
109124
{
110-
# Rename the comptuer, and join it to the domain.
125+
# Rename the computer, and join it to the domain.
111126
if ($UnjoinCredential)
112127
{
113128
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -UnjoinDomainCredential $UnjoinCredential -Force
114129
}
115130
else
116131
{
117-
if ($JoinOU) {
132+
if ($JoinOU)
133+
{
118134
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -OUPath $JoinOU -Force
119135
}
120-
else {
136+
else
137+
{
121138
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force
122139
}
123140
}
@@ -132,10 +149,12 @@ function Set-TargetResource
132149
}
133150
else
134151
{
135-
if ($JoinOU) {
152+
if ($JoinOU)
153+
{
136154
Add-Computer -DomainName $DomainName -Credential $Credential -OUPath $JoinOU -Force
137155
}
138-
else {
156+
else
157+
{
139158
Add-Computer -DomainName $DomainName -Credential $Credential -Force
140159
}
141160
}
@@ -145,9 +164,9 @@ function Set-TargetResource
145164
}
146165
elseif ($WorkGroupName)
147166
{
148-
if($WorkGroupName -eq (Get-CimInstance -Class WIN32_ComputerSystem).Workgroup)
167+
if ($WorkGroupName -eq (Get-CimInstance -Class 'Win32_ComputerSystem').Workgroup)
149168
{
150-
# Rename the comptuer, but stay in the same workgroup.
169+
# Rename the computer, but stay in the same workgroup.
151170
Rename-Computer -NewName $Name
152171
Write-Verbose -Message "Renamed computer to '$($Name)'."
153172
}
@@ -167,7 +186,7 @@ function Set-TargetResource
167186
}
168187
}
169188
}
170-
elseif($Name -ne $env:COMPUTERNAME)
189+
elseif ($Name -ne $env:COMPUTERNAME)
171190
{
172191
if (Get-ComputerDomain)
173192
{
@@ -185,12 +204,12 @@ function Set-TargetResource
185204
{
186205
if ($DomainName)
187206
{
188-
throw "Missing domain join credentials."
207+
throw 'Missing domain join credentials.'
189208
}
190209
if ($WorkGroupName)
191210
{
192211

193-
if ($WorkGroupName -eq (Get-CimInstance -Class WIN32_ComputerSystem).Workgroup)
212+
if ($WorkGroupName -eq (Get-CimInstance -Class 'Win32_ComputerSystem').Workgroup)
194213
{
195214
# Same workgroup, new computer name
196215
Rename-Computer -NewName $Name -force
@@ -232,8 +251,8 @@ function Test-TargetResource
232251
param
233252
(
234253
[Parameter(Mandatory = $true)]
235-
[ValidateLength(1,15)]
236-
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
254+
[ValidateLength(1, 15)]
255+
[ValidateScript( {$_ -inotmatch '[\/\\:*?"<>|]' })]
237256
[System.String]
238257
$Name,
239258

@@ -242,11 +261,11 @@ function Test-TargetResource
242261
$JoinOU,
243262

244263
[Parameter()]
245-
[PSCredential]
264+
[System.Management.Automation.PSCredential]
246265
$Credential,
247266

248267
[Parameter()]
249-
[PSCredential]
268+
[System.Management.Automation.PSCredential]
250269
$UnjoinCredential,
251270

252271
[Parameter()]
@@ -258,60 +277,67 @@ function Test-TargetResource
258277
$WorkGroupName
259278
)
260279

261-
Write-Verbose -Message "Validate desired Name is a valid name"
280+
Write-Verbose -Message 'Validate desired Name is a valid name'
262281

263-
Write-Verbose -Message "Checking if computer name is correct"
264-
if (($Name -ne 'localhost') -and ($Name -ne $env:COMPUTERNAME)) {return $false}
282+
Write-Verbose -Message 'Checking if computer name is correct'
283+
if (($Name -ne 'localhost') -and ($Name -ne $env:COMPUTERNAME))
284+
{
285+
return $false
286+
}
265287

266288
Assert-DomainOrWorkGroup -DomainName $DomainName -WorkGroupName $WorkGroupName
267289

268-
if($DomainName)
290+
if ($DomainName)
269291
{
270-
if(!($Credential))
292+
if (-not ($Credential))
271293
{
272-
throw "Need to specify credentials with domain"
294+
throw 'Need to specify credentials with domain'
273295
}
274296

275297
try
276298
{
277299
Write-Verbose "Checking if the machine is a member of $DomainName."
300+
278301
if ($DomainName.Contains('.'))
279302
{
280-
$GetComputerDomainSplat = @{
303+
$GetComputerDomainParameters = @{
281304
netbios = $false
282305
}
283306
}
284307
else
285308
{
286-
$GetComputerDomainSplat = @{
309+
$GetComputerDomainParameters = @{
287310
netbios = $true
288311
}
289312
}
290-
return ($DomainName -eq (Get-ComputerDomain @GetComputerDomainSplat))
313+
314+
return ($DomainName -eq (Get-ComputerDomain @GetComputerDomainParameters))
291315
}
292316
catch
293317
{
294-
Write-Verbose 'The machine is not a domain member.'
295-
return $false
318+
Write-Verbose 'The machine is not a domain member.'
319+
320+
return $false
296321
}
297322
}
298-
elseif($WorkGroupName)
323+
elseif ($WorkGroupName)
299324
{
300325
Write-Verbose -Message "Checking if workgroup name is $WorkGroupName"
301-
return ($WorkGroupName -eq (Get-CimInstance -Class WIN32_ComputerSystem).Workgroup)
326+
327+
return ($WorkGroupName -eq (Get-CimInstance -Class 'Win32_ComputerSystem').Workgroup)
302328
}
303329
else
304330
{
305331
# No Domain or Workgroup specified and computer name is correct
306-
return $true;
332+
return $true
307333
}
308334
}
309335

310336
function Assert-DomainOrWorkGroup($DomainName, $WorkGroupName)
311337
{
312338
if ($DomainName -and $WorkGroupName)
313339
{
314-
throw "Only DomainName or WorkGroupName can be specified at once."
340+
throw 'Only DomainName or WorkGroupName can be specified at once.'
315341
}
316342
}
317343

DSCResources/MSFT_xOfflineDomainJoin/MSFT_xOfflineDomainJoin.psm1

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ $script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xOfflineDomainJoin
1010
function Get-TargetResource
1111
{
1212
[CmdletBinding()]
13-
[OutputType([Hashtable])]
13+
[OutputType([System.Collections.Hashtable])]
1414
param
1515
(
1616
[Parameter(Mandatory = $true)]
@@ -28,15 +28,16 @@ function Get-TargetResource
2828
$($script:localizedData.GettingOfflineDomainJoinMessage)
2929
) -join '')
3030

31-
# It is not possible to read the ODJ file that was used to join a domain
32-
# So it has to always be returned as blank.
31+
<#
32+
It is not possible to read the ODJ file that was used to join a domain
33+
So it has to always be returned as blank.
34+
#>
3335
$returnValue = @{
3436
IsSingleInstance = 'Yes'
3537
RequestFile = ''
3638
}
3739

38-
#Output the target resource
39-
$returnValue
40+
return $returnValue
4041
} # Get-TargetResource
4142

4243
function Set-TargetResource
@@ -74,15 +75,17 @@ function Set-TargetResource
7475
$PSCmdlet.ThrowTerminatingError($errorRecord)
7576
} # if
7677

77-
# Don't need to check if the domain is already joined because
78-
# Set-TargetResource wouldn't fire unless it wasn't.
78+
<#
79+
Don't need to check if the domain is already joined because
80+
Set-TargetResource wouldn't fire unless it wasn't.
81+
#>
7982
Join-Domain -RequestFile $RequestFile
8083
} # Set-TargetResource
8184

8285
function Test-TargetResource
8386
{
8487
[CmdletBinding()]
85-
[OutputType([Boolean])]
88+
[OutputType([System.Boolean])]
8689
param
8790
(
8891
[Parameter(Mandatory = $true)]
@@ -97,7 +100,7 @@ function Test-TargetResource
97100
)
98101

99102
# Flag to signal whether settings are correct
100-
[Boolean] $desiredConfigurationMatch = $true
103+
[System.Boolean] $desiredConfigurationMatch = $true
101104

102105
Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
103106
$($script:localizedData.CheckingOfflineDomainJoinMessage)
@@ -166,9 +169,10 @@ function Join-Domain {
166169
'/WINDOWSPATH'
167170
$ENV:SystemRoot
168171
'/LOCALOS')
172+
169173
if ($LASTEXITCODE -eq 0)
170174
{
171-
# Notify DSC that a reboot is required.
175+
# Notify DSC that a reboot is required.
172176
$global:DSCMachineStatus = 1
173177
}
174178
else
@@ -206,14 +210,14 @@ function Get-DomainName
206210
param()
207211

208212
# Use CIM to detect the domain name so that this will work on Nano Server.
209-
$ComputerSystem = Get-CimInstance -ClassName win32_computersystem -Namespace root\cimv2
210-
if ($ComputerSystem.Workgroup)
213+
$computerSystem = Get-CimInstance -ClassName 'Win32_ComputerSystem' -Namespace root\cimv2
214+
if ($computerSystem.Workgroup)
211215
{
212216
return $null
213217
}
214218
else
215219
{
216-
$ComputerSystem.Domain
220+
$computerSystem.Domain
217221
}
218222
} # function Get-DomainName
219223

0 commit comments

Comments
 (0)