diff --git a/README.md b/README.md index 505cc3f..e1beea0 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,12 @@ Setup-ScheduledTask -DefinitionDirectory "C:\path\to\definition\directory\" # Via directory containing .json definition files Setup-ScheduledTask -DefinitionDirectory "C:\path\to\definition\directory\" -AsJson -# Via definition objects +# Via definition object(s) $tasks = . "C:\path\to\definition.ps1" +## Via parameter Setup-ScheduledTask -DefinitionObject $tasks +## Via pipeline +$tasks | Setup-ScheduledTask ``` To list all available functions of the module: diff --git a/src/ScheduledTaskManagement/Public/Setup-ScheduledTask.ps1 b/src/ScheduledTaskManagement/Public/Setup-ScheduledTask.ps1 index ae705dc..b08ee95 100644 --- a/src/ScheduledTaskManagement/Public/Setup-ScheduledTask.ps1 +++ b/src/ScheduledTaskManagement/Public/Setup-ScheduledTask.ps1 @@ -11,7 +11,7 @@ function Setup-ScheduledTask { [ValidateNotNullOrEmpty()] [string[]]$DefinitionDirectory , - [Parameter(ParameterSetName='DefinitionObject', Mandatory=$true)] + [Parameter(ParameterSetName='DefinitionObject', Mandatory=$true, ValueFromPipeline=$true)] [ValidateNotNullOrEmpty()] [object[]]$DefinitionObject , @@ -19,63 +19,81 @@ function Setup-ScheduledTask { [Parameter(ParameterSetName='DefinitionDirectory', Mandatory=$false)] [switch]$AsJson ) - try { - # Import definitions as an array of hashtable definitions - $DefinitionsCollection = New-Object System.Collections.ArrayList - if ($DefinitionFile) { - $DefinitionFileCollection = Get-Item $DefinitionFile - }elseif ($DefinitionDirectory) { - $DefinitionFileCollection = if ($AsJson) { - Get-ChildItem $DefinitionDirectory -File | ? { $_.Extension -eq '.json' } - }else { - Get-ChildItem $DefinitionDirectory -File | ? { $_.Extension -eq '.ps1' } - } - } - if (!$DefinitionObject) { - if (!$DefinitionFileCollection) { - "No definitions could be found from the specified definition files or directories." | Write-Error - return + begin { + try { + # Import definitions as an array of hashtable definitions (begin) + $DefinitionsCollection = New-Object System.Collections.ArrayList + if ($DefinitionFile) { + $DefinitionFileCollection = Get-Item $DefinitionFile + }elseif ($DefinitionDirectory) { + $DefinitionFileCollection = if ($AsJson) { + Get-ChildItem $DefinitionDirectory -File | ? { $_.Extension -eq '.json' } + }else { + Get-ChildItem $DefinitionDirectory -File | ? { $_.Extension -eq '.ps1' } + } } - } - if (!$DefinitionObject) { - $DefinitionCollectionRaw = $DefinitionFileCollection | % { - if ($AsJson) { - Get-Content $_.FullName | ConvertFrom-Json - }else { - . $_.FullName + if ($DefinitionFile -or $DefinitionDirectory) { + if (!$DefinitionFileCollection) { + "No definitions could be found from the specified definition files or directories." | Write-Error + return + } + $DefinitionCollectionRaw = $DefinitionFileCollection | % { + if ($AsJson) { + Get-Content $_.FullName | ConvertFrom-Json + }else { + . $_.FullName + } } + }elseif ($DefinitionObject) { # $DefinitionObject being non-null in the begin block indicates the value was not passed via pipeline + $isDefinitionObjectValueFromPipeline = $false + }else { + $isDefinitionObjectValueFromPipeline = $true } - }elseif ($DefinitionObject) { - $DefinitionCollectionRaw = $DefinitionObject - } - $DefinitionCollectionRaw | % { - $definitionHashtable = if ($_.GetType() -ne [hashtable]) { $_ | ConvertTo-Hashtable } else { $_ } - $definition = $definitionHashtable | Validate-DefinitionObject - if ($definition) { $DefinitionsCollection.Add($definition) | Out-Null } + }catch { + Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject } + }process { + try { + # Import definitions as an array of hashtable definitions (process) + if ($DefinitionObject) { + $DefinitionCollectionRaw = $DefinitionObject # Store the array of objects or present object of $DefinitionObject processed within the pipeline + } + $DefinitionCollectionRaw | % { + $definitionHashtable = if ($_.GetType() -ne [hashtable]) { $_ | ConvertTo-Hashtable } else { $_ } + $definition = $definitionHashtable | Validate-DefinitionObject + if ($definition) { + if ($DefinitionObject) { + if ($isDefinitionObjectValueFromPipeline) { + $DefinitionsCollection.Clear() # Clear the $DefinitionsCollection arraylist to have the variable store only the present object of $DefinitionObject processed within the pipeline + } + } + $DefinitionsCollection.Add($definition) | Out-Null + } + } - # Serialize definitions - $DefinitionsCollectionSerialized = $DefinitionsCollection | % { - "Serializing task definition:" | Write-Verbose - $_ | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose - try { - Serialize-DefinitionObject -DefinitionObject $_ - }catch { - Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject + # Serialize definitions + $DefinitionsCollectionSerialized = $DefinitionsCollection | % { + "Serializing task definition:" | Write-Verbose + $_ | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose + try { + Serialize-DefinitionObject -DefinitionObject $_ + }catch { + Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject + } } - } - # Setup scheduled tasks - $DefinitionsCollectionSerialized | % { - "Setting up task:" | Write-Verbose - $_ | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose - try { - Apply-ScheduledTask -DefinitionObject $_ - }catch { - Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject + # Setup scheduled tasks + $DefinitionsCollectionSerialized | % { + "Setting up task:" | Write-Verbose + $_ | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose + try { + Apply-ScheduledTask -DefinitionObject $_ + }catch { + Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject + } } + }catch { + Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject } - }catch { - Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject } } diff --git a/test/scripts/integration/Run-IntegrationTests.ps1 b/test/scripts/integration/Run-IntegrationTests.ps1 index 0595533..90a4e3c 100644 --- a/test/scripts/integration/Run-IntegrationTests.ps1 +++ b/test/scripts/integration/Run-IntegrationTests.ps1 @@ -13,7 +13,11 @@ $functionTestScriptBlock = { $script:cmdArgs | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Verbose for ($i=0; $i -le $iterations-1; $i++) { "Iteration: $($i+1)" | Write-Host - $stdout = & $script:cmd @script:cmdArgs + if ($script:cmdArgs) { + $stdout = & $script:cmd @script:cmdArgs + }else { + $stdout = & $script:cmd + } $stdout | Out-String -Stream | % { $_.Trim() } | ? { $_ } | Write-Host } }catch { @@ -53,6 +57,11 @@ $cmdArgs = @{ } & $functionTestScriptBlock +$cmd = { + . "$PSScriptRoot\..\..\definitions\scheduledtasks\tasks.ps1" | Setup-ScheduledTask +} +$cmdArgs = $null +& $functionTestScriptBlock ########### # Results #