Skip to content

Commit 98463a5

Browse files
authored
Merge pull request #162 from r3dlin3/ADJoinWithServer5.0
Computer: AD join with server
2 parents 550074c + e0a0e89 commit 98463a5

5 files changed

Lines changed: 133 additions & 63 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
ComputerManagementDsc.Common module to match what TimeZone requires.
2323
It was not exported in ComputerManagementDsc.ResourceHelper and not
2424
used.
25+
- Add `server` parameter to `Computer` resource - fixes [Issue #161](https://github.com/PowerShell/ComputerManagementDsc/issues/161)
2526

2627
## 5.0.0.0
2728

Modules/ComputerManagementDsc/DSCResources/MSFT_Computer/MSFT_Computer.psm1

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ $script:localizedData = Get-LocalizedData `
4545
4646
.PARAMETER Description
4747
The value assigned here will be set as the local computer description.
48+
49+
.PARAMETER Server
50+
The Active Directory Domain Controller to use to join the domain.
4851
#>
4952
function Get-TargetResource
5053
{
@@ -80,7 +83,11 @@ function Get-TargetResource
8083

8184
[Parameter()]
8285
[System.String]
83-
$Description
86+
$Description,
87+
88+
[Parameter()]
89+
[System.String]
90+
$Server
8491
)
8592

8693
Write-Verbose -Message ($script:localizedData.GettingComputerStateMessage -f $Name)
@@ -112,6 +119,7 @@ function Get-TargetResource
112119
UnjoinCredential = [ciminstance] $convertToCimUnjoinCredential
113120
WorkGroupName = (Get-CimInstance -Class 'Win32_ComputerSystem').Workgroup
114121
Description = (Get-CimInstance -Class 'Win32_OperatingSystem').Description
122+
Server = Get-LogonServer
115123
}
116124

117125
return $returnValue
@@ -142,6 +150,9 @@ function Get-TargetResource
142150
143151
.PARAMETER Description
144152
The value assigned here will be set as the local computer description.
153+
154+
.PARAMETER Server
155+
The Active Directory Domain Controller to use to join the domain.
145156
#>
146157
function Set-TargetResource
147158
{
@@ -176,7 +187,11 @@ function Set-TargetResource
176187

177188
[Parameter()]
178189
[System.String]
179-
$Description
190+
$Description,
191+
192+
[Parameter()]
193+
[System.String]
194+
$Server
180195
)
181196

182197
Write-Verbose -Message ($script:localizedData.SettingComputerStateMessage -f $Name)
@@ -208,71 +223,42 @@ function Set-TargetResource
208223
}
209224
else
210225
{
226+
$addComputerParameters = @{
227+
DomainName = $DomainName
228+
Credential = $Credential
229+
Force = $true
230+
}
231+
$rename = $false
211232
if ($Name -ne $env:COMPUTERNAME)
212233
{
213-
# Rename the computer, and join it to the domain.
214-
if ($UnjoinCredential)
215-
{
216-
Add-Computer `
217-
-DomainName $DomainName `
218-
-Credential $Credential `
219-
-NewName $Name `
220-
-UnjoinDomainCredential $UnjoinCredential `
221-
-Force
222-
}
223-
else
224-
{
225-
if ($JoinOU)
226-
{
227-
Add-Computer `
228-
-DomainName $DomainName `
229-
-Credential $Credential `
230-
-NewName $Name `
231-
-OUPath $JoinOU `
232-
-Force
233-
}
234-
else
235-
{
236-
Add-Computer `
237-
-DomainName $DomainName `
238-
-Credential $Credential `
239-
-NewName $Name `
240-
-Force
241-
}
242-
}
234+
$addComputerParameters.Add("NewName", $Name)
235+
$rename = $true
236+
}
237+
238+
if ($UnjoinCredential)
239+
{
240+
$addComputerParameters.Add("UnjoinDomainCredential", $UnjoinCredential)
241+
}
242+
243+
if ($JoinOU)
244+
{
245+
$addComputerParameters.Add("OUPath", $JoinOU)
246+
}
247+
248+
if ($Server)
249+
{
250+
$addComputerParameters.Add("Server", $Server)
251+
}
252+
253+
# Rename the computer, and join it to the domain.
254+
Add-Computer @addComputerParameters
243255

256+
if ($rename)
257+
{
244258
Write-Verbose -Message ($script:localizedData.RenamedComputerAndJoinedDomainMessage -f $Name,$DomainName)
245259
}
246260
else
247261
{
248-
# Same computer name, and join it to the domain.
249-
if ($UnjoinCredential)
250-
{
251-
Add-Computer `
252-
-DomainName $DomainName `
253-
-Credential $Credential `
254-
-UnjoinDomainCredential $UnjoinCredential `
255-
-Force
256-
}
257-
else
258-
{
259-
if ($JoinOU)
260-
{
261-
Add-Computer `
262-
-DomainName $DomainName `
263-
-Credential $Credential `
264-
-OUPath $JoinOU `
265-
-Force
266-
}
267-
else
268-
{
269-
Add-Computer `
270-
-DomainName $DomainName `
271-
-Credential $Credential `
272-
-Force
273-
}
274-
}
275-
276262
Write-Verbose -Message ($script:localizedData.JoinedDomainMessage -f $DomainName)
277263
}
278264
}
@@ -449,7 +435,11 @@ function Test-TargetResource
449435

450436
[Parameter()]
451437
[System.String]
452-
$Description
438+
$Description,
439+
440+
[Parameter()]
441+
[System.String]
442+
$Server
453443
)
454444

455445
Write-Verbose -Message ($script:localizedData.TestingComputerStateMessage -f $Name)
@@ -621,4 +611,18 @@ function Get-ComputerOU
621611
return $ou
622612
}
623613

614+
<#
615+
.SYNOPSIS
616+
Returns the logon server.
617+
#>
618+
function Get-LogonServer
619+
{
620+
[CmdletBinding()]
621+
[OutputType([System.String])]
622+
param()
623+
624+
$logonserver = $env:LOGONSERVER -replace "\\",""
625+
return $logonserver
626+
}
627+
624628
Export-ModuleMember -Function *-TargetResource

Modules/ComputerManagementDsc/DSCResources/MSFT_Computer/MSFT_Computer.schema.mof

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ class MSFT_Computer : OMI_BaseResource
88
[Write, Description("Credential to be used to leave a domain."), EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
99
[Write, Description("The name of the workgroup.")] String WorkGroupName;
1010
[Write, Description("The value assigned here will be set as the local computer description.")] String Description;
11+
[Write, Description("The Active Directory Domain Controller to use to join the domain")] String Server;
1112
[Read, Description("A read-only property that specifies the organizational unit that the computer account is currently in.")] String CurrentOU;
1213
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.EXAMPLE
3+
This configuration sets the machine name to 'Server01' and
4+
joins the 'Contoso' domain using the domain controller 'dc1.contoso.com'.
5+
Note: this requires an AD credential to join the domain.
6+
#>
7+
Configuration Example
8+
{
9+
param
10+
(
11+
[Parameter()]
12+
[System.String[]]
13+
$NodeName = 'localhost',
14+
15+
[Parameter(Mandatory = $true)]
16+
[ValidateNotNullorEmpty()]
17+
[System.Management.Automation.PSCredential]
18+
$Credential
19+
)
20+
21+
Import-DscResource -Module ComputerManagementDsc
22+
23+
Node $NodeName
24+
{
25+
Computer JoinDomain
26+
{
27+
Name = 'Server01'
28+
DomainName = 'Contoso'
29+
Credential = $Credential # Credential to join to domain
30+
Server = 'dc1.contoso.com'
31+
}
32+
}
33+
}

Tests/Unit/MSFT_Computer.Tests.ps1

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ try
9696
}
9797

9898
It 'Should return True if Workgroup name is same as specified' {
99-
Mock -CommandName Get-WMIObject -MockWith {
99+
Mock -CommandName Get-CimInstance -MockWith {
100100
[PSCustomObject] @{
101101
Domain = 'Workgroup';
102102
Workgroup = 'Workgroup';
@@ -457,7 +457,7 @@ try
457457
-Verbose
458458

459459
$Result.GetType().Fullname | Should -Be 'System.Collections.Hashtable'
460-
$Result.Keys | Sort-Object | Should -Be @('Credential', 'CurrentOU', 'Description', 'DomainName', 'JoinOU', 'Name', 'UnjoinCredential', 'WorkGroupName')
460+
$Result.Keys | Sort-Object | Should -Be @('Credential', 'CurrentOU', 'Description', 'DomainName', 'JoinOU', 'Name', 'Server', 'UnjoinCredential', 'WorkGroupName')
461461
}
462462

463463
It 'Throws if name is to long' {
@@ -613,6 +613,31 @@ try
613613
Assert-MockCalled -CommandName Add-Computer -Exactly -Times 0 -Scope It -ParameterFilter { $WorkGroupName }
614614
}
615615

616+
It 'Changes ComputerName and changes Workgroup to Domain with specified Domain Controller' {
617+
Mock -CommandName Get-WMIObject -MockWith {
618+
[PSCustomObject] @{
619+
Domain = 'Contoso';
620+
Workgroup = 'Contoso';
621+
PartOfDomain = $false
622+
}
623+
}
624+
625+
Mock -CommandName Get-ComputerDomain -MockWith {
626+
''
627+
}
628+
629+
Set-TargetResource `
630+
-Name $notComputerName `
631+
-DomainName 'Contoso.com' `
632+
-Server 'dc01.contoso.com' `
633+
-Credential $credential `
634+
-Verbose | Should -BeNullOrEmpty
635+
636+
Assert-MockCalled -CommandName Rename-Computer -Exactly -Times 0 -Scope It
637+
Assert-MockCalled -CommandName Add-Computer -Exactly -Times 1 -Scope It -ParameterFilter { $DomainName -and $NewName -and $Server }
638+
Assert-MockCalled -CommandName Add-Computer -Exactly -Times 0 -Scope It -ParameterFilter { $WorkGroupName }
639+
}
640+
616641
It 'Changes ComputerName and changes Workgroup to Domain with specified OU' {
617642
Mock -CommandName Get-WMIObject -MockWith {
618643
[PSCustomObject] @{
@@ -1008,6 +1033,12 @@ try
10081033
Assert-MockCalled -CommandName Get-Item -Exactly -Times 0 -Scope It
10091034
}
10101035
}
1036+
1037+
Context "$($script:DSCResourceName)\Get-LogonServer" {
1038+
It 'Should return a non-empty string' {
1039+
Get-LogonServer | Should -Not -BeNullOrEmpty
1040+
}
1041+
}
10111042
}
10121043
} #end InModuleScope $DSCResourceName
10131044
#endregion

0 commit comments

Comments
 (0)