Skip to content

Commit

Permalink
Merge pull request #62 from vlariono/Issue#45
Browse files Browse the repository at this point in the history
xScheduledTask: Incorrect TaskPath handling (Fixes #45)
  • Loading branch information
PlagueHO authored Jul 17, 2017
2 parents e54626d + 59ad4a4 commit 8a4cca3
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 283 deletions.
53 changes: 45 additions & 8 deletions DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,12 @@ function Get-TargetResource
$RunOnlyIfNetworkAvailable = $false
)

$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath

Write-Verbose -Message ('Retrieving existing task ({0} in {1})' -f $TaskName, $TaskPath)

$task = Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -ErrorAction SilentlyContinue

if ($null -eq $task)
{
Write-Verbose -Message ('No task found. returning empty task {0} with Ensure = "Absent"' -f $Taskname)
Expand Down Expand Up @@ -527,8 +530,8 @@ function Get-TargetResource
}

return @{
TaskName = $TaskName
TaskPath = $TaskPath
TaskName = $task.TaskName
TaskPath = $task.TaskPath
StartTime = $startAt
Ensure = 'Present'
Description = $task.Description
Expand Down Expand Up @@ -820,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

Expand Down Expand Up @@ -974,7 +979,7 @@ function Set-TargetResource
{
if ($RepetitionDuration.TimeOfDay -le $RepeatInterval.TimeOfDay)
{
$exceptionMessage ='Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay, $RepetitionDuration.TimeOfDay
$exceptionMessage = 'Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay, $RepetitionDuration.TimeOfDay
New-InvalidArgumentException -Message $exceptionMessage -ArgumentName RepetitionDuration
}

Expand All @@ -993,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

Expand Down Expand Up @@ -1042,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
}
}
Expand Down Expand Up @@ -1297,6 +1302,8 @@ function Test-TargetResource
[System.Boolean]
$RunOnlyIfNetworkAvailable = $false
)

$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath

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

Expand All @@ -1315,6 +1322,36 @@ function Test-TargetResource
return $false
}

$desiredValues = $PSBoundParameters
$desiredValues.TaskPath = $TaskPath

Write-Verbose -Message 'Testing DSC parameter state'
return Test-DscParameterState -CurrentValues $CurrentValues -DesiredValues $PSBoundParameters
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
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ xVirtualMemory has the following properties:
## Versions

### Unreleased
* xScheduledTask
- Fixed incorrect TaskPath handling [Fix #45](https://github.com/PowerShell/xComputerManagement/issues/45)

* xComputer: Changed comparision that validates if we are in the correct AD Domain to work correctly if FQDN wasn't used

### 2.0.0.0
Expand All @@ -128,7 +131,7 @@ xVirtualMemory has the following properties:
### 1.10.0.0
* Added resources
- xVirtualMemory

### 1.9.0.0
* Added resources
- xPowerPlan
Expand Down
Loading

0 comments on commit 8a4cca3

Please sign in to comment.