From 6a11761516ab24424c871c6607cdc9881b018d1b Mon Sep 17 00:00:00 2001 From: Vasily Larionov Date: Fri, 10 Feb 2017 18:33:56 +0300 Subject: [PATCH 1/8] Fixed TaskPath to include backslash --- .../MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 index d1289880..c57d1769 100644 --- a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 +++ b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 @@ -48,15 +48,18 @@ function Get-TargetResource [System.Management.Automation.PSCredential] $ExecuteAsCredential ) - $task = Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -ErrorAction SilentlyContinue + + $fixedTaskPath = "\$(($TaskPath -split '\\').Where({$_}) -join '\')\" + + $task = Get-ScheduledTask -TaskName $TaskName -TaskPath $fixedTaskPath -ErrorAction SilentlyContinue if ($null -eq $task) { return @{ TaskName = $TaskName - TaskPath = $TaskPath + TaskPath = $fixedTaskPath Ensure = "Absent" - TriggerType = "Unknown" + ScheduleType = "Unknown" } } else @@ -129,7 +132,7 @@ function Get-TargetResource return @{ TaskName = $TaskName - TaskPath = $TaskPath + TaskPath = $task.TaskPath Ensure = "Present" ActionExecutable = $action.Execute ActionArguments = $action.Arguments From 995a164a67a07e9d6086e7d143ddcca2dbfedbe8 Mon Sep 17 00:00:00 2001 From: Vasily Larionov Date: Fri, 10 Feb 2017 19:16:24 +0300 Subject: [PATCH 2/8] Fixed root path Fixed tests to rely on TaskPath --- .../MSFT_xScheduledTask.psm1 | 14 +++- Tests/Unit/MSFT_xScheduledTask.Tests.ps1 | 77 +++++++++++-------- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 index c57d1769..d65af36f 100644 --- a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 +++ b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 @@ -49,10 +49,18 @@ function Get-TargetResource $ExecuteAsCredential ) - $fixedTaskPath = "\$(($TaskPath -split '\\').Where({$_}) -join '\')\" - - $task = Get-ScheduledTask -TaskName $TaskName -TaskPath $fixedTaskPath -ErrorAction SilentlyContinue + if($TaskPath -eq '\') + { + $fixedTaskPath = '\' + } + else + { + $fixedTaskPath = "\$(($TaskPath -split '\\').Where({$_}) -join '\')\" + + } + $task = Get-ScheduledTask -TaskName $TaskName -TaskPath $fixedTaskPath -ErrorAction SilentlyContinue + if ($null -eq $task) { return @{ diff --git a/Tests/Unit/MSFT_xScheduledTask.Tests.ps1 b/Tests/Unit/MSFT_xScheduledTask.Tests.ps1 index 40594eb2..b52bd214 100644 --- a/Tests/Unit/MSFT_xScheduledTask.Tests.ps1 +++ b/Tests/Unit/MSFT_xScheduledTask.Tests.ps1 @@ -36,6 +36,7 @@ try Context "No scheduled task exists, but it should" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 @@ -59,6 +60,7 @@ try Context "A scheduled task exists, but it shouldn't" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 @@ -66,8 +68,8 @@ try } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable }) @@ -99,6 +101,7 @@ try Context "A scheduled task doesnt exist, and it shouldn't" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 @@ -119,14 +122,15 @@ try Context "A scheduled task with minutes based repetition exists, but has the wrong settings" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable }) @@ -158,14 +162,15 @@ try Context "A scheduled task with minutes based repetition exists and has the correct settings" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable }) @@ -192,14 +197,15 @@ try Context "A scheduled task with hourly based repetition exists, but has the wrong settings" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Hourly" RepeatInterval = 4 } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable }) @@ -231,14 +237,15 @@ try Context "A scheduled task with hourly based repetition exists and has the correct settings" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Hourly" RepeatInterval = 4 } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable }) @@ -265,14 +272,15 @@ try Context "A scheduled task with daily based repetition exists, but has the wrong settings" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Daily" RepeatInterval = 3 } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable }) @@ -304,14 +312,15 @@ try Context "A scheduled task with daily based repetition exists and has the correct settings" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Daily" RepeatInterval = 3 } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable }) @@ -338,6 +347,7 @@ try Context "A scheduled task exists and is configured with the wrong execution account" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 @@ -345,8 +355,8 @@ try } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable }) @@ -378,6 +388,7 @@ try Context "A scheduled task exists and is configured with the wrong working directory" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ActionWorkingPath = "C:\Example" ScheduleType = "Minutes" @@ -385,8 +396,8 @@ try } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable WorkingDirectory = "C:\Wrong" @@ -419,6 +430,7 @@ try Context "A scheduled task exists and is configured with the wrong executable arguments" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ActionArguments = "-File `"C:\something\right.ps1`"" ScheduleType = "Minutes" @@ -426,8 +438,8 @@ try } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable Arguments = "-File `"C:\something\wrong.ps1`"" @@ -460,6 +472,7 @@ try Context "A scheduled task is enabled and should be disabled" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 @@ -467,8 +480,8 @@ try } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable Arguments = $testParams.Arguments @@ -505,6 +518,7 @@ try Context "A scheduled task is enabled and has the correct settings" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 @@ -512,8 +526,8 @@ try } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable Arguments = $testParams.Arguments @@ -544,6 +558,7 @@ try Context "A scheduled task is disabled and has the correct settings" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 @@ -551,8 +566,8 @@ try } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable Arguments = $testParams.Arguments @@ -583,6 +598,7 @@ try Context "A scheduled task is disabled but should be enabled" { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 @@ -590,8 +606,8 @@ try } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable Arguments = $testParams.Arguments @@ -627,14 +643,15 @@ try Context "A Scheduled task exists, is disabled, and the optional parameter enable is not specified" -Fixture { $testParams = @{ TaskName = "Test task" + TaskPath = '\Test\' ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" ScheduleType = "Minutes" RepeatInterval = 15 } Mock Get-ScheduledTask { return @{ - Name = $testParams.TaskName - Path = $testParams.TaskPath + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath Actions = @(@{ Execute = $testParams.ActionExecutable Arguments = $testParams.Arguments From c8d6dcc7774aa48314f57d77a1d385e2c83e4016 Mon Sep 17 00:00:00 2001 From: Vasily Larionov Date: Fri, 10 Feb 2017 19:22:46 +0300 Subject: [PATCH 3/8] Update readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e0028faa..bcb9f50d 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,8 @@ xPowerPlan resource has following properties: ## Versions ### Unreleased - +- xScheduledTask + - Fixed incorrect TaskPath handling (Fix #45) ### 1.9.0.0 * Added resources - xPowerPlan From c6615356f25035d1c488f47f08f3859cd385a85b Mon Sep 17 00:00:00 2001 From: Vasily Larionov Date: Fri, 10 Feb 2017 19:47:24 +0300 Subject: [PATCH 4/8] TaskPath: Fixed empty string --- DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 index d65af36f..774fdae2 100644 --- a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 +++ b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 @@ -49,7 +49,7 @@ function Get-TargetResource $ExecuteAsCredential ) - if($TaskPath -eq '\') + if(($TaskPath -eq '\') -or ($TaskPath.Length -eq 0)) { $fixedTaskPath = '\' } From c245747ff7f34b306b8f922b8f4830cf82233589 Mon Sep 17 00:00:00 2001 From: Vasily Larionov Date: Fri, 10 Feb 2017 22:02:28 +0300 Subject: [PATCH 5/8] Fixed MSFT_xComputer\Get-TargetResource test Preserve order of reference and result arrays --- Tests/Unit/MSFT_xComputer.Tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/Unit/MSFT_xComputer.Tests.ps1 b/Tests/Unit/MSFT_xComputer.Tests.ps1 index b3258249..c8f90dd9 100644 --- a/Tests/Unit/MSFT_xComputer.Tests.ps1 +++ b/Tests/Unit/MSFT_xComputer.Tests.ps1 @@ -130,7 +130,9 @@ try 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', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName') + $referenceKeys = @('Name', 'DomainName', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName')|Sort-Object + $resultKeys = $Result.Keys|Sort-Object + $resultKeys | Should Be $referenceKeys } It 'Throws if name is to long' { {Get-TargetResource -Name "ThisNameIsTooLong"} | Should Throw From d15aed60038966c0fb05af2915ea5f8341a24742 Mon Sep 17 00:00:00 2001 From: Vasily Larionov Date: Thu, 16 Feb 2017 01:13:09 +0300 Subject: [PATCH 6/8] Fixed Test-TargetResource --- .../MSFT_xScheduledTask.psm1 | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 index 774fdae2..798b5990 100644 --- a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 +++ b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 @@ -51,21 +51,21 @@ function Get-TargetResource if(($TaskPath -eq '\') -or ($TaskPath.Length -eq 0)) { - $fixedTaskPath = '\' + $realTaskPath = '\' } else { - $fixedTaskPath = "\$(($TaskPath -split '\\').Where({$_}) -join '\')\" + $realTaskPath = "\$(($TaskPath -split '\\').Where({$_}) -join '\')\" } - $task = Get-ScheduledTask -TaskName $TaskName -TaskPath $fixedTaskPath -ErrorAction SilentlyContinue + $task = Get-ScheduledTask -TaskName $TaskName -TaskPath $realTaskPath -ErrorAction SilentlyContinue if ($null -eq $task) { return @{ TaskName = $TaskName - TaskPath = $fixedTaskPath + TaskPath = $realTaskPath Ensure = "Absent" ScheduleType = "Unknown" } @@ -368,7 +368,17 @@ function Test-TargetResource } if ($Ensure -eq "Present") { - if ($TaskPath -ne $currentValues.TaskPath) + if(($TaskPath -eq '\') -or ($TaskPath.Length -eq 0)) + { + $realTaskPath = '\' + } + else + { + $realTaskPath = "\$(($TaskPath -split '\\').Where({$_}) -join '\')\" + + } + + if ($realTaskPath -ne $currentValues.TaskPath) { Write-Verbose -Message "TaskPath does not match desired state. Current value: $($currentValues.TaskPath) - Desired Value: $TaskPath" return $false From 152f20ea203cd0d2737c6a417b42cfe002e9b97a Mon Sep 17 00:00:00 2001 From: Vasily Larionov Date: Mon, 17 Jul 2017 16:42:41 +0300 Subject: [PATCH 7/8] Added helper function Added unit test for helper function --- .../MSFT_xScheduledTask.psm1 | 55 +- README.md | 5 +- Tests/Unit/MSFT_xScheduledTask.Tests.ps1 | 569 +++++++++--------- 3 files changed, 334 insertions(+), 295 deletions(-) diff --git a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 index 58beebd3..a3389455 100644 --- a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 +++ b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 @@ -267,12 +267,7 @@ function Get-TargetResource $RunOnlyIfNetworkAvailable = $false ) - # Normalize path - $pathArray = ($TaskPath -split '\\').Where( {$_}) - if ($pathArray.Count -gt 0) - { - $TaskPath = "\$($pathArray -join '\')\" - } + $TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath Write-Verbose -Message ('Retrieving existing task ({0} in {1})' -f $TaskName, $TaskPath) @@ -828,6 +823,8 @@ function Set-TargetResource $RunOnlyIfNetworkAvailable = $false ) + $TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath + Write-Verbose -Message ('Entering Set-TargetResource for {0} in {1}' -f $TaskName, $TaskPath) $currentValues = Get-TargetResource @PSBoundParameters @@ -1001,11 +998,11 @@ function Set-TargetResource if ($currentValues.Ensure -eq 'Present') { - Write-Verbose -Message ('Removing previous scheduled task' -f $TaskName) + Write-Verbose -Message ('Removing previous scheduled task {0}' -f $TaskName) $null = Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$false } - Write-Verbose -Message ('Creating new scheduled task' -f $TaskName) + Write-Verbose -Message ('Creating new scheduled task {0}' -f $TaskName) $scheduledTask = New-ScheduledTask -Action $action -Trigger $trigger -Settings $setting @@ -1050,7 +1047,7 @@ function Set-TargetResource if ($Ensure -eq 'Absent') { - Write-Verbose -Message ('Removing scheduled task' -f $TaskName) + Write-Verbose -Message ('Removing scheduled task {0}' -f $TaskName) Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$false } } @@ -1306,12 +1303,7 @@ function Test-TargetResource $RunOnlyIfNetworkAvailable = $false ) - # Normalize path - $pathArray = ($TaskPath -split '\\').Where( {$_}) - if ($pathArray.Count -gt 0) - { - $TaskPath = "\$($pathArray -join '\')\" - } + $TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath Write-Verbose -Message ('Testing scheduled task {0}' -f $TaskName) @@ -1330,9 +1322,36 @@ function Test-TargetResource return $false } - $DesiredValues = $PSBoundParameters - $DesiredValues.TaskPath = $TaskPath + $desiredValues = $PSBoundParameters + $desiredValues.TaskPath = $TaskPath Write-Verbose -Message 'Testing DSC parameter state' - return Test-DscParameterState -CurrentValues $CurrentValues -DesiredValues $DesiredValues + return Test-DscParameterState -CurrentValues $CurrentValues -DesiredValues $desiredValues } + +<# +.SYNOPSIS +Helper function to convert TaskPath to the right form + +.PARAMETER TaskPath +The path to the task +#> + +function ConvertTo-NormalizedTaskPath +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [System.String] + $TaskPath + ) + + $pathArray = $TaskPath.Split('\').Where( {$_}) + if ($pathArray.Count -gt 0) + { + $TaskPath = "\$($pathArray -join '\')\" + } + + return $TaskPath +} \ No newline at end of file diff --git a/README.md b/README.md index 97a26704..9cf96632 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ xVirtualMemory has the following properties: ### Unreleased - xScheduledTask - - Fixed incorrect TaskPath handling (Fix #45) + - Fixed incorrect TaskPath handling [Fix #45](https://github.com/PowerShell/xComputerManagement/issues/45) ### 2.0.0.0 * Updated resources @@ -129,8 +129,7 @@ xVirtualMemory has the following properties: ### 1.10.0.0 * Added resources - xVirtualMemory - ->>>>>>> 09d3709ed7a50e2c4bcb965c871b18cfafc52454 + ### 1.9.0.0 * Added resources - xPowerPlan diff --git a/Tests/Unit/MSFT_xScheduledTask.Tests.ps1 b/Tests/Unit/MSFT_xScheduledTask.Tests.ps1 index b73a6131..c2a735eb 100644 --- a/Tests/Unit/MSFT_xScheduledTask.Tests.ps1 +++ b/Tests/Unit/MSFT_xScheduledTask.Tests.ps1 @@ -73,24 +73,24 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalMinutes)M" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalMinutes)M" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' - } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -132,29 +132,29 @@ try TaskPath = '\Test\' ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe' ScheduleType = 'Once' - RepeatInterval =[datetime]::Today + (New-TimeSpan -Minutes 15) + RepeatInterval = [datetime]::Today + (New-TimeSpan -Minutes 15) RepetitionDuration = [datetime]::Today + (New-TimeSpan -Minutes 150) } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - }) - Triggers = @(@{ - Repetition = @{ - Duration = $null - Interval = "PT$(($testParams.RepeatInterval.TimeOfDay.TotalMinutes) + 1)M" + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + }) + Triggers = @(@{ + Repetition = @{ + Duration = $null + Interval = "PT$(($testParams.RepeatInterval.TimeOfDay.TotalMinutes) + 1)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' - } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -182,24 +182,24 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalMinutes)M" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalMinutes)M" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' - } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -221,24 +221,24 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$(($testParams.RepetitionDuration.TimeOfDay.TotalHours))H" - Interval = "PT$(($testParams.RepeatInterval.TimeOfDay.TotalHours) + 1)H" - } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$(($testParams.RepetitionDuration.TimeOfDay.TotalHours))H" + Interval = "PT$(($testParams.RepeatInterval.TimeOfDay.TotalHours) + 1)H" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -266,24 +266,24 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalHours)H" - } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalHours)H" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -304,24 +304,24 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - }) - Triggers = @(@{ - Repetition = @{ - Duration = $null - Interval = "P$(($testParams.DaysInterval) + 1)D" - } - CimClass = @{ - CimClassName = 'MSFT_TaskDailyTrigger' + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + }) + Triggers = @(@{ + Repetition = @{ + Duration = $null + Interval = "P$(($testParams.DaysInterval) + 1)D" + } + CimClass = @{ + CimClassName = 'MSFT_TaskDailyTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -348,21 +348,21 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - }) - Triggers = @(@{ - DaysInterval = $testParams.DaysInterval - CimClass = @{ - CimClassName = 'MSFT_TaskDailyTrigger' + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + }) + Triggers = @(@{ + DaysInterval = $testParams.DaysInterval + CimClass = @{ + CimClassName = 'MSFT_TaskDailyTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -385,24 +385,24 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" - } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Principal = @{ + UserId = 'WrongUser' } - }) - Principal = @{ - UserId = 'WrongUser' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -431,25 +431,25 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - WorkingDirectory = 'C:\Wrong' - }) - Triggers = @(@{ - Repetition = @{ - Duration = $null - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" - } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + WorkingDirectory = 'C:\Wrong' + }) + Triggers = @(@{ + Repetition = @{ + Duration = $null + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -478,25 +478,25 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - Arguments = '-File "C:\something\wrong.ps1"' - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + Arguments = '-File "C:\something\wrong.ps1"' + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Principal = @{ + UserId = 'SYSTEM' } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' - } - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -525,27 +525,27 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - Arguments = $testParams.Arguments - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + Arguments = $testParams.Arguments + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Settings = @(@{ + Enabled = $true + }) + Principal = @{ + UserId = 'SYSTEM' } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' - } - }) - Settings = @(@{ - Enabled = $true - }) - Principal = @{ - UserId = 'SYSTEM' - } } } It 'should return present from the get method' { @@ -576,28 +576,28 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - Arguments = $testParams.Arguments - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" - } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + Arguments = $testParams.Arguments + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Settings = @(@{ + Enabled = $true + }) + Principal = @{ + UserId = 'SYSTEM' } - }) - Settings = @(@{ - Enabled = $true - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -620,28 +620,28 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - Arguments = $testParams.Arguments - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + Arguments = $testParams.Arguments + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Settings = @(@{ + Enabled = $false + }) + Principal = @{ + UserId = 'SYSTEM' } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' - } - }) - Settings = @(@{ - Enabled = $false - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -664,28 +664,28 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - Arguments = $testParams.Arguments - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + Arguments = $testParams.Arguments + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Settings = @(@{ + Enabled = $false + }) + Principal = @{ + UserId = 'SYSTEM' } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' - } - }) - Settings = @(@{ - Enabled = $false - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -713,28 +713,28 @@ try } Mock Get-ScheduledTask { return @{ - TaskName = $testParams.TaskName - TaskPath = $testParams.TaskPath - Actions = @(@{ - Execute = $testParams.ActionExecutable - Arguments = $testParams.Arguments - }) - Triggers = @(@{ - Repetition = @{ - Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" - Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" - } - CimClass = @{ - CimClassName = 'MSFT_TaskTimeTrigger' + TaskName = $testParams.TaskName + TaskPath = $testParams.TaskPath + Actions = @(@{ + Execute = $testParams.ActionExecutable + Arguments = $testParams.Arguments + }) + Triggers = @(@{ + Repetition = @{ + Duration = "PT$($testParams.RepetitionDuration.TimeOfDay.TotalHours)H" + Interval = "PT$($testParams.RepeatInterval.TimeOfDay.TotalMinutes)M" + } + CimClass = @{ + CimClassName = 'MSFT_TaskTimeTrigger' + } + }) + Settings = @(@{ + Enabled = $false + }) + Principal = @{ + UserId = 'SYSTEM' } - }) - Settings = @(@{ - Enabled = $false - }) - Principal = @{ - UserId = 'SYSTEM' - } - } } + } } It 'should return present from the get method' { (Get-TargetResource @testParams).Ensure | Should Be 'Present' @@ -745,6 +745,27 @@ try } } + Context 'A scheduled task path is root or custom' -Fixture { + It 'should return backslash' { + ConvertTo-NormalizedTaskPath -TaskPath '\'| Should Be '\' + } + + It 'should add backslash at the end' { + ConvertTo-NormalizedTaskPath -TaskPath '\Test'| Should Be '\Test\' + } + + It 'should add backslash at the beginning' { + ConvertTo-NormalizedTaskPath -TaskPath 'Test\'| Should Be '\Test\' + } + + It 'should add backslash at the beginning and at the end' { + ConvertTo-NormalizedTaskPath -TaskPath 'Test'| Should Be '\Test\' + } + + It 'should not add backslash' { + ConvertTo-NormalizedTaskPath -TaskPath '\Test\'| Should Be '\Test\' + } + } } } #endregion From 59ad4a4b2ca4abbe6c5dcf29cb4986cbc8c8d249 Mon Sep 17 00:00:00 2001 From: Vasily Larionov Date: Mon, 17 Jul 2017 17:01:42 +0300 Subject: [PATCH 8/8] Add new line --- DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 index a3389455..7b2e75cb 100644 --- a/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 +++ b/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1 @@ -1354,4 +1354,4 @@ function ConvertTo-NormalizedTaskPath } return $TaskPath -} \ No newline at end of file +}