Public/Setup-ScheduledTask.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function Setup-ScheduledTask {
    [CmdletBinding(DefaultParameterSetName='DefinitionFile')]
    Param(
        [Parameter(ParameterSetName='DefinitionFile', Mandatory=$true)]
        [ValidateScript({Test-Path $_ -PathType Leaf})]
        [ValidateNotNullOrEmpty()]
        [string[]]$DefinitionFile
    ,
        [Parameter(ParameterSetName='DefinitionDirectory', Mandatory=$true)]
        [ValidateScript({Test-Path $_ -PathType Container})]
        [ValidateNotNullOrEmpty()]
        [string[]]$DefinitionDirectory
    ,
        [Parameter(ParameterSetName='DefinitionObject', Mandatory=$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 ($PSBoundParameters['DefinitionFile']) {
            $DefinitionFileCollection = Get-Item $PSBoundParameters['DefinitionFile']
        }elseif ($PSBoundParameters['DefinitionDirectory']) {
            $DefinitionFileCollection = if ($PSBoundParameters['AsJson']) {
                                            Get-ChildItem $PSBoundParameters['DefinitionDirectory'] -File | ? { $_.Extension -eq '.json' }
                                        }else {
                                            Get-ChildItem $PSBoundParameters['DefinitionDirectory'] -File | ? { $_.Extension -eq '.ps1' }
                                        }
        }
        if (!$PSBoundParameters['DefinitionObject']) {
            if (!$DefinitionFileCollection) {
                "No definitions could be found from the specified definition files or directories." | Write-Error
                return
            }
        }
        if (!$PSBoundParameters['DefinitionObject']) {
            $DefinitionCollectionRaw = $DefinitionFileCollection | % {
                if ($PSBoundParameters['AsJson']) {
                    Get-Content -Path $_.FullName | ConvertFrom-Json
                }else {
                    . $_.FullName
                }
            }
        }elseif ($PSBoundParameters['DefinitionObject']) {
            "DefinitionObject" | Write-Verbose
            $DefinitionCollectionRaw = $PSBoundParameters['DefinitionObject']
        }
        $DefinitionCollectionRaw | % {
            $definitionHashtable = if ($_.GetType() -ne [hashtable]) { $_ | ConvertTo-Hashtable } else { $_ }
            $definition = $definitionHashtable | Validate-DefinitionObject
            if ($definition) { $DefinitionsCollection.Add($definition) | Out-Null }
        }

        # Serialize definitions
        $DefinitionsCollectionSerialized = $DefinitionsCollection | % {
            "Serializing task definition:" | Write-Verbose
            $_ | Out-String | 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 | 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
    }
}