Private/Serialize-DefinitionObject.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
function Serialize-DefinitionObject {
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [hashtable]$DefinitionObject
    )
    begin {
    }process {
        try {
            $SerializedObject = $DefinitionObject.Clone()
            # Serialize proerties with appropriate object values for use by scheduledtasks cmdlets
            if ($DefinitionObject['Trigger']) {
                $SerializedObject['Trigger'] = @(
                    $DefinitionObject['Trigger'] | % {
                        $trigger = $_
                        $triggerTemp = @{}
                        $trigger.GetEnumerator() | % {
                            if ($_.Key -eq 'At') {
                                $d = $_.Value
                                $triggerTemp[$_.Key] = Get-Date @d
                            }elseif ($_.Key -eq 'RepetitionDuration') {
                                $t = $_.Value
                                $triggerTemp[$_.Key] = New-Timespan @t
                            }elseif ($_.Key -eq 'RepetitionInterval') {
                                $t = $_.Value
                                $triggerTemp[$_.Key] = New-Timespan @t
                            }else {
                                $triggerTemp[$_.Key] = $_.Value
                            }
                        }
                        $triggerTemp
                    }
                )
           }
        }catch {
            throw
        }
        $SerializedObject
    }
}