AppMgt/Clean-NCHCustomNAVApps.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
function Clean-NCHCustomNAVApps {
    <#
    .SYNOPSIS
    Removes all non-Microsoft Apps from a Container
     
    .DESCRIPTION
    Long description
     
    .PARAMETER ContainerName
    The Container
    #>

    param(
        [Parameter(Mandatory = $true)]
        [String] $ContainerName
    )

    Write-Host -ForegroundColor Green "$($MyInvocation.MyCommand.Name) on $env:COMPUTERNAME"

    # $Session = Get-NavContainerSession -containerName $ContainerName
    # Invoke-Command -Session $Session -ScriptBlock {
    Invoke-ScriptInNavContainer -ContainerName $ContainerName -scriptblock {

        $SC = Get-NAVServerInstance

        $Apps = Get-NAVAppInfo -ServerInstance $SC.ServerInstance | Where Publisher -ne 'Microsoft'

        foreach ($App in $Apps) {
            $App | Uninstall-NAVApp -DoNotSaveData
            $App | Sync-NAVApp -ServerInstance $SC.ServerInstance -Mode Clean -force
            $App | UnPublish-NAVApp            
            Sync-NAVTenant -ServerInstance NAV -Tenant Default -Mode ForceSync -force    
            
            Write-Host " Removed $($App.Name) from $($App.Publisher)" -ForegroundColor Gray
        }       
    }  

}