Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions DSCResources/MSFT_xComputer/MSFT_xComputer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function Get-TargetResource

[string] $DomainName,

[string] $JoinOU,

[PSCredential] $Credential,

[PSCredential] $UnjoinCredential,
Expand All @@ -26,6 +28,8 @@ function Get-TargetResource
$returnValue = @{
Name = $env:COMPUTERNAME
DomainName = GetComputerDomain
JoinOU = $JoinOU
CurrentOU = Get-ComputerOU
Credential = [ciminstance]$convertToCimCredential
UnjoinCredential = [ciminstance]$convertToCimUnjoinCredential
WorkGroupName= (gwmi WIN32_ComputerSystem).WorkGroup
Expand All @@ -42,6 +46,8 @@ function Set-TargetResource
[string] $Name,

[string] $DomainName,

[string] $JoinOU,

[PSCredential] $Credential,

Expand Down Expand Up @@ -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)."
}
Expand All @@ -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)."
}
Expand Down Expand Up @@ -182,6 +198,8 @@ function Test-TargetResource
(
[parameter(Mandatory)]
[string] $Name,

[string] $JoinOU,

[PSCredential]$Credential,

Expand Down Expand Up @@ -247,5 +265,18 @@ function GetComputerDomain
}
}

Export-ModuleMember -Function *-TargetResource
function Get-ComputerOU
{
$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
20 changes: 11 additions & 9 deletions DSCResources/MSFT_xComputer/MSFT_xComputer.schema.mof
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[ClassVersion("1.0.1.0"), FriendlyName("xComputer")]
class MSFT_xComputer : OMI_BaseResource
{
[key] string Name;
[write] string DomainName;
[write,EmbeddedInstance("MSFT_Credential")] String Credential;
[write,EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
[write] string WorkGroupName;
};
[ClassVersion("1.0.1.0"), FriendlyName("xComputer")]
class MSFT_xComputer : OMI_BaseResource
{
[key] string Name;
[write] string DomainName;
[write] string JoinOU;
[read] string CurrentOU;
[write,EmbeddedInstance("MSFT_Credential")] String Credential;
[write,EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
[write] string WorkGroupName;
};
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 27 additions & 2 deletions Tests/xComputermanagement.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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','Credential','UnjoinCredential','WorkGroupName')
$Result.Keys | Should Be @('Name', 'DomainName', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName')
}
}
Context Set-TargetResource {
Expand All @@ -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'}
Expand All @@ -131,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 {''}
Expand All @@ -148,6 +164,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 {''}
Expand Down