Skip to content

Commit e0c63cd

Browse files
author
Sergei Vorobev
committed
Update to version 1.2.2 from PSGallery
1 parent 8600be2 commit e0c63cd

3 files changed

Lines changed: 90 additions & 108 deletions

File tree

Lines changed: 82 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
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-
#######################################################################
56
function 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-
########################################################################
3837
function 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-
#######################################################################
196177
function 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

261245
Export-ModuleMember -Function *-TargetResource
246+
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
2-
[ClassVersion("1.0.0"), FriendlyName("xComputer")]
1+
[ClassVersion("1.0.1.0"), FriendlyName("xComputer")]
32
class MSFT_xComputer : OMI_BaseResource
43
{
54
[key] string Name;
6-
75
[write] string DomainName;
8-
96
[write,EmbeddedInstance("MSFT_Credential")] String Credential;
10-
117
[write,EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
12-
138
[write] string WorkGroupName;
149
};
15-

xComputerManagement.psd1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@{
22
# Version number of this module.
3-
ModuleVersion = '1.2'
3+
ModuleVersion = '1.2.2'
44

55
# ID used to uniquely identify this module
6-
GUID = '41c83857-b8c6-43a2-8568-1b155cddcb85'
6+
GUID = 'B5004952-489E-43EA-999C-F16A25355B89'
77

88
# Author of this module
99
Author = 'Microsoft Corporation'
@@ -12,10 +12,12 @@ Author = 'Microsoft Corporation'
1212
CompanyName = 'Microsoft Corporation'
1313

1414
# Copyright statement for this module
15-
Copyright = '(c) 2013 Microsoft Corporation. All rights reserved.'
15+
Copyright = '(c) 2014 Microsoft Corporation. All rights reserved.'
1616

1717
# Description of the functionality provided by this module
18-
Description = 'Module with DSC Resources for Computer Management area'
18+
Description = 'The xComputerManagement module is originally part of the Windows PowerShell Desired State Configuration (DSC) Resource Kit. This version has been modified for use in Azure. This module contains the xComputer and xDisk resources. These DSC Resources allow you to perform computer management tasks, like joining a domain or initializing disks.
19+
20+
All of the resources in the DSC Resource Kit are provided AS IS, and are not supported through any Microsoft standard support program or service.'
1921

2022
# Minimum version of the Windows PowerShell engine required by this module
2123
PowerShellVersion = '4.0'
@@ -29,3 +31,4 @@ FunctionsToExport = '*'
2931
# Cmdlets to export from this module
3032
CmdletsToExport = '*'
3133
}
34+

0 commit comments

Comments
 (0)