functions/clear-axservercacheobjects.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

<#
    .SYNOPSIS
        Clear AX 2012 AOS Server Cache Objects
         
    .DESCRIPTION
        Remove AX 2012 AOS Server Cache Object files from the file system
         
    .PARAMETER ObjectType
        The type of cache object that you want to remove
         
        Valid options are:
        XppIL
        Label
        VSAssemblies
         
    .PARAMETER InstanceName
        Name of the instance that you are working against
         
        Default value can be configured with the Set-AxActiveAosConfig cmdlet
         
    .PARAMETER ListOnly
        Instruct the cmdlet to only list the files that matches your selection from the other parameters
         
    .EXAMPLE
        PS C:\> Clear-AxServerCacheObjects -ObjectType "XppIL" -InstanceName "AXTEST" -ListOnly
         
        This will list all the XppIL files under the AXTEST AOS Instance location.
        It will work against the ObjectType "XppIL".
        It will work againt the InstanceName "AXTEST".
        It will only list the files and folders, it will NOT delete anything.
         
    .EXAMPLE
        PS C:\> Clear-AxServerCacheObjects -ObjectType "XppIL" -InstanceName "AXTEST"
         
        This will delete all the XppIL files under the AXTEST AOS Instance location.
        It will work against the ObjectType "XppIL".
        It will work againt the InstanceName "AXTEST".
         
        It WILL delete the files without further warning or notification!
         
    .EXAMPLE
        PS C:\> Clear-AxServerCacheObjects -ObjectType "XppIL","Label","VSAssemblies" -InstanceName "AXTEST"
         
        This will delete all the XppIL,Label and VSAssemblies files under the AXTEST AOS Instance location.
        It will work against the ObjectType "XppIL","Label","VSAssemblies".
        It will work againt the InstanceName "AXTEST".
         
        It WILL delete the files without further warning or notification!
         
    .NOTES
        Tags: Client Cache, Cache, Label, XppIL, 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-AxServerCacheObjects {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")]
    [CmdletBinding()]
    param (
        [string[]] $ObjectType,

        [string] $InstanceName = $Script:ActiveAosInstancename,

        [switch] $ListOnly
    )
    
    if ([String]::IsNullOrEmpty($InstanceName)) {
        $messageString = "Instance name was <c='em'>empty</c>. You either need to supply it with <c='em'>-InstanceName</c> parameter or configure it using the <c='em'>Set-AxActiveAosConfig</c> cmdlet."
        Write-PSFMessage -Level Host -Message $messageString
        Stop-PSFFunction -Message "Stopping because of missing instance name parameter." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
        return
    }

    if ($InstanceName -eq "*") {
        $messageString = "Instance name cannot be <c='em'>*</c>. You either need to supply an instance name that will only result in <c='em'>one</c> AOS instance."
        Write-PSFMessage -Level Host -Message $messageString
        Stop-PSFFunction -Message "Stopping because of missing instance name parameter." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
        return
    }

    Invoke-TimeSignal -Start

    $basePath = (Get-AxAosInstance -InstanceName $InstanceName).BinDirectory

    $parms = @{}
    foreach ($item in $ObjectType) {
        switch ($item.ToUpper()) {
            "XPPIL" {
                Write-PSFMessage -Level Verbose -Message "Working against cache object type: `"XppIL`""

                $parms.Path = (Join-Path $basePath "XppIL")
                $parms.Include = "*.*"
            }
            "LABEL" {
                Write-PSFMessage -Level Verbose -Message "Working against cache object type: `"Label`""
                
                $parms.Path = (Join-Path $basePath "Application\appl\standard\ax*.*")
            }
            "VSASSEMBLIES" {
                Write-PSFMessage -Level Verbose -Message "Working against cache object type: `"VSAssemblies`""
                
                $parms.Path = (Join-Path $basePath "VSAssemblies\*.*")
            }
        }

        Write-PSFMessage -Level Verbose -Message "Working against path: $($parms.Path)" -Target $parms.Path
        
        if ($ListOnly) {
            Get-ChildItem @parms -Recurse
        }
        else {
            Remove-Item @parms -Recurse -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
        }
    }
    
    Invoke-TimeSignal -End
}