Skip to content

Commit 7385faa

Browse files
authored
Merge pull request #1 from gesbeckj/xDescription
xComputer: Added Description Property
2 parents e98a911 + 6d5b06d commit 7385faa

5 files changed

Lines changed: 83 additions & 6 deletions

File tree

DSCResources/MSFT_xComputer/MSFT_xComputer.psm1

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ function Get-TargetResource
3333

3434
[Parameter()]
3535
[System.String]
36-
$WorkGroupName
36+
$WorkGroupName,
37+
38+
[Parameter()]
39+
[System.String]
40+
$Description
3741
)
3842

3943
Write-Verbose -Message "Getting computer state for '$($Name)'."
@@ -64,6 +68,7 @@ function Get-TargetResource
6468
Credential = [ciminstance]$convertToCimCredential
6569
UnjoinCredential = [ciminstance]$convertToCimUnjoinCredential
6670
WorkGroupName = (Get-CimInstance -Class 'Win32_ComputerSystem').Workgroup
71+
Description = (Get-CimInstance -Class 'Win32_OperatingSystem').Description
6772
}
6873

6974
$returnValue
@@ -98,7 +103,11 @@ function Set-TargetResource
98103

99104
[Parameter()]
100105
[System.String]
101-
$WorkGroupName
106+
$WorkGroupName,
107+
108+
[Parameter()]
109+
[System.String]
110+
$Description
102111
)
103112

104113
Assert-DomainOrWorkGroup -DomainName $DomainName -WorkGroupName $WorkGroupName
@@ -108,6 +117,11 @@ function Set-TargetResource
108117
$Name = $env:COMPUTERNAME
109118
}
110119

120+
Write-Verbose -message "Setting description to '$($Description)'."
121+
$Win32_OperatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem
122+
$Win32_OperatingSystem.Description = $Description
123+
Set-CimInstance -InputObject $Win32_OperatingSystem
124+
111125
if ($Credential)
112126
{
113127
if ($DomainName)
@@ -274,7 +288,11 @@ function Test-TargetResource
274288

275289
[Parameter()]
276290
[System.String]
277-
$WorkGroupName
291+
$WorkGroupName,
292+
293+
[Parameter()]
294+
[System.String]
295+
$Description
278296
)
279297

280298
Write-Verbose -Message 'Validate desired Name is a valid name'
@@ -285,6 +303,12 @@ function Test-TargetResource
285303
return $false
286304
}
287305

306+
Write-Verbose -Message 'Checking if description is corerect'
307+
if ($Description -ne (Get-CimInstance -Class 'Win32_OperatingSystem').Description)
308+
{
309+
return $false
310+
}
311+
288312
Assert-DomainOrWorkGroup -DomainName $DomainName -WorkGroupName $WorkGroupName
289313

290314
if ($DomainName)

DSCResources/MSFT_xComputer/MSFT_xComputer.schema.mof

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class MSFT_xComputer : OMI_BaseResource
88
[write,EmbeddedInstance("MSFT_Credential")] String Credential;
99
[write,EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
1010
[write] string WorkGroupName;
11+
[write] string Description;
1112
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<#
2+
.EXAMPLE
3+
This example will set the computer description
4+
#>
5+
Configuration Example
6+
{
7+
param
8+
(
9+
[Parameter()]
10+
[System.String[]]
11+
$NodeName = 'localhost'
12+
)
13+
14+
Import-DscResource -Module xComputerManagement
15+
16+
Node $NodeName
17+
{
18+
xComputer NewDescription
19+
{
20+
Name = 'localhost'
21+
Description = 'This is my computer.'
22+
}
23+
}
24+
}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ xComputer resource has following properties:
5353
* Credential: Credential to be used to join or leave domain
5454
* CurrentOU: A read-only property that specifies the organizational unit that
5555
the computer account is currently in
56+
* Description: The local computer descrption
5657

5758
### xComputer Examples
5859

@@ -61,6 +62,7 @@ xComputer resource has following properties:
6162
* [Set the Name while staying on the Domain](/Examples/xComputer/3-RenameComputerInDomain.ps1)
6263
* [Set the Name while staying on the Workgroup](/Examples/xComputer/4-RenameComputerInWorkgroup.ps1)
6364
* [Switch from a Domain to a Workgroup](/Examples/xComputer/5-UnjoinDomainAndJoinWorkgroup.ps1)
65+
* [Set a Description for the Workstation](/Examples/xComputer/6-SetComputerDescriptionInWorkgroup.ps1)
6466

6567
## xOfflineDomainJoin
6668

@@ -202,6 +204,9 @@ xVirtualMemory has the following properties:
202204

203205
### Unreleased
204206

207+
* xComputer: Added parameter to set the local computer description along with documentation
208+
and unit tests for this change.
209+
205210
### 2.1.0.0
206211

207212
* xComputer: Changed comparison that validates if we are in the correct AD

Tests/Unit/MSFT_xComputer.Tests.ps1

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,25 @@ try
121121
It 'Should not Throw if name is localhost' {
122122
{Test-TargetResource -Name "localhost"} | Should Not Throw
123123
}
124-
124+
It 'Should return true if description is same as specified' {
125+
Mock Get-CimInstance {[PSCustomObject]@{Description = 'This is my computer'}}
126+
Test-TargetResource -Name $env:COMPUTERNAME -Description "This is my computer" | Should Be $true
127+
Test-TargetResource -Name 'localhost' -Description "This is my computer" | Should Be $true
128+
}
129+
It 'Should return false if description is same as specified' {
130+
Mock Get-CimInstance {[PSCustomObject]@{Description = 'This is not my computer'}}
131+
Test-TargetResource -Name $env:COMPUTERNAME -Description "This is my computer" | Should Be $false
132+
Test-TargetResource -Name 'localhost' -Description "This is my computer" | Should Be $false
133+
}
125134
}
126135
Context "$($Global:DSCResourceName)\Get-TargetResource" {
127136
It 'should not throw' {
128137
{Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw
129138
}
130-
It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' {
139+
It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential, WorkGroupName and Description' {
131140
$Result = Get-TargetResource -Name $env:COMPUTERNAME
132141
$Result.GetType().Fullname | Should Be 'System.Collections.Hashtable'
133-
$Result.Keys | Sort-Object | Should Be @('Credential', 'CurrentOU', 'DomainName', 'JoinOU', 'Name', 'UnjoinCredential', 'WorkGroupName')
142+
$Result.Keys | Sort-Object | Should Be @('Credential', 'CurrentOU', 'Description', 'DomainName', 'JoinOU', 'Name', 'UnjoinCredential', 'WorkGroupName')
134143
}
135144
It 'Throws if name is to long' {
136145
{Get-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
@@ -142,6 +151,7 @@ try
142151
Context "$($Global:DSCResourceName)\Set-TargetResource" {
143152
Mock Rename-Computer {}
144153
Mock Add-Computer {}
154+
Mock Set-CimInstance {}
145155
It 'Throws if both DomainName and WorkGroupName are specified' {
146156
{Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw
147157
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
@@ -274,6 +284,19 @@ try
274284
It 'Throws if name contains illigal characters' {
275285
{Set-TargetResource -Name "ThisIsBad<>"} | Should Throw
276286
}
287+
It 'Changes computer description in a workgroup'{
288+
Mock Get-ComputerDomain {''}
289+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
290+
Set-TargetResource -Name $env:COMPUTERNAME -Description = 'This is my computer' -DomainName "" | Should BeNullOrEmpty
291+
Assert-MockCalled -CommandName Set-CimInstance -Exactly 1 -Scope It
292+
}
293+
It 'Changes computer description in a domain'{
294+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
295+
Mock Get-ComputerDomain {'contoso.com'}
296+
Set-TargetResource -Name $Env:ComputerName | Should BeNullOrEmpty
297+
Set-TargetResource -Name $env:COMPUTERNAME -DomainName 'Contoso.com' -Credential $Credential -UnjoinCredential $Credential -Description = 'This is my computer' | Should BeNullOrEmpty
298+
Assert-MockCalled -CommandName Set-CimInstance -Exactly 2 -Scope It
299+
}
277300
}
278301
}
279302
} #end InModuleScope $DSCResourceName

0 commit comments

Comments
 (0)