Private/New-IdoItCacheFile.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
Function New-IdoItCacheFile {

     <#
    .SYNOPSIS
    Creates a new Cache File in the users local app data directory

    .DESCRIPTION
    Creates a new Cache File in the users local app data directory

    .PARAMETER CacheData
    This parameter accepts a PSObject and will convert it into JSON and appends it into the cache file

    .PARAMETER CacheType
    CacheType defines wich type of cache to be written.

    .EXAMPLE
    PS> New-IdoItCacheFile -CacheType Constant -CacheData (Get-IdoItConstant)

    This will create a new cache for idoit constants

    .NOTES
    Version
    0.1.0 20.01.2018 CB initial release
    #>


    [CmdletBinding(
        SupportsShouldProcess = $True
    )]
    Param (
        [Parameter (
            Mandatory = $True,
            Position = 0
        )]
        [PSObject]$CacheData,

        [Parameter (
            Mandatory = $True
        )]
        [ValidateSet('Constant','Config')]
        [String]$CacheType
    )

    Begin {
            # $VerbosePreference = Continue
            $CachePath = $env:LOCALAPPDATA+"\.psidoit"
            #$CacheMetaDataFile = $CachePath + "\cachemetadata.json"
            $CacheFile = $CachePath + "\$CacheType.json"


            $CacheDataObject = [PSCustomObject]@{
                'CreationTime' = (Get-Date -Format o)
                'CacheType' = $CacheType
                'Data' = $CacheData
            }
    }
    Process {
    # More work needed :-)
        Try {


            If ( -Not (Test-Path $CachePath ) ) {

                New-Item -ItemType Directory -Path $CachePath

            }



            Write-Verbose "Creating idoit cache file"
            ConvertTo-Json -InputObject $CacheDataObject -Depth 4 | Out-File -FilePath ($CacheFile) -Encoding default -Force:$True

        }
        Catch [Exeption] {

            Throw $_

        }

    }

}




            #Create a cache meta file to store cache age and some other stuff into it
            #Write-Verbose "Checking if cache metadata file exists"
            #If ( Test-Path -Path $CacheMetaDataFile ) {

             # Write-Verbose "Found existing metadata file. Loading content"
             # $CacheMetaData = Get-Content $CacheMetaDataFile -Encoding Default -Raw | ConvertFrom-Json
             # $MaxCacheAge = New-TimeSpan -Days 1
             # $TimeSpan = New-TimeSpan -Start ([Datetime]::parseexact($CacheMetaData.Created, "o", $Null))

             # If ($TimeSpan -gt $MaxCacheAge) {

             # Write-Verbose "Cache MaxAge reached - forcing rebuild of the cache"
             # $ValidCache = $False

             # } Else {

             # Write-Verbose "Cache MaxAge not reached - skipping rebuild of cache"
             # $ValidCache = $True

             # }

            #}
            #ElseIf ( (-Not $ValidCache ) -or ( -Not (Test-Path -Path $CacheMetaDataFile) ) ) {

   # $CacheMetaData = @{
    # Created = (Get-Date -Format o)
     # }

      # ConvertTo-Json -InputObject $CacheMetaData -Depth 2 | Out-File -FilePath $CacheMetaDataFile -Encoding default -Force:$True
    #
     # }

      # If ( ( -not $ValidCache ) -or ( $ForceCacheRebuild ) -or (-Not (Test-Path -Path $CacheConstantFile)) ) {