From c8320ff8bd21d0c34423e2eaca875d2fde359635 Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 12:26:34 -0400 Subject: [PATCH 1/9] Added the ability to specify an OU upon joining to the Domain --- .../MSFT_xComputer/MSFT_xComputer.psm1 | 37 ++++++++++++++++-- .../MSFT_xComputer/MSFT_xComputer.schema.mof | Bin 326 -> 788 bytes 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 b/DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 index 92056ffd..b71f2233 100644 --- a/DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 +++ b/DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 @@ -13,6 +13,8 @@ function Get-TargetResource [string] $DomainName, + [string] $JoinOU, + [PSCredential] $Credential, [PSCredential] $UnjoinCredential, @@ -26,6 +28,8 @@ function Get-TargetResource $returnValue = @{ Name = $env:COMPUTERNAME DomainName = GetComputerDomain + JoinOU = $JoinOU + CurrentOU = GetComputerOU Credential = [ciminstance]$convertToCimCredential UnjoinCredential = [ciminstance]$convertToCimUnjoinCredential WorkGroupName= (gwmi WIN32_ComputerSystem).WorkGroup @@ -42,6 +46,8 @@ function Set-TargetResource [string] $Name, [string] $DomainName, + + [string] $JoinOU, [PSCredential] $Credential, @@ -73,7 +79,12 @@ function Set-TargetResource } else { - Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force + if ($JoinOU) { + Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -OUPath $JoinOU -Force + } + else { + Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force + } } Write-Verbose -Message "Renamed computer to '$($Name)' and added to the domain '$($DomainName)." } @@ -86,7 +97,12 @@ function Set-TargetResource } else { - Add-Computer -DomainName $DomainName -Credential $Credential -Force + if ($JoinOU) { + Add-Computer -DomainName $DomainName -Credential $Credential -OUPath $JoinOU -Force + } + else { + Add-Computer -DomainName $DomainName -Credential $Credential -Force + } } Write-Verbose -Message "Added computer to domain '$($DomainName)." } @@ -182,6 +198,8 @@ function Test-TargetResource ( [parameter(Mandatory)] [string] $Name, + + [string] $JoinOU, [PSCredential]$Credential, @@ -247,5 +265,18 @@ function GetComputerDomain } } -Export-ModuleMember -Function *-TargetResource +function GetComputerOU +{ + $ou = $null + if (GetComputerDomain) + { + $dn = $null + $dn = ([adsisearcher]"(&(objectCategory=computer)(objectClass=computer)(cn=$env:COMPUTERNAME))").FindOne().Properties.distinguishedname + $ou = $dn -replace '^(CN=.*?(?<=,))', '' + } + + return $ou +} + +Export-ModuleMember -Function *-TargetResource diff --git a/DSCResources/MSFT_xComputer/MSFT_xComputer.schema.mof b/DSCResources/MSFT_xComputer/MSFT_xComputer.schema.mof index ef592e058c73a74e2655ff43d542af6a686c96f0..ddf833ff1c7fd85f39e6f410c3e937a0278ff32d 100644 GIT binary patch literal 788 zcmchV%}WAN6vfXv=zn;)Dv+pc?HV-UW=(GBX=n#ADva zymQXI=ic}A*-@m4Vl5bL4V1Dbn#t=#C0*!D=X{&Bq*ImUR0EZO_3Fc2>kj^uvoYS# zn{k?JrDf1F*0YXvq~32Nr-mNTdDKe*!|9SJO)z!tuS0eN-J`ZZdqwmfdsBQ5vjc6T zqcQ*2L~|gQAxp0zr=JY3GWgeku{`sfO7Icm)c=Wp3*Wd+wUmvU5;3NF`KVFwXZIcl zMciG^WmTzk7i^zla~+mBk6Eoo){$MMBitHlqn&`7aBtmlbYs6>MaZm&|Hz|-kI}Y{ Zhvk&7>=QaEFXV>VUeP?$)sJnLz5%x}f_VS{ literal 326 zcmb7;O$&lR5Qgvmip7&cL|yZiec-`@?Bft3n>)m&&dTgcN%Y^-1dHg>z+B#!dFDC# zIoEm*q@_Z~9uG&uW^H%C79;^hUaxooj(zi0QC0@Xc4I4TZ&(;Dwn?9}30sFjGUXaJ zpjAaFSa*vxbIhQQnKmTx%vze)#aEIB`eLREF7dk_%!?BU0U|(cIHrxs>#a{90FC5% hQxVlvL&gi>}(&N^#n2aZNmTn From e1442c9a12c20a2caf2188cc3dfd22438db8ef6b Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 12:29:57 -0400 Subject: [PATCH 2/9] Updated the readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index fb1beb55..8d3cf9db 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,10 @@ xComputer resource has following properties: * Name: The desired computer name * DomainName: The name of the domain to join +* JoinOU: The distinguished name of the organizational unit that the computer account will be created in * WorkGroupName: The name of the workgroup * Credential: Credential to be used to join or leave domain +* CurrentOU: A read-only property that specifies the organizational unit that the computer account is currently in ## Versions From e961a1a05f30a85800e64336d5778a28bcada772 Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 12:34:55 -0400 Subject: [PATCH 3/9] Updated test --- Tests/xComputermanagement.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/xComputermanagement.Tests.ps1 b/Tests/xComputermanagement.Tests.ps1 index 5166519e..8822dd3f 100644 --- a/Tests/xComputermanagement.Tests.ps1 +++ b/Tests/xComputermanagement.Tests.ps1 @@ -91,7 +91,7 @@ InModuleScope MSFT_xComputer { It 'Should return a hashtable containing Name,DomainName, Credential, UnjoinCredential and WorkGroupName' { $Result = Get-TargetResource -Name $env:COMPUTERNAME $Result.GetType().Fullname | Should Be 'System.Collections.Hashtable' - $Result.Keys | Should Be @('Name','DomainName','Credential','UnjoinCredential','WorkGroupName') + $Result.Keys | Should Be @('Name','DomainName', 'JoinOU', 'CurrentOU', 'Credential','UnjoinCredential','WorkGroupName') } } Context Set-TargetResource { From 0b390536b982fa9eb3a3f4fb3b1467a998519f2d Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 12:45:19 -0400 Subject: [PATCH 4/9] Fix file encoding --- .../MSFT_xComputer/MSFT_xComputer.schema.mof | Bin 788 -> 393 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/DSCResources/MSFT_xComputer/MSFT_xComputer.schema.mof b/DSCResources/MSFT_xComputer/MSFT_xComputer.schema.mof index ddf833ff1c7fd85f39e6f410c3e937a0278ff32d..44bc6b9763019a6519b04eed6c7b62d1de8f7342 100644 GIT binary patch literal 393 zcmb79%?g4*5WWk(!{SLHqONtzekib@vJMfkxkGH}Y|O4yMDN~erl$@K%w@j${&r!? zwcY?}snGYj?M~aS7qr-zB%nwibDn|k-9wdKa|7fBd)G+{5{p*j^`Qn>k1e9oJoZc*l6Yc;V86QGNE)b-p~|?#-}p>PT*O~40T(qj%!vR^gO5y-T_&XRYgP4S4d2Xw BfP4S| literal 788 zcmchV%}WAN6vfXv=zn;)Dv+pc?HV-UW=(GBX=n#ADva zymQXI=ic}A*-@m4Vl5bL4V1Dbn#t=#C0*!D=X{&Bq*ImUR0EZO_3Fc2>kj^uvoYS# zn{k?JrDf1F*0YXvq~32Nr-mNTdDKe*!|9SJO)z!tuS0eN-J`ZZdqwmfdsBQ5vjc6T zqcQ*2L~|gQAxp0zr=JY3GWgeku{`sfO7Icm)c=Wp3*Wd+wUmvU5;3NF`KVFwXZIcl zMciG^WmTzk7i^zla~+mBk6Eoo){$MMBitHlqn&`7aBtmlbYs6>MaZm&|Hz|-kI}Y{ Zhvk&7>=QaEFXV>VUeP?$)sJnLz5%x}f_VS{ From 91923b17bca8c69189ed0901e19b5091ec703369 Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 14:43:10 -0400 Subject: [PATCH 5/9] Small fix on the test --- Tests/xComputermanagement.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/xComputermanagement.Tests.ps1 b/Tests/xComputermanagement.Tests.ps1 index 8822dd3f..1e05ff1b 100644 --- a/Tests/xComputermanagement.Tests.ps1 +++ b/Tests/xComputermanagement.Tests.ps1 @@ -88,7 +88,7 @@ InModuleScope MSFT_xComputer { It 'should not throw' { {Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw } - It 'Should return a hashtable containing Name,DomainName, Credential, UnjoinCredential and WorkGroupName' { + It 'Should return a hashtable containing Name,DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' { $Result = Get-TargetResource -Name $env:COMPUTERNAME $Result.GetType().Fullname | Should Be 'System.Collections.Hashtable' $Result.Keys | Should Be @('Name','DomainName', 'JoinOU', 'CurrentOU', 'Credential','UnjoinCredential','WorkGroupName') From ec2135e8c91b08d66ba09898810e86748799b8dc Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 14:43:23 -0400 Subject: [PATCH 6/9] Changed function name to Verb-Noun --- DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 b/DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 index b71f2233..d1967f00 100644 --- a/DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 +++ b/DSCResources/MSFT_xComputer/MSFT_xComputer.psm1 @@ -29,7 +29,7 @@ function Get-TargetResource Name = $env:COMPUTERNAME DomainName = GetComputerDomain JoinOU = $JoinOU - CurrentOU = GetComputerOU + CurrentOU = Get-ComputerOU Credential = [ciminstance]$convertToCimCredential UnjoinCredential = [ciminstance]$convertToCimUnjoinCredential WorkGroupName= (gwmi WIN32_ComputerSystem).WorkGroup @@ -265,7 +265,7 @@ function GetComputerDomain } } -function GetComputerOU +function Get-ComputerOU { $ou = $null From 3fa99a6de2626905a6cc887978cf7f95c37e2d9b Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 14:47:08 -0400 Subject: [PATCH 7/9] Small spacing fix in the tests --- Tests/xComputermanagement.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/xComputermanagement.Tests.ps1 b/Tests/xComputermanagement.Tests.ps1 index 1e05ff1b..38db51be 100644 --- a/Tests/xComputermanagement.Tests.ps1 +++ b/Tests/xComputermanagement.Tests.ps1 @@ -88,10 +88,10 @@ InModuleScope MSFT_xComputer { It 'should not throw' { {Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw } - It 'Should return a hashtable containing Name,DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' { + It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' { $Result = Get-TargetResource -Name $env:COMPUTERNAME $Result.GetType().Fullname | Should Be 'System.Collections.Hashtable' - $Result.Keys | Should Be @('Name','DomainName', 'JoinOU', 'CurrentOU', 'Credential','UnjoinCredential','WorkGroupName') + $Result.Keys | Should Be @('Name', 'DomainName', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName') } } Context Set-TargetResource { From ce84cfa0bdd185e684f68d5277a94cd11682d212 Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 15:23:46 -0400 Subject: [PATCH 8/9] Added a few tests for Set --- Tests/xComputermanagement.Tests.ps1 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Tests/xComputermanagement.Tests.ps1 b/Tests/xComputermanagement.Tests.ps1 index 38db51be..e6b1ce14 100644 --- a/Tests/xComputermanagement.Tests.ps1 +++ b/Tests/xComputermanagement.Tests.ps1 @@ -115,6 +115,14 @@ InModuleScope MSFT_xComputer { Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName} Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} } + It 'Changes ComputerName and changes Domain to new Domain with specified OU' { + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} + Mock GetComputerDomain {'contoso.com'} + Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName} + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} + } It 'Changes ComputerName and changes Domain to Workgroup' { Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} Mock GetComputerDomain {'contoso.com'} @@ -148,6 +156,15 @@ InModuleScope MSFT_xComputer { Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName} Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} } + It 'Changes only the Domain to new Domain with specified OU' { + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} + Mock GetComputerDomain {'contoso.com'} + Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName} + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName} + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} + } It 'Changes only Domain to Workgroup' { Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} Mock GetComputerDomain {''} From 8785de74239e58cc6a09dfde43c548a2ae4ad525 Mon Sep 17 00:00:00 2001 From: prahlm93 Date: Tue, 20 Oct 2015 15:28:53 -0400 Subject: [PATCH 9/9] Another add to the Set tests --- Tests/xComputermanagement.Tests.ps1 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Tests/xComputermanagement.Tests.ps1 b/Tests/xComputermanagement.Tests.ps1 index e6b1ce14..b1649086 100644 --- a/Tests/xComputermanagement.Tests.ps1 +++ b/Tests/xComputermanagement.Tests.ps1 @@ -139,6 +139,14 @@ InModuleScope MSFT_xComputer { Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName} Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} } + It 'Changes ComputerName and changes Workgroup to Domain with specified OU' { + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}} + Mock GetComputerDomain {''} + Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential | Should BeNullOrEmpty + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName} + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} + } It 'Changes ComputerName and changes Workgroup to new Workgroup' { Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}} Mock GetComputerDomain {''}