Private/ConvertTo-LocalObject.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<#
 
    .COPYRIGHT
    Copyright (c) ECIT Solutions AS. All rights reserved. Licensed under the MIT license.
    See https://github.com/ecitsolutions/Autotask/blob/master/LICENSE.md for license information.
 
#>


Function ConvertTo-LocalObject {
    <#
        .SYNOPSIS
            This function adjusts the timezone and converts picklist fields from their label to their index value.
        .DESCRIPTION
            This function adjusts the timezone and converts picklist fields from their label to their index value.
        .INPUTS
            [PSObject[]]
        .OUTPUTS
            [PSObject[]]
        .EXAMPLE
            $Element | ConvertFrom-LocalTimeAndLabels
            Updates the properties of object $Element with the values of any parameter with the same name as a property-
        .NOTES
            NAME: ConvertTo-LocalObject
       
    #>

    [cmdletbinding()]
    
    Param
    (
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true
        )]
        [PSObject[]]
        $InputObject
    )

    begin {
        # Enable modern -Debug behavior
        if ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) {
            $DebugPreference = 'Continue'
        }
    
        Write-Debug ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name)
        
        # Set up TimeZone offset handling and make sure the if statement will
        # default to Windows if platform information is not available
        $timezoneid = if ($IsMacOS -or $IsLinux) { 'America/New_York' }
        else { 'Eastern Standard Time' }
        $EST = [System.Timezoneinfo]::FindSystemTimeZoneById($timezoneid)
        $result = @()
    }

    process {

        # Get the entity name from input
        $entityName = $InputObject[0].GetType().Name       
        
        # Get updated field info about this entity
        $fields = Get-AtwsFieldInfo -Entity $entityName
    
        # Normalize dates, i.e. set them to CEST. The .Update() method of the API reads all datetime fields as CEST
        # We can safely ignore readonly fields, even if we have modified them previously. The API ignores them.
        $DateTimeParams = $fields.Where( { $_.Type -eq 'datetime' }).Name
    
        # Prepare picklists
        $Picklists = $fields.Where{ $_.IsPickList }
    
        # Loop through all objects and make adjustments
        foreach ($object in $InputObject) { 

            # Any userdefined fields?
            if ($object.UserDefinedFields.Count -gt 0) { 
                # Expand User defined fields for easy filtering of collections and readability
                foreach ($UDF in $object.UserDefinedFields) {
                    # Make names you HAVE TO escape...
                    $UDFName = '#{0}' -F $UDF.Name
                    Add-Member -InputObject $object -MemberType NoteProperty -Name $UDFName -Value $UDF.Value -Force
                }  
            }

            # Adjust TimeZone on all DateTime properties
            foreach ($DateTimeParam in $DateTimeParams) {
    
                # Get the datetime value
                $value = $object.$DateTimeParam
                
                # Skip if parameter is empty
                if (-not ($value)) {
                    Continue
                }
                # Convert the datetime to LocalTime unless it is a date
                If ($object.$DateTimeParam -ne $object.$DateTimeParam.Date) { 

                    # Convert the datetime from EST back to local time
                    $object.$dateTimeParam = [TimeZoneInfo]::ConvertTime($value, $EST, [TimeZoneInfo]::Local)
                }
            }
    
            if ($Script:Atws.Configuration.ConvertPicklistIdToLabel) { 
                # Restore picklist labels
                foreach ($field in $Picklists) {
                    if ($object.$($field.Name) -in $field.PicklistValues.Value) {
                        $object.$($field.Name) = ($field.PickListValues.Where{ $_.Value -eq $object.$($field.Name) }).Label
                    }
                }
            }

            Foreach ($field in $Picklists) {
                $fieldName = '{0}Label' -F $field.Name
                $value = ($field.PickListValues.Where{ $_.Value -eq $object.$($field.Name) }).Label
                Add-Member -InputObject $object -MemberType NoteProperty -Name $fieldName -Value $value -Force
            }
        }
        
        # If using pipeline the process block will run once per object in pipeline. Store them all
        $result += $InputObject
        
    }

    end {
        Write-Debug -Message ('{0}: End of function, returning {1} {2}(s)' -F $MyInvocation.MyCommand.Name, $result.count, $entityName)
        Return $result
    }
}