Skip to content

Commit 85845ca

Browse files
Merge pull request #18 from PowerShell/dev
Release of version 1.4.0.0 of xComputerManagement
2 parents 06fd8c5 + d8153dc commit 85845ca

6 files changed

Lines changed: 180 additions & 27 deletions

File tree

DSCResources/MSFT_xComputer/MSFT_xComputer.psm1

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ function Get-TargetResource
99
param
1010
(
1111
[parameter(Mandatory)]
12+
[ValidateLength(1,15)]
13+
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
1214
[string] $Name,
1315

1416
[string] $DomainName,
1517

18+
[string] $JoinOU,
19+
1620
[PSCredential] $Credential,
1721

1822
[PSCredential] $UnjoinCredential,
@@ -26,6 +30,8 @@ function Get-TargetResource
2630
$returnValue = @{
2731
Name = $env:COMPUTERNAME
2832
DomainName = GetComputerDomain
33+
JoinOU = $JoinOU
34+
CurrentOU = Get-ComputerOU
2935
Credential = [ciminstance]$convertToCimCredential
3036
UnjoinCredential = [ciminstance]$convertToCimUnjoinCredential
3137
WorkGroupName= (gwmi WIN32_ComputerSystem).WorkGroup
@@ -39,9 +45,13 @@ function Set-TargetResource
3945
param
4046
(
4147
[parameter(Mandatory)]
48+
[ValidateLength(1,15)]
49+
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
4250
[string] $Name,
4351

4452
[string] $DomainName,
53+
54+
[string] $JoinOU,
4555

4656
[PSCredential] $Credential,
4757

@@ -73,7 +83,12 @@ function Set-TargetResource
7383
}
7484
else
7585
{
76-
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force
86+
if ($JoinOU) {
87+
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -OUPath $JoinOU -Force
88+
}
89+
else {
90+
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force
91+
}
7792
}
7893
Write-Verbose -Message "Renamed computer to '$($Name)' and added to the domain '$($DomainName)."
7994
}
@@ -86,7 +101,12 @@ function Set-TargetResource
86101
}
87102
else
88103
{
89-
Add-Computer -DomainName $DomainName -Credential $Credential -Force
104+
if ($JoinOU) {
105+
Add-Computer -DomainName $DomainName -Credential $Credential -OUPath $JoinOU -Force
106+
}
107+
else {
108+
Add-Computer -DomainName $DomainName -Credential $Credential -Force
109+
}
90110
}
91111
Write-Verbose -Message "Added computer to domain '$($DomainName)."
92112
}
@@ -181,7 +201,11 @@ function Test-TargetResource
181201
param
182202
(
183203
[parameter(Mandatory)]
204+
[ValidateLength(1,15)]
205+
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
184206
[string] $Name,
207+
208+
[string] $JoinOU,
185209

186210
[PSCredential]$Credential,
187211

@@ -191,6 +215,8 @@ function Test-TargetResource
191215

192216
[string] $WorkGroupName
193217
)
218+
219+
Write-Verbose -Message "Validate desired Name is a valid name"
194220

195221
Write-Verbose -Message "Checking if computer name is $Name"
196222
if ($Name -ne $env:COMPUTERNAME) {return $false}
@@ -247,5 +273,18 @@ function GetComputerDomain
247273
}
248274
}
249275

250-
Export-ModuleMember -Function *-TargetResource
276+
function Get-ComputerOU
277+
{
278+
$ou = $null
251279

280+
if (GetComputerDomain)
281+
{
282+
$dn = $null
283+
$dn = ([adsisearcher]"(&(objectCategory=computer)(objectClass=computer)(cn=$env:COMPUTERNAME))").FindOne().Properties.distinguishedname
284+
$ou = $dn -replace '^(CN=.*?(?<=,))', ''
285+
}
286+
287+
return $ou
288+
}
289+
290+
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ 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

50+
### Unreleased
51+
52+
### 1.4.0.0
53+
* Adding Name parameter validation
4854

4955
### 1.3.0
5056

Tests/xComputermanagement.Tests.ps1

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,28 @@ InModuleScope MSFT_xComputer {
8282
Mock GetComputerDomain {'contoso.com'}
8383
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -Credential $Credential -UnjoinCredential $Credential | Should Be $false
8484
}
85+
It 'Throws if name is to long' {
86+
{Test-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
87+
}
88+
It 'Throws if name contains illigal characters' {
89+
{Test-TargetResource -Name "ThisIsBad<>"} | Should Throw
90+
}
8591

8692
}
8793
Context Get-TargetResource {
8894
It 'should not throw' {
8995
{Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw
9096
}
91-
It 'Should return a hashtable containing Name,DomainName, Credential, UnjoinCredential and WorkGroupName' {
97+
It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' {
9298
$Result = Get-TargetResource -Name $env:COMPUTERNAME
9399
$Result.GetType().Fullname | Should Be 'System.Collections.Hashtable'
94-
$Result.Keys | Should Be @('Name','DomainName','Credential','UnjoinCredential','WorkGroupName')
100+
$Result.Keys | Should Be @('Name', 'DomainName', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName')
101+
}
102+
It 'Throws if name is to long' {
103+
{Get-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
104+
}
105+
It 'Throws if name contains illigal characters' {
106+
{Get-TargetResource -Name "ThisIsBad<>"} | Should Throw
95107
}
96108
}
97109
Context Set-TargetResource {
@@ -115,6 +127,14 @@ InModuleScope MSFT_xComputer {
115127
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
116128
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
117129
}
130+
It 'Changes ComputerName and changes Domain to new Domain with specified OU' {
131+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
132+
Mock GetComputerDomain {'contoso.com'}
133+
Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
134+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
135+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
136+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
137+
}
118138
It 'Changes ComputerName and changes Domain to Workgroup' {
119139
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
120140
Mock GetComputerDomain {'contoso.com'}
@@ -131,6 +151,14 @@ InModuleScope MSFT_xComputer {
131151
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
132152
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
133153
}
154+
It 'Changes ComputerName and changes Workgroup to Domain with specified OU' {
155+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
156+
Mock GetComputerDomain {''}
157+
Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential | Should BeNullOrEmpty
158+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
159+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
160+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
161+
}
134162
It 'Changes ComputerName and changes Workgroup to new Workgroup' {
135163
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
136164
Mock GetComputerDomain {''}
@@ -148,6 +176,15 @@ InModuleScope MSFT_xComputer {
148176
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
149177
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
150178
}
179+
It 'Changes only the Domain to new Domain with specified OU' {
180+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
181+
Mock GetComputerDomain {'contoso.com'}
182+
Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
183+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
184+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName}
185+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
186+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
187+
}
151188
It 'Changes only Domain to Workgroup' {
152189
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
153190
Mock GetComputerDomain {''}
@@ -171,6 +208,12 @@ InModuleScope MSFT_xComputer {
171208
Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It
172209
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
173210
}
211+
It 'Throws if name is to long' {
212+
{Set-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
213+
}
214+
It 'Throws if name contains illigal characters' {
215+
{Set-TargetResource -Name "ThisIsBad<>"} | Should Throw
216+
}
174217
}
175218
}
176219
}

appveyor.yml

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
#---------------------------------#
2+
# environment configuration #
3+
#---------------------------------#
4+
version: 1.4.{build}.0
15
install:
2-
- cinst -y pester
3-
- git clone https://github.com/PowerShell/DscResource.Tests
6+
- cinst -y pester
7+
- git clone https://github.com/PowerShell/DscResource.Tests
8+
- ps: Push-Location
9+
- cd DscResource.Tests
10+
- ps: Import-Module .\TestHelper.psm1 -force
11+
- ps: Pop-Location
12+
13+
#---------------------------------#
14+
# build configuration #
15+
#---------------------------------#
416

517
build: false
618

19+
#---------------------------------#
20+
# test configuration #
21+
#---------------------------------#
22+
723
test_script:
824
- ps: |
925
$testResultsFile = ".\TestsResults.xml"
@@ -12,14 +28,37 @@ test_script:
1228
if ($res.FailedCount -gt 0) {
1329
throw "$($res.FailedCount) tests failed."
1430
}
15-
on_finish:
16-
- ps: |
17-
$stagingDirectory = (Resolve-Path ..).Path
18-
$zipFile = Join-Path $stagingDirectory "$(Split-Path $pwd -Leaf).zip"
19-
Add-Type -assemblyname System.IO.Compression.FileSystem
20-
[System.IO.Compression.ZipFile]::CreateFromDirectory($pwd, $zipFile)
21-
@(
22-
# You can add other artifacts here
23-
(ls $zipFile)
24-
) | % { Push-AppveyorArtifact $_.FullName }
31+
32+
#---------------------------------#
33+
# deployment configuration #
34+
#---------------------------------#
35+
36+
# scripts to run before deployment
37+
deploy_script:
38+
- ps: |
39+
# Creating project artifact
40+
$stagingDirectory = (Resolve-Path ..).Path
41+
$manifest = Join-Path $pwd "xComputerManagement.psd1"
42+
(Get-Content $manifest -Raw).Replace("1.4.0.0", $env:APPVEYOR_BUILD_VERSION) | Out-File $manifest
43+
$zipFilePath = Join-Path $stagingDirectory "$(Split-Path $pwd -Leaf).zip"
44+
Add-Type -assemblyname System.IO.Compression.FileSystem
45+
[System.IO.Compression.ZipFile]::CreateFromDirectory($pwd, $zipFilePath)
46+
47+
# Creating NuGet package artifact
48+
New-Nuspec -packageName $env:APPVEYOR_PROJECT_NAME -version $env:APPVEYOR_BUILD_VERSION -author "Microsoft" -owners "Microsoft" -licenseUrl "https://github.com/PowerShell/DscResources/blob/master/LICENSE" -projectUrl "https://github.com/$($env:APPVEYOR_REPO_NAME)" -packageDescription $env:APPVEYOR_PROJECT_NAME -tags "DesiredStateConfiguration DSC DSCResourceKit" -destinationPath .
49+
nuget pack ".\$($env:APPVEYOR_PROJECT_NAME).nuspec" -outputdirectory .
50+
$nuGetPackageName = $env:APPVEYOR_PROJECT_NAME + "." + $env:APPVEYOR_BUILD_VERSION + ".nupkg"
51+
$nuGetPackagePath = (Get-ChildItem $nuGetPackageName).FullName
52+
53+
@(
54+
# You can add other artifacts here
55+
$zipFilePath,
56+
$nuGetPackagePath
57+
) | % {
58+
Write-Host "Pushing package $_ as Appveyor artifact"
59+
Push-AppveyorArtifact $_
60+
}
61+
62+
63+
2564

xComputerManagement.psd1

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
# Version number of this module.
3-
ModuleVersion = '1.3.0'
3+
ModuleVersion = '1.4.0.0'
44

55
# ID used to uniquely identify this module
66
GUID = 'B5004952-489E-43EA-999C-F16A25355B89'
@@ -30,5 +30,29 @@ FunctionsToExport = '*'
3030

3131
# Cmdlets to export from this module
3232
CmdletsToExport = '*'
33+
34+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
35+
PrivateData = @{
36+
37+
PSData = @{
38+
39+
# Tags applied to this module. These help with module discovery in online galleries.
40+
Tags = @('DesiredStateConfiguration', 'DSC', 'DSCResourceKit', 'DSCResource')
41+
42+
# A URL to the license for this module.
43+
LicenseUri = 'https://github.com/PowerShell/xComputerManagement/blob/master/LICENSE'
44+
45+
# A URL to the main website for this project.
46+
ProjectUri = 'https://github.com/PowerShell/xComputerManagement'
47+
48+
# A URL to an icon representing this module.
49+
# IconUri = ''
50+
51+
# ReleaseNotes of this module
52+
# ReleaseNotes = ''
53+
54+
} # End of PSData hashtable
55+
56+
} # End of PrivateData hashtable
3357
}
3458

0 commit comments

Comments
 (0)