1- [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSAvoidGlobalVars" , " " , Scope= " Function" )]
1+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSAvoidGlobalVars" , " " , Scope = " Function" )]
22param
33(
44)
@@ -10,8 +10,8 @@ function Get-TargetResource
1010 param
1111 (
1212 [Parameter (Mandatory = $true )]
13- [ValidateLength (1 , 15 )]
14- [ValidateScript ({$_ -inotmatch ' [\/\\:*?"<>|]' })]
13+ [ValidateLength (1 , 15 )]
14+ [ValidateScript ( {$_ -inotmatch ' [\/\\:*?"<>|]' })]
1515 [System.String ]
1616 $Name ,
1717
@@ -24,11 +24,11 @@ function Get-TargetResource
2424 $JoinOU ,
2525
2626 [Parameter ()]
27- [PSCredential ]
27+ [System.Management.Automation. PSCredential ]
2828 $Credential ,
2929
3030 [Parameter ()]
31- [PSCredential ]
31+ [System.Management.Automation. PSCredential ]
3232 $UnjoinCredential ,
3333
3434 [Parameter ()]
@@ -38,17 +38,32 @@ function Get-TargetResource
3838
3939 Write-Verbose - Message " Getting computer state for '$ ( $Name ) '."
4040
41- $convertToCimCredential = New-CimInstance - ClassName MSFT_Credential - Property @ {Username = [System.String ]$Credential.UserName ; Password = [System.String ]$null } - Namespace root/ microsoft/ windows/ desiredstateconfiguration - ClientOnly
42- $convertToCimUnjoinCredential = New-CimInstance - ClassName MSFT_Credential - Property @ {Username = [System.String ]$UnjoinCredential.UserName ; Password = [System.String ]$null } - Namespace root/ microsoft/ windows/ desiredstateconfiguration - ClientOnly
41+ $convertToCimCredential = New-CimInstance `
42+ - ClassName MSFT_Credential `
43+ - Property @ {
44+ Username = [System.String ] $Credential.UserName
45+ Password = [System.String ] $null
46+ } `
47+ - Namespace root/ microsoft/ windows/ desiredstateconfiguration `
48+ - ClientOnly
49+
50+ $convertToCimUnjoinCredential = New-CimInstance `
51+ - ClassName MSFT_Credential `
52+ - Property @ {
53+ Username = [System.String ] $UnjoinCredential.UserName
54+ Password = [System.String ]$null
55+ } `
56+ - Namespace root/ microsoft/ windows/ desiredstateconfiguration `
57+ - ClientOnly
4358
4459 $returnValue = @ {
45- Name = $env: COMPUTERNAME
46- DomainName = Get-ComputerDomain
47- JoinOU = $JoinOU
48- CurrentOU = Get-ComputerOU
49- Credential = [ciminstance ]$convertToCimCredential
60+ Name = $env: COMPUTERNAME
61+ DomainName = Get-ComputerDomain
62+ JoinOU = $JoinOU
63+ CurrentOU = Get-ComputerOU
64+ Credential = [ciminstance ]$convertToCimCredential
5065 UnjoinCredential = [ciminstance ]$convertToCimUnjoinCredential
51- WorkGroupName = (Get-CimInstance - Class WIN32_ComputerSystem ).Workgroup
66+ WorkGroupName = (Get-CimInstance - Class ' Win32_ComputerSystem ' ).Workgroup
5267 }
5368
5469 $returnValue
@@ -60,8 +75,8 @@ function Set-TargetResource
6075 param
6176 (
6277 [Parameter (Mandatory = $true )]
63- [ValidateLength (1 , 15 )]
64- [ValidateScript ({$_ -inotmatch ' [\/\\:*?"<>|]' })]
78+ [ValidateLength (1 , 15 )]
79+ [ValidateScript ( {$_ -inotmatch ' [\/\\:*?"<>|]' })]
6580 [System.String ]
6681 $Name ,
6782
@@ -74,11 +89,11 @@ function Set-TargetResource
7489 $JoinOU ,
7590
7691 [Parameter ()]
77- [PSCredential ]
92+ [System.Management.Automation. PSCredential ]
7893 $Credential ,
7994
8095 [Parameter ()]
81- [PSCredential ]
96+ [System.Management.Automation. PSCredential ]
8297 $UnjoinCredential ,
8398
8499 [Parameter ()]
@@ -107,17 +122,19 @@ function Set-TargetResource
107122 {
108123 if ($Name -ne $env: COMPUTERNAME )
109124 {
110- # Rename the comptuer , and join it to the domain.
125+ # Rename the computer , and join it to the domain.
111126 if ($UnjoinCredential )
112127 {
113128 Add-Computer - DomainName $DomainName - Credential $Credential - NewName $Name - UnjoinDomainCredential $UnjoinCredential - Force
114129 }
115130 else
116131 {
117- if ($JoinOU ) {
132+ if ($JoinOU )
133+ {
118134 Add-Computer - DomainName $DomainName - Credential $Credential - NewName $Name - OUPath $JoinOU - Force
119135 }
120- else {
136+ else
137+ {
121138 Add-Computer - DomainName $DomainName - Credential $Credential - NewName $Name - Force
122139 }
123140 }
@@ -132,10 +149,12 @@ function Set-TargetResource
132149 }
133150 else
134151 {
135- if ($JoinOU ) {
152+ if ($JoinOU )
153+ {
136154 Add-Computer - DomainName $DomainName - Credential $Credential - OUPath $JoinOU - Force
137155 }
138- else {
156+ else
157+ {
139158 Add-Computer - DomainName $DomainName - Credential $Credential - Force
140159 }
141160 }
@@ -145,9 +164,9 @@ function Set-TargetResource
145164 }
146165 elseif ($WorkGroupName )
147166 {
148- if ($WorkGroupName -eq (Get-CimInstance - Class WIN32_ComputerSystem ).Workgroup)
167+ if ($WorkGroupName -eq (Get-CimInstance - Class ' Win32_ComputerSystem ' ).Workgroup)
149168 {
150- # Rename the comptuer , but stay in the same workgroup.
169+ # Rename the computer , but stay in the same workgroup.
151170 Rename-Computer - NewName $Name
152171 Write-Verbose - Message " Renamed computer to '$ ( $Name ) '."
153172 }
@@ -167,7 +186,7 @@ function Set-TargetResource
167186 }
168187 }
169188 }
170- elseif ($Name -ne $env: COMPUTERNAME )
189+ elseif ($Name -ne $env: COMPUTERNAME )
171190 {
172191 if (Get-ComputerDomain )
173192 {
@@ -185,12 +204,12 @@ function Set-TargetResource
185204 {
186205 if ($DomainName )
187206 {
188- throw " Missing domain join credentials."
207+ throw ' Missing domain join credentials.'
189208 }
190209 if ($WorkGroupName )
191210 {
192211
193- if ($WorkGroupName -eq (Get-CimInstance - Class WIN32_ComputerSystem ).Workgroup)
212+ if ($WorkGroupName -eq (Get-CimInstance - Class ' Win32_ComputerSystem ' ).Workgroup)
194213 {
195214 # Same workgroup, new computer name
196215 Rename-Computer - NewName $Name - force
@@ -232,8 +251,8 @@ function Test-TargetResource
232251 param
233252 (
234253 [Parameter (Mandatory = $true )]
235- [ValidateLength (1 , 15 )]
236- [ValidateScript ({$_ -inotmatch ' [\/\\:*?"<>|]' })]
254+ [ValidateLength (1 , 15 )]
255+ [ValidateScript ( {$_ -inotmatch ' [\/\\:*?"<>|]' })]
237256 [System.String ]
238257 $Name ,
239258
@@ -242,11 +261,11 @@ function Test-TargetResource
242261 $JoinOU ,
243262
244263 [Parameter ()]
245- [PSCredential ]
264+ [System.Management.Automation. PSCredential ]
246265 $Credential ,
247266
248267 [Parameter ()]
249- [PSCredential ]
268+ [System.Management.Automation. PSCredential ]
250269 $UnjoinCredential ,
251270
252271 [Parameter ()]
@@ -258,60 +277,67 @@ function Test-TargetResource
258277 $WorkGroupName
259278 )
260279
261- Write-Verbose - Message " Validate desired Name is a valid name"
280+ Write-Verbose - Message ' Validate desired Name is a valid name'
262281
263- Write-Verbose - Message " Checking if computer name is correct"
264- if (($Name -ne ' localhost' ) -and ($Name -ne $env: COMPUTERNAME )) {return $false }
282+ Write-Verbose - Message ' Checking if computer name is correct'
283+ if (($Name -ne ' localhost' ) -and ($Name -ne $env: COMPUTERNAME ))
284+ {
285+ return $false
286+ }
265287
266288 Assert-DomainOrWorkGroup - DomainName $DomainName - WorkGroupName $WorkGroupName
267289
268- if ($DomainName )
290+ if ($DomainName )
269291 {
270- if ( ! ($Credential ))
292+ if ( -not ($Credential ))
271293 {
272- throw " Need to specify credentials with domain"
294+ throw ' Need to specify credentials with domain'
273295 }
274296
275297 try
276298 {
277299 Write-Verbose " Checking if the machine is a member of $DomainName ."
300+
278301 if ($DomainName.Contains (' .' ))
279302 {
280- $GetComputerDomainSplat = @ {
303+ $GetComputerDomainParameters = @ {
281304 netbios = $false
282305 }
283306 }
284307 else
285308 {
286- $GetComputerDomainSplat = @ {
309+ $GetComputerDomainParameters = @ {
287310 netbios = $true
288311 }
289312 }
290- return ($DomainName -eq (Get-ComputerDomain @GetComputerDomainSplat ))
313+
314+ return ($DomainName -eq (Get-ComputerDomain @GetComputerDomainParameters ))
291315 }
292316 catch
293317 {
294- Write-Verbose ' The machine is not a domain member.'
295- return $false
318+ Write-Verbose ' The machine is not a domain member.'
319+
320+ return $false
296321 }
297322 }
298- elseif ($WorkGroupName )
323+ elseif ($WorkGroupName )
299324 {
300325 Write-Verbose - Message " Checking if workgroup name is $WorkGroupName "
301- return ($WorkGroupName -eq (Get-CimInstance - Class WIN32_ComputerSystem).Workgroup)
326+
327+ return ($WorkGroupName -eq (Get-CimInstance - Class ' Win32_ComputerSystem' ).Workgroup)
302328 }
303329 else
304330 {
305331 # No Domain or Workgroup specified and computer name is correct
306- return $true ;
332+ return $true
307333 }
308334}
309335
310336function Assert-DomainOrWorkGroup ($DomainName , $WorkGroupName )
311337{
312338 if ($DomainName -and $WorkGroupName )
313339 {
314- throw " Only DomainName or WorkGroupName can be specified at once."
340+ throw ' Only DomainName or WorkGroupName can be specified at once.'
315341 }
316342}
317343
0 commit comments