Skip to content

Commit 8a4cca3

Browse files
authored
Merge pull request #62 from vlariono/Issue#45
xScheduledTask: Incorrect TaskPath handling (Fixes #45)
2 parents e54626d + 59ad4a4 commit 8a4cca3

3 files changed

Lines changed: 361 additions & 283 deletions

File tree

DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,12 @@ function Get-TargetResource
267267
$RunOnlyIfNetworkAvailable = $false
268268
)
269269

270+
$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath
271+
270272
Write-Verbose -Message ('Retrieving existing task ({0} in {1})' -f $TaskName, $TaskPath)
273+
271274
$task = Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -ErrorAction SilentlyContinue
272-
275+
273276
if ($null -eq $task)
274277
{
275278
Write-Verbose -Message ('No task found. returning empty task {0} with Ensure = "Absent"' -f $Taskname)
@@ -527,8 +530,8 @@ function Get-TargetResource
527530
}
528531

529532
return @{
530-
TaskName = $TaskName
531-
TaskPath = $TaskPath
533+
TaskName = $task.TaskName
534+
TaskPath = $task.TaskPath
532535
StartTime = $startAt
533536
Ensure = 'Present'
534537
Description = $task.Description
@@ -820,6 +823,8 @@ function Set-TargetResource
820823
$RunOnlyIfNetworkAvailable = $false
821824
)
822825

826+
$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath
827+
823828
Write-Verbose -Message ('Entering Set-TargetResource for {0} in {1}' -f $TaskName, $TaskPath)
824829
$currentValues = Get-TargetResource @PSBoundParameters
825830

@@ -974,7 +979,7 @@ function Set-TargetResource
974979
{
975980
if ($RepetitionDuration.TimeOfDay -le $RepeatInterval.TimeOfDay)
976981
{
977-
$exceptionMessage ='Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay, $RepetitionDuration.TimeOfDay
982+
$exceptionMessage = 'Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay, $RepetitionDuration.TimeOfDay
978983
New-InvalidArgumentException -Message $exceptionMessage -ArgumentName RepetitionDuration
979984
}
980985

@@ -993,11 +998,11 @@ function Set-TargetResource
993998

994999
if ($currentValues.Ensure -eq 'Present')
9951000
{
996-
Write-Verbose -Message ('Removing previous scheduled task' -f $TaskName)
1001+
Write-Verbose -Message ('Removing previous scheduled task {0}' -f $TaskName)
9971002
$null = Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$false
9981003
}
9991004

1000-
Write-Verbose -Message ('Creating new scheduled task' -f $TaskName)
1005+
Write-Verbose -Message ('Creating new scheduled task {0}' -f $TaskName)
10011006

10021007
$scheduledTask = New-ScheduledTask -Action $action -Trigger $trigger -Settings $setting
10031008

@@ -1042,7 +1047,7 @@ function Set-TargetResource
10421047

10431048
if ($Ensure -eq 'Absent')
10441049
{
1045-
Write-Verbose -Message ('Removing scheduled task' -f $TaskName)
1050+
Write-Verbose -Message ('Removing scheduled task {0}' -f $TaskName)
10461051
Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$false
10471052
}
10481053
}
@@ -1297,6 +1302,8 @@ function Test-TargetResource
12971302
[System.Boolean]
12981303
$RunOnlyIfNetworkAvailable = $false
12991304
)
1305+
1306+
$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath
13001307

13011308
Write-Verbose -Message ('Testing scheduled task {0}' -f $TaskName)
13021309

@@ -1315,6 +1322,36 @@ function Test-TargetResource
13151322
return $false
13161323
}
13171324

1325+
$desiredValues = $PSBoundParameters
1326+
$desiredValues.TaskPath = $TaskPath
1327+
13181328
Write-Verbose -Message 'Testing DSC parameter state'
1319-
return Test-DscParameterState -CurrentValues $CurrentValues -DesiredValues $PSBoundParameters
1329+
return Test-DscParameterState -CurrentValues $CurrentValues -DesiredValues $desiredValues
1330+
}
1331+
1332+
<#
1333+
.SYNOPSIS
1334+
Helper function to convert TaskPath to the right form
1335+
1336+
.PARAMETER TaskPath
1337+
The path to the task
1338+
#>
1339+
1340+
function ConvertTo-NormalizedTaskPath
1341+
{
1342+
[CmdletBinding()]
1343+
param
1344+
(
1345+
[Parameter(Mandatory = $true)]
1346+
[System.String]
1347+
$TaskPath
1348+
)
1349+
1350+
$pathArray = $TaskPath.Split('\').Where( {$_})
1351+
if ($pathArray.Count -gt 0)
1352+
{
1353+
$TaskPath = "\$($pathArray -join '\')\"
1354+
}
1355+
1356+
return $TaskPath
13201357
}

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ xVirtualMemory has the following properties:
119119
## Versions
120120

121121
### Unreleased
122+
* xScheduledTask
123+
- Fixed incorrect TaskPath handling [Fix #45](https://github.com/PowerShell/xComputerManagement/issues/45)
124+
122125
* xComputer: Changed comparision that validates if we are in the correct AD Domain to work correctly if FQDN wasn't used
123126

124127
### 2.0.0.0
@@ -128,7 +131,7 @@ xVirtualMemory has the following properties:
128131
### 1.10.0.0
129132
* Added resources
130133
- xVirtualMemory
131-
134+
132135
### 1.9.0.0
133136
* Added resources
134137
- xPowerPlan

0 commit comments

Comments
 (0)