Skip to content

Commit

Permalink
Feature (function): Add ValueFromPipeline support for `Setup-Schedu…
Browse files Browse the repository at this point in the history
…ledTask` `DefinitionObject` parameter
  • Loading branch information
joeltimothyoh committed Aug 30, 2022
1 parent 0981ffd commit 564d2d5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 52 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
118 changes: 68 additions & 50 deletions src/ScheduledTaskManagement/Public/Setup-ScheduledTask.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,89 @@ function Setup-ScheduledTask {
[ValidateNotNullOrEmpty()]
[string[]]$DefinitionDirectory
,
[Parameter(ParameterSetName='DefinitionObject', Mandatory=$true)]
[Parameter(ParameterSetName='DefinitionObject', Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[object[]]$DefinitionObject
,
[Parameter(ParameterSetName='DefinitionFile', Mandatory=$false)]
[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
}
}
11 changes: 10 additions & 1 deletion test/scripts/integration/Run-IntegrationTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -53,6 +57,11 @@ $cmdArgs = @{
}
& $functionTestScriptBlock

$cmd = {
. "$PSScriptRoot\..\..\definitions\scheduledtasks\tasks.ps1" | Setup-ScheduledTask
}
$cmdArgs = $null
& $functionTestScriptBlock

###########
# Results #
Expand Down

0 comments on commit 564d2d5

Please sign in to comment.