Skip to content

Commit fd98a90

Browse files
committed
Merge remote-tracking branch 'refs/remotes/PowerShell/dev'
2 parents 5f99cf2 + 8f47d6a commit fd98a90

5 files changed

Lines changed: 240 additions & 15 deletions

File tree

DSCResources/MSFT_xComputer/MSFT_xComputer.psm1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,11 @@ function Test-TargetResource
220220
Write-Verbose -Message "Checking if workgroup name is $WorkGroupName"
221221
return ($WorkGroupName -eq (gwmi WIN32_ComputerSystem).WorkGroup)
222222
}
223-
224-
return $true
223+
else
224+
{
225+
## No Domain or Workgroup specified and computer name is correct
226+
return $true;
227+
}
225228
}
226229

227230
function ValidateDomainOrWorkGroup($DomainName, $WorkGroupName)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ xComputer resource has following properties:
4545

4646
## Versions
4747

48+
49+
### 1.3.0
50+
51+
* xComputer
52+
* Fixed issue with Test-TargetResource when not specifying Domain or Workgroup name
53+
* Added tests
54+
4855
### 1.2.2
4956

5057
Added types to Get/Set/Test definitions to allow xResourceDesigner validation to succeed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
$Module = "$PSScriptRoot\..\DSCResources\MSFT_xComputer\MSFT_xComputer.psm1"
2+
Remove-Module -Name MSFT_xComputer -Force -ErrorAction SilentlyContinue
3+
Import-Module -Name $Module -Force -ErrorAction Stop
4+
5+
InModuleScope MSFT_xComputer {
6+
7+
Describe 'MSFT_xComputer' {
8+
9+
$SecPassword = ConvertTo-SecureString -String 'password' -AsPlainText -Force
10+
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'USER',$SecPassword
11+
$NotComputerName = if($env:COMPUTERNAME -ne 'othername'){'othername'}else{'name'}
12+
13+
Context Test-TargetResource {
14+
Mock Get-WMIObject {[PSCustomObject]@{DomainName = 'ContosoLtd'}} -ParameterFilter {$Class -eq 'Win32_NTDomain'}
15+
It 'Throws if both DomainName and WorkGroupName are specified' {
16+
{Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw
17+
}
18+
It 'Throws if Domain is specified without Credentials' {
19+
{Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com'} | Should Throw
20+
}
21+
It 'Should return True if Domain name is same as specified' {
22+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
23+
Mock GetComputerDomain {'contoso.com'}
24+
Test-TargetResource -Name $Env:ComputerName -DomainName 'Contoso.com' -Credential $Credential | Should Be $true
25+
}
26+
It 'Should return True if Workgroup name is same as specified' {
27+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
28+
Mock GetComputerDomain {''}
29+
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true
30+
}
31+
It 'Should return True if ComputerName and Domain name is same as specified' {
32+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
33+
Mock GetComputerDomain {'contoso.com'}
34+
Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $true
35+
}
36+
It 'Should return True if ComputerName and Workgroup is same as specified' {
37+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
38+
Mock GetComputerDomain {''}
39+
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true
40+
}
41+
It 'Should return True if ComputerName is same and no Domain or Workgroup specified' {
42+
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
43+
Mock GetComputerDomain {''}
44+
Test-TargetResource -Name $Env:ComputerName | Should Be $true
45+
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
46+
Mock GetComputerDomain {'contoso.com'}
47+
Test-TargetResource -Name $Env:ComputerName | Should Be $true
48+
}
49+
It 'Should return False if ComputerName is not same and no Domain or Workgroup specified' {
50+
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
51+
Mock GetComputerDomain {''}
52+
Test-TargetResource -Name $NotComputerName | Should Be $false
53+
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
54+
Mock GetComputerDomain {'contoso.com'}
55+
Test-TargetResource -Name $NotComputerName | Should Be $false
56+
}
57+
It 'Should return False if Domain name is not same as specified' {
58+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
59+
Mock GetComputerDomain {'contoso.com'}
60+
Test-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential | Should Be $false
61+
}
62+
It 'Should return False if Workgroup name is not same as specified' {
63+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
64+
Mock GetComputerDomain {''}
65+
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'NOTworkgroup' | Should Be $false
66+
}
67+
It 'Should return False if ComputerName is not same as specified' {
68+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
69+
Mock GetComputerDomain {''}
70+
Test-TargetResource -Name $NotComputerName -WorkGroupName 'workgroup' | Should Be $false
71+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
72+
Mock GetComputerDomain {'contoso.com'}
73+
Test-TargetResource -Name $NotComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false
74+
}
75+
It 'Should return False if Computer is in Workgroup and Domain is specified' {
76+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$false}}
77+
Mock GetComputerDomain {''}
78+
Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false
79+
}
80+
It 'Should return False if ComputerName is in Domain and Workgroup is specified' {
81+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
82+
Mock GetComputerDomain {'contoso.com'}
83+
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -Credential $Credential -UnjoinCredential $Credential | Should Be $false
84+
}
85+
86+
}
87+
Context Get-TargetResource {
88+
It 'should not throw' {
89+
{Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw
90+
}
91+
It 'Should return a hashtable containing Name,DomainName, Credential, UnjoinCredential and WorkGroupName' {
92+
$Result = Get-TargetResource -Name $env:COMPUTERNAME
93+
$Result.GetType().Fullname | Should Be 'System.Collections.Hashtable'
94+
$Result.Keys | Should Be @('Name','DomainName','Credential','UnjoinCredential','WorkGroupName')
95+
}
96+
}
97+
Context Set-TargetResource {
98+
Mock Rename-Computer {}
99+
Mock Add-Computer {}
100+
It 'Throws if both DomainName and WorkGroupName are specified' {
101+
{Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw
102+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
103+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
104+
}
105+
It 'Throws if Domain is specified without Credentials' {
106+
{Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com'} | Should Throw
107+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
108+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
109+
}
110+
It 'Changes ComputerName and changes Domain to new Domain' {
111+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
112+
Mock GetComputerDomain {'contoso.com'}
113+
Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
114+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
115+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
116+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
117+
}
118+
It 'Changes ComputerName and changes Domain to Workgroup' {
119+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
120+
Mock GetComputerDomain {'contoso.com'}
121+
Set-TargetResource -Name $NotComputerName -WorkGroupName 'contoso' -Credential $Credential | Should BeNullOrEmpty
122+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
123+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName -and $NewName -and $Credential}
124+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName -or $UnjoinCredential}
125+
}
126+
It 'Changes ComputerName and changes Workgroup to Domain' {
127+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
128+
Mock GetComputerDomain {''}
129+
Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -Credential $Credential | Should BeNullOrEmpty
130+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
131+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
132+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
133+
}
134+
It 'Changes ComputerName and changes Workgroup to new Workgroup' {
135+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
136+
Mock GetComputerDomain {''}
137+
Set-TargetResource -Name $NotComputerName -WorkGroupName 'adventure-works' | Should BeNullOrEmpty
138+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
139+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName -and $NewName}
140+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName}
141+
}
142+
It 'Changes only the Domain to new Domain' {
143+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
144+
Mock GetComputerDomain {'contoso.com'}
145+
Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential -UnjoinCredential $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}
148+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
149+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
150+
}
151+
It 'Changes only Domain to Workgroup' {
152+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
153+
Mock GetComputerDomain {''}
154+
Set-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -UnjoinCredential $Credential | Should BeNullOrEmpty
155+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
156+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
157+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName}
158+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName}
159+
}
160+
It 'Changes only ComputerName in Domain' {
161+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
162+
Mock GetComputerDomain {'contoso.com'}
163+
Set-TargetResource -Name $NotComputerName -Credential $Credential | Should BeNullOrEmpty
164+
Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It
165+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
166+
}
167+
It 'Changes only ComputerName in Workgroup' {
168+
Mock GetComputerDomain {''}
169+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
170+
Set-TargetResource -Name $NotComputerName | Should BeNullOrEmpty
171+
Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It
172+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
173+
}
174+
}
175+
}
176+
}

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.3.{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+
before_deploy:
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.3.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
# Version number of this module.
3-
ModuleVersion = '1.2.2'
3+
ModuleVersion = '1.3.0'
44

55
# ID used to uniquely identify this module
66
GUID = 'B5004952-489E-43EA-999C-F16A25355B89'

0 commit comments

Comments
 (0)