Skip to content

Commit d715779

Browse files
author
Tyson J. Hayes
committed
Merge pull request #13 from PrahlM93/JoinOuProperty
Add the ability to specify an OU upon Domain Join
2 parents 7fe0e4a + 8785de7 commit d715779

4 files changed

Lines changed: 74 additions & 14 deletions

File tree

DSCResources/MSFT_xComputer/MSFT_xComputer.psm1

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function Get-TargetResource
1313

1414
[string] $DomainName,
1515

16+
[string] $JoinOU,
17+
1618
[PSCredential] $Credential,
1719

1820
[PSCredential] $UnjoinCredential,
@@ -26,6 +28,8 @@ function Get-TargetResource
2628
$returnValue = @{
2729
Name = $env:COMPUTERNAME
2830
DomainName = GetComputerDomain
31+
JoinOU = $JoinOU
32+
CurrentOU = Get-ComputerOU
2933
Credential = [ciminstance]$convertToCimCredential
3034
UnjoinCredential = [ciminstance]$convertToCimUnjoinCredential
3135
WorkGroupName= (gwmi WIN32_ComputerSystem).WorkGroup
@@ -42,6 +46,8 @@ function Set-TargetResource
4246
[string] $Name,
4347

4448
[string] $DomainName,
49+
50+
[string] $JoinOU,
4551

4652
[PSCredential] $Credential,
4753

@@ -73,7 +79,12 @@ function Set-TargetResource
7379
}
7480
else
7581
{
76-
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force
82+
if ($JoinOU) {
83+
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -OUPath $JoinOU -Force
84+
}
85+
else {
86+
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force
87+
}
7788
}
7889
Write-Verbose -Message "Renamed computer to '$($Name)' and added to the domain '$($DomainName)."
7990
}
@@ -86,7 +97,12 @@ function Set-TargetResource
8697
}
8798
else
8899
{
89-
Add-Computer -DomainName $DomainName -Credential $Credential -Force
100+
if ($JoinOU) {
101+
Add-Computer -DomainName $DomainName -Credential $Credential -OUPath $JoinOU -Force
102+
}
103+
else {
104+
Add-Computer -DomainName $DomainName -Credential $Credential -Force
105+
}
90106
}
91107
Write-Verbose -Message "Added computer to domain '$($DomainName)."
92108
}
@@ -182,6 +198,8 @@ function Test-TargetResource
182198
(
183199
[parameter(Mandatory)]
184200
[string] $Name,
201+
202+
[string] $JoinOU,
185203

186204
[PSCredential]$Credential,
187205

@@ -247,5 +265,18 @@ function GetComputerDomain
247265
}
248266
}
249267

250-
Export-ModuleMember -Function *-TargetResource
268+
function Get-ComputerOU
269+
{
270+
$ou = $null
251271

272+
if (GetComputerDomain)
273+
{
274+
$dn = $null
275+
$dn = ([adsisearcher]"(&(objectCategory=computer)(objectClass=computer)(cn=$env:COMPUTERNAME))").FindOne().Properties.distinguishedname
276+
$ou = $dn -replace '^(CN=.*?(?<=,))', ''
277+
}
278+
279+
return $ou
280+
}
281+
282+
Export-ModuleMember -Function *-TargetResource
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
[ClassVersion("1.0.1.0"), FriendlyName("xComputer")]
2-
class MSFT_xComputer : OMI_BaseResource
3-
{
4-
[key] string Name;
5-
[write] string DomainName;
6-
[write,EmbeddedInstance("MSFT_Credential")] String Credential;
7-
[write,EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
8-
[write] string WorkGroupName;
9-
};
1+
[ClassVersion("1.0.1.0"), FriendlyName("xComputer")]
2+
class MSFT_xComputer : OMI_BaseResource
3+
{
4+
[key] string Name;
5+
[write] string DomainName;
6+
[write] string JoinOU;
7+
[read] string CurrentOU;
8+
[write,EmbeddedInstance("MSFT_Credential")] String Credential;
9+
[write,EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
10+
[write] string WorkGroupName;
11+
};

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ xComputer resource has following properties:
4040

4141
* Name: The desired computer name
4242
* DomainName: The name of the domain to join
43+
* JoinOU: The distinguished name of the organizational unit that the computer account will be created in
4344
* WorkGroupName: The name of the workgroup
4445
* Credential: Credential to be used to join or leave domain
46+
* CurrentOU: A read-only property that specifies the organizational unit that the computer account is currently in
4547

4648
## Versions
4749

Tests/xComputermanagement.Tests.ps1

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ InModuleScope MSFT_xComputer {
8888
It 'should not throw' {
8989
{Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw
9090
}
91-
It 'Should return a hashtable containing Name,DomainName, Credential, UnjoinCredential and WorkGroupName' {
91+
It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' {
9292
$Result = Get-TargetResource -Name $env:COMPUTERNAME
9393
$Result.GetType().Fullname | Should Be 'System.Collections.Hashtable'
94-
$Result.Keys | Should Be @('Name','DomainName','Credential','UnjoinCredential','WorkGroupName')
94+
$Result.Keys | Should Be @('Name', 'DomainName', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName')
9595
}
9696
}
9797
Context Set-TargetResource {
@@ -115,6 +115,14 @@ InModuleScope MSFT_xComputer {
115115
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
116116
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
117117
}
118+
It 'Changes ComputerName and changes Domain to new Domain with specified OU' {
119+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
120+
Mock GetComputerDomain {'contoso.com'}
121+
Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
122+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
123+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
124+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
125+
}
118126
It 'Changes ComputerName and changes Domain to Workgroup' {
119127
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
120128
Mock GetComputerDomain {'contoso.com'}
@@ -131,6 +139,14 @@ InModuleScope MSFT_xComputer {
131139
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
132140
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
133141
}
142+
It 'Changes ComputerName and changes Workgroup to Domain with specified OU' {
143+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
144+
Mock GetComputerDomain {''}
145+
Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential | Should BeNullOrEmpty
146+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
147+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
148+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
149+
}
134150
It 'Changes ComputerName and changes Workgroup to new Workgroup' {
135151
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
136152
Mock GetComputerDomain {''}
@@ -148,6 +164,15 @@ InModuleScope MSFT_xComputer {
148164
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
149165
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
150166
}
167+
It 'Changes only the Domain to new Domain with specified OU' {
168+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
169+
Mock GetComputerDomain {'contoso.com'}
170+
Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
171+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
172+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName}
173+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
174+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
175+
}
151176
It 'Changes only Domain to Workgroup' {
152177
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
153178
Mock GetComputerDomain {''}

0 commit comments

Comments
 (0)