Skip to content

Commit 152f20e

Browse files
Vasily LarionovVasily Larionov
authored andcommitted
Added helper function
Added unit test for helper function
1 parent 41f75e6 commit 152f20e

3 files changed

Lines changed: 334 additions & 295 deletions

File tree

DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1

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

270-
# Normalize path
271-
$pathArray = ($TaskPath -split '\\').Where( {$_})
272-
if ($pathArray.Count -gt 0)
273-
{
274-
$TaskPath = "\$($pathArray -join '\')\"
275-
}
270+
$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath
276271

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

@@ -828,6 +823,8 @@ function Set-TargetResource
828823
$RunOnlyIfNetworkAvailable = $false
829824
)
830825

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

@@ -1001,11 +998,11 @@ function Set-TargetResource
1001998

1002999
if ($currentValues.Ensure -eq 'Present')
10031000
{
1004-
Write-Verbose -Message ('Removing previous scheduled task' -f $TaskName)
1001+
Write-Verbose -Message ('Removing previous scheduled task {0}' -f $TaskName)
10051002
$null = Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$false
10061003
}
10071004

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

10101007
$scheduledTask = New-ScheduledTask -Action $action -Trigger $trigger -Settings $setting
10111008

@@ -1050,7 +1047,7 @@ function Set-TargetResource
10501047

10511048
if ($Ensure -eq 'Absent')
10521049
{
1053-
Write-Verbose -Message ('Removing scheduled task' -f $TaskName)
1050+
Write-Verbose -Message ('Removing scheduled task {0}' -f $TaskName)
10541051
Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$false
10551052
}
10561053
}
@@ -1306,12 +1303,7 @@ function Test-TargetResource
13061303
$RunOnlyIfNetworkAvailable = $false
13071304
)
13081305

1309-
# Normalize path
1310-
$pathArray = ($TaskPath -split '\\').Where( {$_})
1311-
if ($pathArray.Count -gt 0)
1312-
{
1313-
$TaskPath = "\$($pathArray -join '\')\"
1314-
}
1306+
$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath
13151307

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

@@ -1330,9 +1322,36 @@ function Test-TargetResource
13301322
return $false
13311323
}
13321324

1333-
$DesiredValues = $PSBoundParameters
1334-
$DesiredValues.TaskPath = $TaskPath
1325+
$desiredValues = $PSBoundParameters
1326+
$desiredValues.TaskPath = $TaskPath
13351327

13361328
Write-Verbose -Message 'Testing DSC parameter state'
1337-
return Test-DscParameterState -CurrentValues $CurrentValues -DesiredValues $DesiredValues
1329+
return Test-DscParameterState -CurrentValues $CurrentValues -DesiredValues $desiredValues
13381330
}
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
1357+
}

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ xVirtualMemory has the following properties:
120120

121121
### Unreleased
122122
- xScheduledTask
123-
- Fixed incorrect TaskPath handling (Fix #45)
123+
- Fixed incorrect TaskPath handling [Fix #45](https://github.com/PowerShell/xComputerManagement/issues/45)
124124

125125
### 2.0.0.0
126126
* Updated resources
@@ -129,8 +129,7 @@ xVirtualMemory has the following properties:
129129
### 1.10.0.0
130130
* Added resources
131131
- xVirtualMemory
132-
133-
>>>>>>> 09d3709ed7a50e2c4bcb965c871b18cfafc52454
132+
134133
### 1.9.0.0
135134
* Added resources
136135
- xPowerPlan

0 commit comments

Comments
 (0)