1+ #
2+ # xComputer: DSC resource to rename a computer and add it to a domain or
3+ # workgroup.
4+ #
15
2- # ######################################################################
3- # The Get-TargetResource cmdlet.
4- # ######################################################################
56function Get-TargetResource
67{
8+ [OutputType ([System.Collections.Hashtable ])]
79 param
8- (
10+ (
911 [parameter (Mandatory )]
1012 [string ] $Name ,
1113
1214 [string ] $DomainName ,
1315
14- [PSCredential ]$Credential ,
16+ [PSCredential ] $Credential ,
1517
16- [PSCredential ]$UnjoinCredential ,
18+ [PSCredential ] $UnjoinCredential ,
1719
1820 [string ] $WorkGroupName
19- )
21+ )
2022
2123 $convertToCimCredential = New-CimInstance - ClassName MSFT_Credential - Property @ {Username = [string ]$Credential.UserName ; Password = [string ]$null } - Namespace root/ microsoft/ windows/ desiredstateconfiguration - ClientOnly
2224 $convertToCimUnjoinCredential = New-CimInstance - ClassName MSFT_Credential - Property @ {Username = [string ]$UnjoinCredential.UserName ; Password = [string ]$null } - Namespace root/ microsoft/ windows/ desiredstateconfiguration - ClientOnly
2325
2426 $returnValue = @ {
2527 Name = $env: COMPUTERNAME
26- DomainName = (gwmi WIN32_ComputerSystem).Domain
28+ DomainName = GetComputerDomain
2729 Credential = [ciminstance ]$convertToCimCredential
2830 UnjoinCredential = [ciminstance ]$convertToCimUnjoinCredential
2931 WorkGroupName = (gwmi WIN32_ComputerSystem).WorkGroup
@@ -32,169 +34,149 @@ function Get-TargetResource
3234 $returnValue
3335}
3436
35- # #######################################################################
36- # The Set-TargetResource cmdlet.
37- # #######################################################################
3837function Set-TargetResource
3938{
4039 param
41- (
40+ (
4241 [parameter (Mandatory )]
4342 [string ] $Name ,
4443
4544 [string ] $DomainName ,
4645
47- [PSCredential ]$Credential ,
46+ [PSCredential ] $Credential ,
4847
49- [PSCredential ]$UnjoinCredential ,
48+ [PSCredential ] $UnjoinCredential ,
5049
5150 [string ] $WorkGroupName
5251 )
5352
54- ValidateDomainWorkGroup - DomainName $DomainName - WorkGroupName $WorkGroupName
55-
56- $currName = $env: COMPUTERNAME
53+ ValidateDomainOrWorkGroup - DomainName $DomainName - WorkGroupName $WorkGroupName
5754
58- if ($Credential )
55+ if ($Credential )
5956 {
60- if ($DomainName )
57+ if ($DomainName )
6158 {
62- $currentMachineDomain = (gwmi win32_computersystem).Domain
63- if ($DomainName -eq $currentMachineDomain )
59+ if ($DomainName -eq (GetComputerDomain))
6460 {
65- # new computer name, stay on the same domain
66- Rename-Computer - NewName $Name - DomainCredential $Credential - Force
67- Write-Verbose - Message " Renamed computer to $ Name"
61+ # Rename the computer, but stay joined to the domain.
62+ Rename-Computer - NewName $Name - DomainCredential $Credential - Force
63+ Write-Verbose - Message " Renamed computer to ' $ ( $ Name) '. "
6864 }
6965 else
7066 {
71- if ($Name -ne $currName )
67+ if ($Name -ne $env: COMPUTERNAME )
7268 {
73- # New computer name, join to domain
69+ # Rename the comptuer, and join it to the domain.
7470 if ($UnjoinCredential )
7571 {
76- # leave old domain with other credential
7772 Add-Computer - DomainName $DomainName - Credential $Credential - NewName $Name - UnjoinDomainCredential $UnjoinCredential - Force
7873 }
7974 else
8075 {
8176 Add-Computer - DomainName $DomainName - Credential $Credential - NewName $Name - Force
8277 }
83- Write-Verbose - Message " Renamed computer to $ Name and added to the domain $ DomainName"
78+ Write-Verbose - Message " Renamed computer to ' $ ( $ Name) ' and added to the domain ' $ ( $ DomainName) . "
8479 }
8580 else
8681 {
87- # Same computer name, join to domain
82+ # Same computer name, and join it to the domain.
8883 if ($UnjoinCredential )
8984 {
90- # leave old domain with other credential
9185 Add-Computer - DomainName $DomainName - Credential $Credential - UnjoinDomainCredential $UnjoinCredential - Force
9286 }
9387 else
9488 {
9589 Add-Computer - DomainName $DomainName - Credential $Credential - Force
9690 }
97- Write-Verbose - Message " Added computer to Domain $ DomainName"
91+ Write-Verbose - Message " Added computer to domain ' $ ( $ DomainName) . "
9892 }
9993 }
10094 }
10195 elseif ($WorkGroupName )
10296 {
103- $currentMachineWorkgroup = (gwmi win32_computersystem).WorkGroup
104- if ($WorkGroupName -eq $currentMachineWorkgroup )
97+ if ($WorkGroupName -eq (gwmi win32_computersystem).WorkGroup)
10598 {
106- # new computer name, stay on the same workgroup
99+ # Rename the comptuer, but stay in the same workgroup.
107100 Rename-Computer - NewName $Name
108- Write-Verbose - Message " Renamed computer to $ Name"
101+ Write-Verbose - Message " Renamed computer to ' $ ( $ Name) '. "
109102 }
110103 else
111104 {
112- if ($Name -ne $currName )
105+ if ($Name -ne $env: COMPUTERNAME )
113106 {
114- # New computer name, join to workgroup
115- Add-Computer - NewName $Name - Credential $Credential - WorkgroupName $WorkGroupName - Force
116- Write-Verbose - Message " Renamed computer to $ Name and addded computer to workgroup $ WorkGroupName"
107+ # Rename the computer, and join it to the workgroup.
108+ Add-Computer - NewName $Name - Credential $Credential - WorkgroupName $WorkGroupName - Force
109+ Write-Verbose - Message " Renamed computer to ' $ ( $ Name) ' and addded to workgroup ' $ ( $ WorkGroupName) '. "
117110 }
118111 else
119112 {
120- # same computer name, join to workgroup
121- Add-Computer - WorkGroupName $WorkGroupName - Credential $Credential - Force
122- Write-Verbose - Message " Added computer to workgroup $ WorkGroupName"
123- }
113+ # Same computer name, and join it to the workgroup.
114+ Add-Computer - WorkGroupName $WorkGroupName - Credential $Credential - Force
115+ Write-Verbose - Message " Added computer to workgroup ' $ ( $ WorkGroupName) '. "
116+ }
124117 }
125118 }
126- # a user neither provides domain nor workgroup
127- elseif ($Name -ne $currName )
119+ elseif ($Name -ne $env: COMPUTERNAME )
128120 {
129- # Check if the computer is domain-joined or part of a workgroup
130- $isMachineInDomain = (Get-WmiObject win32_computersystem).PartOfDomain
131- if ($isMachineInDomain )
121+ if (GetComputerDomain)
132122 {
133- Rename-Computer - NewName $Name - DomainCredential $Credential - Force
134- Write-Verbose - Message " Renamed computer to $ Name"
123+ Rename-Computer - NewName $Name - DomainCredential $Credential - Force
124+ Write-Verbose - Message " Renamed computer to ' $ ( $ Name) '. "
135125 }
136126 else
137127 {
138128 Rename-Computer - NewName $Name - Force
139- Write-Verbose - Message " Renamed computer to $ Name"
129+ Write-Verbose - Message " Renamed computer to ' $ ( $ Name) '. "
140130 }
141- }
131+ }
142132 }
143-
144- # must be non domain scenario - change the machine name in the same workgroup or change workgroup name or both
145133 else
146134 {
147- if ($DomainName )
135+ if ($DomainName )
148136 {
149- throw " Need to specify credentials with domain "
137+ throw " Missing domain join credentials. "
150138 }
151- if ($WorkGroupName )
139+ if ($WorkGroupName )
152140 {
153141
154- if ($WorkGroupName -eq (Get-WmiObject win32_computersystem).Workgroup)
142+ if ($WorkGroupName -eq (Get-WmiObject win32_computersystem).Workgroup)
155143 {
156144 # Same workgroup, new computer name
157145 Rename-Computer - NewName $Name - force
158- Write-Verbose - Message " Renamed computer to $ Name"
146+ Write-Verbose - Message " Renamed computer to ' $ ( $ Name) '. "
159147 }
160148 else
161149 {
162- if ($name -ne $env: COMPUTERNAME )
150+ if ($name -ne $env: COMPUTERNAME )
163151 {
164- # New workgroup, new name
152+ # New workgroup, new computer name
165153 Add-Computer - WorkgroupName $WorkGroupName - NewName $Name
154+ Write-Verbose - Message " Renamed computer to '$ ( $Name ) ' and added to workgroup '$ ( $WorkGroupName ) '."
166155 }
167156 else
168157 {
169- # New workgroup, same name
158+ # New workgroup, same computer name
170159 Add-Computer - WorkgroupName $WorkGroupName
160+ Write-Verbose - Message " Added computer to workgroup '$ ( $WorkGroupName ) '."
171161 }
172-
173- Write-Verbose - Message " Added computer to workgroup $WorkGroupName "
174162 }
175163 }
176164 else
177165 {
178- if ($Name -ne $env: COMPUTERNAME )
166+ if ($Name -ne $env: COMPUTERNAME )
179167 {
180- # Only if new name is different from the current name
181168 Rename-Computer - NewName $Name
182- Write-Verbose - Message " Renamed computer to $ Name"
169+ Write-Verbose - Message " Renamed computer to ' $ ( $ Name) '. "
183170 }
184171 }
185172 }
186-
187-
188-
189- # Tell the DSC Engine to restart the machine
173+
190174 $global :DSCMachineStatus = 1
191175}
192176
193- # ######################################################################
194- # The Test-TargetResource cmdlet.
195- # ######################################################################
196177function Test-TargetResource
197178{
179+ [OutputType ([System.Boolean ])]
198180 [CmdletBinding ()]
199181 param
200182 (
@@ -211,51 +193,54 @@ function Test-TargetResource
211193 )
212194
213195 Write-Verbose - Message " Checking if computer name is $Name "
196+ if ($Name -ne $env: COMPUTERNAME ) {return $false }
197+
198+ ValidateDomainOrWorkGroup - DomainName $DomainName - WorkGroupName $WorkGroupName
214199
215- $testResult = ($Name -eq $env: COMPUTERNAME )
216- ValidateDomainWorkGroup - DomainName $DomainName - WorkGroupName $WorkGroupName
217- $computerSystem = get-WmiObject - Class Win32_ComputerSystem
218200 if ($DomainName )
219201 {
220202 if (! ($Credential ))
221203 {
222204 throw " Need to specify credentials with domain"
223205 }
224- Write-Verbose - Message " Checking if domain name is $DomainName "
225- $domainNameSet = $DomainName.ToLower ().Split(' .' )
226- $testNameSet = $computerSystem.Domain.ToLower ().Split(' .' )
227- if ($domainNameSet.Length -le $testNameSet.Length )
206+
207+ try
228208 {
229- for ($i = 0 ; $i -le $domainNameSet.Length - 1 ; $i ++ )
230- {
231- $testResult = $testResult -and ($domainNameSet [$i ] -eq $testNameSet [$i ])
232- }
209+ Write-Verbose " Checking if the machine is a member of $DomainName ."
210+ return ($DomainName.ToLower () -eq (GetComputerDomain).ToLower())
233211 }
234- else
212+ catch
235213 {
236- $testResult = $testResult -and $false
214+ Write-Verbose ' The machine is not a domain member.'
215+ return $false
237216 }
238217 }
239218 elseif ($WorkGroupName )
240219 {
241220 Write-Verbose - Message " Checking if workgroup name is $WorkGroupName "
242- $testResult = $testResult -and ($WorkGroupName -eq $computerSystem .WorkGroup )
221+ return ($WorkGroupName -eq (gwmi WIN32_ComputerSystem) .WorkGroup)
243222 }
244- $testResult
245223}
246- # ######################################################################
247- # Validation functions (Not exported)
248- # ######################################################################
249224
250- function ValidateDomainWorkGroup
225+ function ValidateDomainOrWorkGroup ( $DomainName , $WorkGroupName )
251226{
252- param ($DomainName , $WorkGroupName )
253- if ($DomainName -and $WorkGroupName )
227+ if ($DomainName -and $WorkGroupName )
254228 {
255- throw " Only one of either the domain name or the workgroup name can be set! Please edit the configuration to ensure that only one of these properties have a value."
256-
229+ throw " Only DomainName or WorkGroupName can be specified at once."
257230 }
258231}
259232
233+ function GetComputerDomain
234+ {
235+ try
236+ {
237+ return ([System.DirectoryServices.ActiveDirectory.Domain ]::GetComputerDomain()).Name
238+ }
239+ catch [System.Management.Automation.MethodInvocationException ]
240+ {
241+ Write-Debug ' This machine is not a domain member.'
242+ }
243+ }
260244
261245Export-ModuleMember - Function *- TargetResource
246+
0 commit comments