Skip to content

Commit

Permalink
Merge pull request #38 from PowerShell/dev
Browse files Browse the repository at this point in the history
Merging release pull request
  • Loading branch information
kwirkykat authored Jun 29, 2016
2 parents 7e1b79d + 5a831d5 commit 80a88b6
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 11 deletions.
11 changes: 8 additions & 3 deletions DSCResources/MSFT_xComputer/MSFT_xComputer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ function Set-TargetResource
)

ValidateDomainOrWorkGroup -DomainName $DomainName -WorkGroupName $WorkGroupName

if ($Name -eq 'localhost')
{
$Name = $env:COMPUTERNAME
}

if ($Credential)
{
Expand Down Expand Up @@ -215,11 +220,11 @@ 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}
Write-Verbose -Message "Checking if computer name is correct"
if (($Name -ne 'localhost') -and ($Name -ne $env:COMPUTERNAME)) {return $false}

ValidateDomainOrWorkGroup -DomainName $DomainName -WorkGroupName $WorkGroupName

Expand Down
36 changes: 34 additions & 2 deletions DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function Get-TargetResource
[ValidateSet("Present","Absent")]
$Ensure = "Present",

[Parameter(Mandatory=$false)]
[System.Boolean]
$Enable,

[Parameter(Mandatory=$false)]
[System.Management.Automation.PSCredential]
$ExecuteAsCredential
Expand Down Expand Up @@ -128,6 +132,7 @@ function Get-TargetResource
ScheduleType = $returnScheduleType
RepeatInterval = $returnInveral
ExecuteAsCredential = $task.Principal.UserId
Enable = $task.Settings.Enabled
}
}
}
Expand Down Expand Up @@ -173,6 +178,10 @@ function Set-TargetResource
[ValidateSet("Present","Absent")]
$Ensure = "Present",

[Parameter(Mandatory=$false)]
[System.Boolean]
$Enable,

[Parameter(Mandatory=$false)]
[System.Management.Automation.PSCredential]
$ExecuteAsCredential
Expand All @@ -195,6 +204,15 @@ function Set-TargetResource
}
$action = New-ScheduledTaskAction @actionArgs

$settingArgs = @{}

if ($PSBoundParameters.ContainsKey("Enable"))
{
$settingArgs.Add("Disable", (-not $Enable))
}

$setting = New-ScheduledTaskSettingsSet @settingArgs

$date = (Get-Date).Date
$startTime = [DateTime]::Parse("$($date.ToShortDateString()) $StartTime")
switch ($ScheduleType)
Expand Down Expand Up @@ -228,7 +246,7 @@ function Set-TargetResource
{
Write-Verbose -Message "Creating new scheduled task `"$TaskName`""

$scheduledTask = New-ScheduledTask -Action $action -Trigger $trigger
$scheduledTask = New-ScheduledTask -Action $action -Trigger $trigger -Settings $setting
$registerArgs = @{
TaskName = $TaskName
TaskPath = $TaskPath
Expand All @@ -253,7 +271,8 @@ function Set-TargetResource
TaskName = $TaskName
TaskPath = $TaskPath
Action= $action
Trigger = $trigger
Trigger = $trigger
Settings = $setting
}
if ($PSBoundParameters.ContainsKey("ExecuteAsCredential") -eq $true)
{
Expand Down Expand Up @@ -317,6 +336,10 @@ function Test-TargetResource
[ValidateSet("Present","Absent")]
$Ensure = "Present",

[Parameter(Mandatory=$false)]
[System.Boolean]
$Enable,

[Parameter(Mandatory=$false)]
[System.Management.Automation.PSCredential]
$ExecuteAsCredential
Expand Down Expand Up @@ -370,6 +393,15 @@ function Test-TargetResource
return $false
}
}

if ($PSBoundParameters.ContainsKey("Enable") -eq $true)
{
if ($Enable -ne ($currentValues.Enable))
{
Write-Verbose -Message "Enable does not match desired state. Current value: $($currentValues.Enabled) - Desired Vale: $Enable"
return $false
}
}
}

return $true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ class MSFT_xScheduledTask : OMI_BaseResource
[Required, Description("How many units (minutes, hours, days) between each run of this task?")] Uint32 RepeatInterval;
[Write, Description("The time of day this task should start at - defaults to 12:00 AM")] string StartTime;
[Write, Description("Present if the task should exist, false if it should be removed"), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] string Ensure;
[Write, Description("True if the task should be enabled, false if it should be disabled")] boolean Enable;
[Write, Description("The credential this task should execute as. If not specified defaults to running as the local system account"), EmbeddedInstance("MSFT_Credential")] string ExecuteAsCredential;
};
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ This DSC Resource allows you to rename a computer and add it to a domain or work
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.
The ""x" in xComputerManagement stands for experimental, which means that these resources will be fix forward and monitored by the module owner(s).

Please leave comments, feature requests, and bug reports in the Q & A tab for this module.
Please leave comments, feature requests, and bug reports in the Issues tab for this module.

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

If you would like to modify xComputerManagement module, feel free.
When modifying, please update the module name, resource friendly name, and MOF class name (instructions below).
Expand Down Expand Up @@ -73,6 +76,10 @@ xScheduledTask has the following properties:

### Unreleased

### 1.7.0.0
*Added support for enabling or disabling scheduled tasks
* The Name parameter resolves to $env:COMPUTERNAME when the value is localhost

### 1.6.0.0
* Added the following resources:
* MSFT_xOfflineDomainJoin resource to join computers to an AD Domain using an Offline Domain Join request file.
Expand Down
32 changes: 32 additions & 0 deletions Tests/Integration/MSFT_xScheduledTask.Config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,35 @@ Configuration xScheduledTask_Remove
}
}
}

Configuration xScheduledTask_Enable
{
Import-DscResource -ModuleName xComputerManagement
node "localhost" {
xScheduledTask xScheduledTask_Remove {
TaskName = "Test task"
TaskPath = "\xComputerManagement\"
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
ScheduleType = "Minutes"
RepeatInterval = 15
Enable = $true
Ensure="Present"
}
}
}

Configuration xScheduledTask_Disable
{
Import-DscResource -ModuleName xComputerManagement
node "localhost" {
xScheduledTask xScheduledTask_Remove {
TaskName = "Test task"
TaskPath = "\xComputerManagement\"
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
ScheduleType = "Minutes"
RepeatInterval = 15
Enable = $false
Ensure="Present"
}
}
}
44 changes: 44 additions & 0 deletions Tests/Integration/MSFT_xScheduledTask.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,50 @@ try
}
}

Context "A scheduled task exists, and should be enabled" {
$CurrentConfig = "xScheduledTask_Enable"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
{
. $CurrentConfig -OutputPath $ConfigDir
} | Should Not Throw
}

It "should apply the MOF correctly" {
{
Start-DscConfiguration -Path $ConfigDir -Wait -Verbose -Force
} | Should Not Throw
}

It "should return a compliant state after being applied" {
(Test-DscConfiguration -ReferenceConfiguration $ConfigMof -Verbose).InDesiredState | Should be $true
}
}

Context "A scheduled task exists, and should be disabled" {
$CurrentConfig = "xScheduledTask_Disable"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
{
. $CurrentConfig -OutputPath $ConfigDir
} | Should Not Throw
}

It "should apply the MOF correctly" {
{
Start-DscConfiguration -Path $ConfigDir -Wait -Verbose -Force
} | Should Not Throw
}

It "should return a compliant state after being applied" {
(Test-DscConfiguration -ReferenceConfiguration $ConfigMof -Verbose).InDesiredState | Should be $true
}
}

AfterEach {
Remove-DscConfigurationDocument -Stage Current, Pending, Previous -Force -Confirm:$false -WarningAction SilentlyContinue
}
Expand Down
38 changes: 38 additions & 0 deletions Tests/Unit/MSFT_xComputer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ try
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $true
Test-TargetResource -Name 'localhost' -DomainName 'contoso.com' -Credential $Credential | Should Be $true
}
It 'Should return True if ComputerName and Workgroup is same as specified' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
Mock GetComputerDomain {''}
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true
Test-TargetResource -Name 'localhost' -WorkGroupName 'workgroup' | Should Be $true
}
It 'Should return True if ComputerName is same and no Domain or Workgroup specified' {
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
Mock GetComputerDomain {''}
Test-TargetResource -Name $Env:ComputerName | Should Be $true
Test-TargetResource -Name 'localhost' | Should Be $true
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Test-TargetResource -Name $Env:ComputerName | Should Be $true
Test-TargetResource -Name 'localhost' | Should Be $true
}
It 'Should return False if ComputerName is not same and no Domain or Workgroup specified' {
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
Expand All @@ -80,11 +84,13 @@ try
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Test-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential | Should Be $false
Test-TargetResource -Name 'localhost' -DomainName 'adventure-works.com' -Credential $Credential | Should Be $false
}
It 'Should return False if Workgroup name is not same as specified' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
Mock GetComputerDomain {''}
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'NOTworkgroup' | Should Be $false
Test-TargetResource -Name 'localhost' -WorkGroupName 'NOTworkgroup' | Should Be $false
}
It 'Should return False if ComputerName is not same as specified' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
Expand All @@ -98,18 +104,23 @@ try
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$false}}
Mock GetComputerDomain {''}
Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false
Test-TargetResource -Name 'localhost' -DomainName 'contoso.com' -Credential $Credential | Should Be $false
}
It 'Should return False if ComputerName is in Domain and Workgroup is specified' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -Credential $Credential -UnjoinCredential $Credential | Should Be $false
Test-TargetResource -Name 'localhost' -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
}
It 'Should not Throw if name is localhost' {
{Test-TargetResource -Name "localhost"} | Should Not Throw
}

}
Context "$($Global:DSCResourceName)\Get-TargetResource" {
Expand Down Expand Up @@ -198,6 +209,15 @@ try
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 when name is [localhost]' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Set-TargetResource -Name 'localhost' -DomainName 'adventure-works.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 the Domain to new Domain with specified OU' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Expand All @@ -207,6 +227,15 @@ try
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 when Name is [localhost]' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Set-TargetResource -Name 'localhost' -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 @@ -216,6 +245,15 @@ try
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName}
}
It 'Changes only Domain to Workgroup when Name is [localhost]' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {''}
Set-TargetResource -Name 'localhost' -WorkGroupName 'Contoso' -UnjoinCredential $Credential | Should BeNullOrEmpty
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName}
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName}
}
It 'Changes only ComputerName in Domain' {
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
Mock GetComputerDomain {'contoso.com'}
Expand Down
Loading

0 comments on commit 80a88b6

Please sign in to comment.