Skip to content

Commit 564d2d5

Browse files
committed
Feature (function): Add ValueFromPipeline support for Setup-ScheduledTask DefinitionObject parameter
1 parent 0981ffd commit 564d2d5

3 files changed

Lines changed: 82 additions & 52 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ Setup-ScheduledTask -DefinitionDirectory "C:\path\to\definition\directory\"
8080
# Via directory containing .json definition files
8181
Setup-ScheduledTask -DefinitionDirectory "C:\path\to\definition\directory\" -AsJson
8282
83-
# Via definition objects
83+
# Via definition object(s)
8484
$tasks = . "C:\path\to\definition.ps1"
85+
## Via parameter
8586
Setup-ScheduledTask -DefinitionObject $tasks
87+
## Via pipeline
88+
$tasks | Setup-ScheduledTask
8689
```
8790

8891
To list all available functions of the module:

src/ScheduledTaskManagement/Public/Setup-ScheduledTask.ps1

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,71 +11,89 @@ function Setup-ScheduledTask {
1111
[ValidateNotNullOrEmpty()]
1212
[string[]]$DefinitionDirectory
1313
,
14-
[Parameter(ParameterSetName='DefinitionObject', Mandatory=$true)]
14+
[Parameter(ParameterSetName='DefinitionObject', Mandatory=$true, ValueFromPipeline=$true)]
1515
[ValidateNotNullOrEmpty()]
1616
[object[]]$DefinitionObject
1717
,
1818
[Parameter(ParameterSetName='DefinitionFile', Mandatory=$false)]
1919
[Parameter(ParameterSetName='DefinitionDirectory', Mandatory=$false)]
2020
[switch]$AsJson
2121
)
22-
try {
23-
# Import definitions as an array of hashtable definitions
24-
$DefinitionsCollection = New-Object System.Collections.ArrayList
25-
if ($DefinitionFile) {
26-
$DefinitionFileCollection = Get-Item $DefinitionFile
27-
}elseif ($DefinitionDirectory) {
28-
$DefinitionFileCollection = if ($AsJson) {
29-
Get-ChildItem $DefinitionDirectory -File | ? { $_.Extension -eq '.json' }
30-
}else {
31-
Get-ChildItem $DefinitionDirectory -File | ? { $_.Extension -eq '.ps1' }
32-
}
33-
}
34-
if (!$DefinitionObject) {
35-
if (!$DefinitionFileCollection) {
36-
"No definitions could be found from the specified definition files or directories." | Write-Error
37-
return
22+
begin {
23+
try {
24+
# Import definitions as an array of hashtable definitions (begin)
25+
$DefinitionsCollection = New-Object System.Collections.ArrayList
26+
if ($DefinitionFile) {
27+
$DefinitionFileCollection = Get-Item $DefinitionFile
28+
}elseif ($DefinitionDirectory) {
29+
$DefinitionFileCollection = if ($AsJson) {
30+
Get-ChildItem $DefinitionDirectory -File | ? { $_.Extension -eq '.json' }
31+
}else {
32+
Get-ChildItem $DefinitionDirectory -File | ? { $_.Extension -eq '.ps1' }
33+
}
3834
}
39-
}
40-
if (!$DefinitionObject) {
41-
$DefinitionCollectionRaw = $DefinitionFileCollection | % {
42-
if ($AsJson) {
43-
Get-Content $_.FullName | ConvertFrom-Json
44-
}else {
45-
. $_.FullName
35+
if ($DefinitionFile -or $DefinitionDirectory) {
36+
if (!$DefinitionFileCollection) {
37+
"No definitions could be found from the specified definition files or directories." | Write-Error
38+
return
39+
}
40+
$DefinitionCollectionRaw = $DefinitionFileCollection | % {
41+
if ($AsJson) {
42+
Get-Content $_.FullName | ConvertFrom-Json
43+
}else {
44+
. $_.FullName
45+
}
4646
}
47+
}elseif ($DefinitionObject) { # $DefinitionObject being non-null in the begin block indicates the value was not passed via pipeline
48+
$isDefinitionObjectValueFromPipeline = $false
49+
}else {
50+
$isDefinitionObjectValueFromPipeline = $true
4751
}
48-
}elseif ($DefinitionObject) {
49-
$DefinitionCollectionRaw = $DefinitionObject
50-
}
51-
$DefinitionCollectionRaw | % {
52-
$definitionHashtable = if ($_.GetType() -ne [hashtable]) { $_ | ConvertTo-Hashtable } else { $_ }
53-
$definition = $definitionHashtable | Validate-DefinitionObject
54-
if ($definition) { $DefinitionsCollection.Add($definition) | Out-Null }
52+
}catch {
53+
Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject
5554
}
55+
}process {
56+
try {
57+
# Import definitions as an array of hashtable definitions (process)
58+
if ($DefinitionObject) {
59+
$DefinitionCollectionRaw = $DefinitionObject # Store the array of objects or present object of $DefinitionObject processed within the pipeline
60+
}
61+
$DefinitionCollectionRaw | % {
62+
$definitionHashtable = if ($_.GetType() -ne [hashtable]) { $_ | ConvertTo-Hashtable } else { $_ }
63+
$definition = $definitionHashtable | Validate-DefinitionObject
64+
if ($definition) {
65+
if ($DefinitionObject) {
66+
if ($isDefinitionObjectValueFromPipeline) {
67+
$DefinitionsCollection.Clear() # Clear the $DefinitionsCollection arraylist to have the variable store only the present object of $DefinitionObject processed within the pipeline
68+
}
69+
}
70+
$DefinitionsCollection.Add($definition) | Out-Null
71+
}
72+
}
5673

57-
# Serialize definitions
58-
$DefinitionsCollectionSerialized = $DefinitionsCollection | % {
59-
"Serializing task definition:" | Write-Verbose
60-
$_ | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose
61-
try {
62-
Serialize-DefinitionObject -DefinitionObject $_
63-
}catch {
64-
Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject
74+
# Serialize definitions
75+
$DefinitionsCollectionSerialized = $DefinitionsCollection | % {
76+
"Serializing task definition:" | Write-Verbose
77+
$_ | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose
78+
try {
79+
Serialize-DefinitionObject -DefinitionObject $_
80+
}catch {
81+
Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject
82+
}
6583
}
66-
}
6784

68-
# Setup scheduled tasks
69-
$DefinitionsCollectionSerialized | % {
70-
"Setting up task:" | Write-Verbose
71-
$_ | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose
72-
try {
73-
Apply-ScheduledTask -DefinitionObject $_
74-
}catch {
75-
Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject
85+
# Setup scheduled tasks
86+
$DefinitionsCollectionSerialized | % {
87+
"Setting up task:" | Write-Verbose
88+
$_ | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose
89+
try {
90+
Apply-ScheduledTask -DefinitionObject $_
91+
}catch {
92+
Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject
93+
}
7694
}
95+
}catch {
96+
Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject
7797
}
78-
}catch {
79-
Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject
8098
}
8199
}

test/scripts/integration/Run-IntegrationTests.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ $functionTestScriptBlock = {
1313
$script:cmdArgs | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose
1414
for ($i=0; $i -le $iterations-1; $i++) {
1515
"Iteration: $($i+1)" | Write-Host
16-
$stdout = & $script:cmd @script:cmdArgs
16+
if ($script:cmdArgs) {
17+
$stdout = & $script:cmd @script:cmdArgs
18+
}else {
19+
$stdout = & $script:cmd
20+
}
1721
$stdout | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Host
1822
}
1923
}catch {
@@ -53,6 +57,11 @@ $cmdArgs = @{
5357
}
5458
& $functionTestScriptBlock
5559

60+
$cmd = {
61+
. "$PSScriptRoot\..\..\definitions\scheduledtasks\tasks.ps1" | Setup-ScheduledTask
62+
}
63+
$cmdArgs = $null
64+
& $functionTestScriptBlock
5665

5766
###########
5867
# Results #

0 commit comments

Comments
 (0)