functions/clear-axclientcacheobjects.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
126
127
128
129
130
131
132

<#
    .SYNOPSIS
        Clear AX 2012 Client Cache Objects
         
    .DESCRIPTION
        Remove AX 2012 Client Cache Object files from the file system
         
    .PARAMETER ObjectType
        The type of cache object that you want to remove
         
        Valid options are:
        AUC
        KTI
        VSAssemblies
         
    .PARAMETER UserLocation
        Decide which user location that you want to work against
         
        Do you want to remove the cache objects from the current user or all users?
         
        Valid options are:
        CurrentUser
        AllUsers
         
    .PARAMETER ListOnly
        Instruct the cmdlet to only list the files that matches your selection from the other parameters
         
    .EXAMPLE
        PS C:\> Clear-AxClientCacheObjects -ObjectType "Auc" -UserLocation "CurrentUser" -ListOnly
         
        This will list all the Auc files under the current user location.
        It will work against the ObjectType "Auc".
        It will work againt the UserLocation "CurrentUser".
        It will only list the files and folders, it will NOT delete anything.
         
    .EXAMPLE
        PS C:\> Clear-AxClientCacheObjects -ObjectType "Auc" -UserLocation "CurrentUser"
         
        This will delete all the Auc files under the current user location.
        It will work against the ObjectType "Auc".
        It will work againt the UserLocation "CurrentUser".
         
        It WILL delete the files without further warning or notification!
         
    .EXAMPLE
        PS C:\> Clear-AxClientCacheObjects -ObjectType "Auc" -UserLocation "AllUsers"
         
        This will delete all the Auc files under all users locations.
        It will work against the ObjectType "Auc".
        It will work againt the UserLocation "AllUsers".
         
        It WILL delete the files without further warning or notification!
         
    .EXAMPLE
        PS C:\> Clear-AxClientCacheObjects -ObjectType "Auc","Kti","VSAssemblies" -UserLocation "CurrentUser"
         
        This will delete all the Auc,Kti and VSAssemblies files under the current user location.
        It will work against the ObjectType "Auc","Kti","VSAssemblies".
        It will work againt the UserLocation "CurrentUser".
         
        It WILL delete the files without further warning or notification!
         
    .NOTES
        Tags: Client Cache, Cache, KTI, AUC, VSAssemblies
         
        Author: Mötz Jensen (@Splaxi)
         
        All credits goes to Kenneth Madsen (@KennethGrupp) for providing detailed examples on how to achieve this the best way using powershell
         
#>


function Clear-AxClientCacheObjects {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")]
    [CmdletBinding()]
    param (
        [string[]] $ObjectType,

        [string] $UserLocation,

        [switch] $ListOnly
    )
    
    Invoke-TimeSignal -Start

    $basePath = ""

    switch ($UserLocation) {
        "CurrentUser" {
            Write-PSFMessage -Level Verbose -Message "CurrentUser path was selected as the location to clear."

            $basePath = $env:LocalAppData
        }
        "AllUsers" {
            Write-PSFMessage -Level Verbose -Message "AllUsers path was selected as the location to clear."

            $basePath = "$env:LocalAppData" -replace "$env:username", "*"
        }
    }

    $pathToClear = ""
    foreach ($item in $ObjectType) {
        switch ($item.ToUpper()) {
            "AUC" {
                Write-PSFMessage -Level Verbose -Message "Working against cache object type: `"AUC`""

                $pathToClear = Join-Path $basePath "*.auc"
            }
            "KTI" {
                Write-PSFMessage -Level Verbose -Message "Working against cache object type: `"KTI`""

                $pathToClear = Join-Path $basePath "*.kti"
            }
            "VSASSEMBLIES" {
                Write-PSFMessage -Level Verbose -Message "Working against cache object type: `"VSAssemblies`""

                $pathToClear = Join-Path $basePath "Microsoft\Dynamics Ax\VSAssemblies*\*"
            }
        }

        Write-PSFMessage -Level Verbose -Message "Working against path: $pathToClear" -Target $pathToClear
        
        if ($ListOnly) {
            Get-ChildItem -Path $pathToClear
        }
        else {
            Remove-Item -Path $pathToClear -Force
        }
    }

    Invoke-TimeSignal -End
}