Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5f99cf2
Merge pull request #1 from dcrreynolds/dev
dcrreynolds May 1, 2015
09df05d
Create nuget package
KarolKaczmarek Aug 12, 2015
8f47d6a
Moving build number to the third section of version
KarolKaczmarek Aug 25, 2015
c8320ff
Added the ability to specify an OU upon joining to the Domain
mprahl Oct 20, 2015
e1442c9
Updated the readme
mprahl Oct 20, 2015
e961a1a
Updated test
mprahl Oct 20, 2015
0b39053
Fix file encoding
mprahl Oct 20, 2015
91923b1
Small fix on the test
mprahl Oct 20, 2015
ec2135e
Changed function name to Verb-Noun
mprahl Oct 20, 2015
3fa99a6
Small spacing fix in the tests
mprahl Oct 20, 2015
ce84cfa
Added a few tests for Set
mprahl Oct 20, 2015
8785de7
Another add to the Set tests
mprahl Oct 20, 2015
fd98a90
Merge remote-tracking branch 'refs/remotes/PowerShell/dev'
dcrreynolds Nov 11, 2015
90580c5
Enhancement #15: Name validation added.
dcrreynolds Nov 14, 2015
aa47e4d
Replacing before_deploy with deploy_script
KarolKaczmarek Dec 8, 2015
7fe0e4a
Temporary workaround for pester issue causing build to get stuck
KarolKaczmarek Dec 16, 2015
d715779
Merge pull request #13 from PrahlM93/JoinOuProperty
Jan 5, 2016
ae137bb
Merge branch 'master' of https://github.com/dcrreynolds/xComputerMana…
Jan 5, 2016
1728bdb
Merge branch 'dcrreynolds-master' into dev
Jan 5, 2016
cab43e8
Reverting temporary workaround for pester issue causing build to get …
KarolKaczmarek Jan 7, 2016
4404ddf
Update README.md
KarolKaczmarek Feb 2, 2016
c9f7bd8
Update xComputerManagement.psd1
KarolKaczmarek Feb 2, 2016
a7b8c0a
Update README.md
KarolKaczmarek Feb 2, 2016
d8153dc
Releasing version 1.4.0.0
KarolKaczmarek Feb 2, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions DSCResources/MSFT_xComputer/MSFT_xComputer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ function Get-TargetResource
param
(
[parameter(Mandatory)]
[ValidateLength(1,15)]
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
[string] $Name,

[string] $DomainName,

[string] $JoinOU,

[PSCredential] $Credential,

[PSCredential] $UnjoinCredential,
Expand All @@ -26,6 +30,8 @@ function Get-TargetResource
$returnValue = @{
Name = $env:COMPUTERNAME
DomainName = GetComputerDomain
JoinOU = $JoinOU
CurrentOU = Get-ComputerOU
Credential = [ciminstance]$convertToCimCredential
UnjoinCredential = [ciminstance]$convertToCimUnjoinCredential
WorkGroupName= (gwmi WIN32_ComputerSystem).WorkGroup
Expand All @@ -39,9 +45,13 @@ function Set-TargetResource
param
(
[parameter(Mandatory)]
[ValidateLength(1,15)]
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
[string] $Name,

[string] $DomainName,

[string] $JoinOU,

[PSCredential] $Credential,

Expand Down Expand Up @@ -73,7 +83,12 @@ function Set-TargetResource
}
else
{
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force
if ($JoinOU) {
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -OUPath $JoinOU -Force
}
else {
Add-Computer -DomainName $DomainName -Credential $Credential -NewName $Name -Force
}
}
Write-Verbose -Message "Renamed computer to '$($Name)' and added to the domain '$($DomainName)."
}
Expand All @@ -86,7 +101,12 @@ function Set-TargetResource
}
else
{
Add-Computer -DomainName $DomainName -Credential $Credential -Force
if ($JoinOU) {
Add-Computer -DomainName $DomainName -Credential $Credential -OUPath $JoinOU -Force
}
else {
Add-Computer -DomainName $DomainName -Credential $Credential -Force
}
}
Write-Verbose -Message "Added computer to domain '$($DomainName)."
}
Expand Down Expand Up @@ -181,7 +201,11 @@ function Test-TargetResource
param
(
[parameter(Mandatory)]
[ValidateLength(1,15)]
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
[string] $Name,

[string] $JoinOU,

[PSCredential]$Credential,

Expand All @@ -191,6 +215,8 @@ function Test-TargetResource

[string] $WorkGroupName
)

Write-Verbose -Message "Validate desired Name is a valid name"

Write-Verbose -Message "Checking if computer name is $Name"
if ($Name -ne $env:COMPUTERNAME) {return $false}
Expand Down Expand Up @@ -247,5 +273,18 @@ function GetComputerDomain
}
}

Export-ModuleMember -Function *-TargetResource
function Get-ComputerOU
{
$ou = $null

if (GetComputerDomain)
{
$dn = $null
$dn = ([adsisearcher]"(&(objectCategory=computer)(objectClass=computer)(cn=$env:COMPUTERNAME))").FindOne().Properties.distinguishedname
$ou = $dn -replace '^(CN=.*?(?<=,))', ''
}

return $ou
}

Export-ModuleMember -Function *-TargetResource
20 changes: 11 additions & 9 deletions DSCResources/MSFT_xComputer/MSFT_xComputer.schema.mof
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[ClassVersion("1.0.1.0"), FriendlyName("xComputer")]
class MSFT_xComputer : OMI_BaseResource
{
[key] string Name;
[write] string DomainName;
[write,EmbeddedInstance("MSFT_Credential")] String Credential;
[write,EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
[write] string WorkGroupName;
};
[ClassVersion("1.0.1.0"), FriendlyName("xComputer")]
class MSFT_xComputer : OMI_BaseResource
{
[key] string Name;
[write] string DomainName;
[write] string JoinOU;
[read] string CurrentOU;
[write,EmbeddedInstance("MSFT_Credential")] String Credential;
[write,EmbeddedInstance("MSFT_Credential")] String UnjoinCredential;
[write] string WorkGroupName;
};
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ xComputer resource has following properties:

* Name: The desired computer name
* DomainName: The name of the domain to join
* JoinOU: The distinguished name of the organizational unit that the computer account will be created in
* WorkGroupName: The name of the workgroup
* Credential: Credential to be used to join or leave domain
* CurrentOU: A read-only property that specifies the organizational unit that the computer account is currently in

## Versions

### Unreleased

### 1.4.0.0
* Adding Name parameter validation

### 1.3.0

Expand Down
47 changes: 45 additions & 2 deletions Tests/xComputermanagement.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,28 @@ InModuleScope MSFT_xComputer {
Mock GetComputerDomain {'contoso.com'}
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -Credential $Credential -UnjoinCredential $Credential | Should Be $false
}
It 'Throws if name is to long' {
{Test-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
}
It 'Throws if name contains illigal characters' {
{Test-TargetResource -Name "ThisIsBad<>"} | Should Throw
}

}
Context Get-TargetResource {
It 'should not throw' {
{Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw
}
It 'Should return a hashtable containing Name,DomainName, Credential, UnjoinCredential and WorkGroupName' {
It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' {
$Result = Get-TargetResource -Name $env:COMPUTERNAME
$Result.GetType().Fullname | Should Be 'System.Collections.Hashtable'
$Result.Keys | Should Be @('Name','DomainName','Credential','UnjoinCredential','WorkGroupName')
$Result.Keys | Should Be @('Name', 'DomainName', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName')
}
It 'Throws if name is to long' {
{Get-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
}
It 'Throws if name contains illigal characters' {
{Get-TargetResource -Name "ThisIsBad<>"} | Should Throw
}
}
Context Set-TargetResource {
Expand All @@ -115,6 +127,14 @@ InModuleScope MSFT_xComputer {
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
}
It 'Changes ComputerName and changes Domain to new Domain with specified OU' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
}
It 'Changes ComputerName and changes Domain to Workgroup' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Expand All @@ -131,6 +151,14 @@ InModuleScope MSFT_xComputer {
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
}
It 'Changes ComputerName and changes Workgroup to Domain with specified OU' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
Mock GetComputerDomain {''}
Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential | Should BeNullOrEmpty
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
}
It 'Changes ComputerName and changes Workgroup to new Workgroup' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
Mock GetComputerDomain {''}
Expand All @@ -148,6 +176,15 @@ InModuleScope MSFT_xComputer {
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
}
It 'Changes only the Domain to new Domain with specified OU' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
}
It 'Changes only Domain to Workgroup' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {''}
Expand All @@ -171,6 +208,12 @@ InModuleScope MSFT_xComputer {
Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
}
It 'Throws if name is to long' {
{Set-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
}
It 'Throws if name contains illigal characters' {
{Set-TargetResource -Name "ThisIsBad<>"} | Should Throw
}
}
}
}
63 changes: 51 additions & 12 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#---------------------------------#
# environment configuration #
#---------------------------------#
version: 1.4.{build}.0
install:
- cinst -y pester
- git clone https://github.com/PowerShell/DscResource.Tests
- cinst -y pester
- git clone https://github.com/PowerShell/DscResource.Tests
- ps: Push-Location
- cd DscResource.Tests
- ps: Import-Module .\TestHelper.psm1 -force
- ps: Pop-Location

#---------------------------------#
# build configuration #
#---------------------------------#

build: false

#---------------------------------#
# test configuration #
#---------------------------------#

test_script:
- ps: |
$testResultsFile = ".\TestsResults.xml"
Expand All @@ -12,14 +28,37 @@ test_script:
if ($res.FailedCount -gt 0) {
throw "$($res.FailedCount) tests failed."
}
on_finish:
- ps: |
$stagingDirectory = (Resolve-Path ..).Path
$zipFile = Join-Path $stagingDirectory "$(Split-Path $pwd -Leaf).zip"
Add-Type -assemblyname System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($pwd, $zipFile)
@(
# You can add other artifacts here
(ls $zipFile)
) | % { Push-AppveyorArtifact $_.FullName }

#---------------------------------#
# deployment configuration #
#---------------------------------#

# scripts to run before deployment
deploy_script:
- ps: |
# Creating project artifact
$stagingDirectory = (Resolve-Path ..).Path
$manifest = Join-Path $pwd "xComputerManagement.psd1"
(Get-Content $manifest -Raw).Replace("1.4.0.0", $env:APPVEYOR_BUILD_VERSION) | Out-File $manifest
$zipFilePath = Join-Path $stagingDirectory "$(Split-Path $pwd -Leaf).zip"
Add-Type -assemblyname System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($pwd, $zipFilePath)

# Creating NuGet package artifact
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 .
nuget pack ".\$($env:APPVEYOR_PROJECT_NAME).nuspec" -outputdirectory .
$nuGetPackageName = $env:APPVEYOR_PROJECT_NAME + "." + $env:APPVEYOR_BUILD_VERSION + ".nupkg"
$nuGetPackagePath = (Get-ChildItem $nuGetPackageName).FullName

@(
# You can add other artifacts here
$zipFilePath,
$nuGetPackagePath
) | % {
Write-Host "Pushing package $_ as Appveyor artifact"
Push-AppveyorArtifact $_
}




26 changes: 25 additions & 1 deletion xComputerManagement.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
# Version number of this module.
ModuleVersion = '1.3.0'
ModuleVersion = '1.4.0.0'

# ID used to uniquely identify this module
GUID = 'B5004952-489E-43EA-999C-F16A25355B89'
Expand Down Expand Up @@ -30,5 +30,29 @@ FunctionsToExport = '*'

# Cmdlets to export from this module
CmdletsToExport = '*'

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
Tags = @('DesiredStateConfiguration', 'DSC', 'DSCResourceKit', 'DSCResource')

# A URL to the license for this module.
LicenseUri = 'https://github.com/PowerShell/xComputerManagement/blob/master/LICENSE'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/PowerShell/xComputerManagement'

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

} # End of PSData hashtable

} # End of PrivateData hashtable
}