ObjectHandling/Invoke-NavContainerCodeunit.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
<#
 .Synopsis
  Invoke codeunit in Nav Container
 .Description
  Invoke a codeunit in a NAV Container.
  Create and remove a temp. windows user if running NavUserPassword
 .Parameter containerName
  Name of the container in which you want to invoke a codeunit
 .Parameter tenant
  Name of the tenant in which context you want to invoke a codeunit
 .Parameter CompanyName
  Name of the Company in which context you want to invoke a codeunit
 .Parameter Codeunitid
  Id of the codeunit you want to invoke
 .Parameter MethodName
  Name of the method you want to invoke (default is OnRun)
 .Parameter Argument
  Arguments for the method (default empty)
 .Example
  Invoke-NavContainerCodeunit -containerName test -codeunitId 50000
 .Example
  Invoke-NavContainerCodeunit -containerName test -codeunitId 50000 -methodName "doit" -argument "Hello"
#>

function Invoke-NavContainerCodeunit {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$containerName, 
        [Parameter(Mandatory=$false)]
        [string]$tenant = "default",
        [Parameter(Mandatory=$false)]
        [string]$CompanyName,
        [Parameter(Mandatory=$true)]
        [int]$Codeunitid,
        [Parameter(Mandatory=$false)]
        [string]$MethodName,
        [Parameter(Mandatory=$false)]
        [string]$Argument
    )

    $session = Get-NavContainerSession -containerName $containerName -silent
    Invoke-Command -Session $session -ScriptBlock { Param($tenant, $CompanyName, $Codeunitid, $MethodName, $Argument)
    
        $me = whoami
        $userexist = Get-NAVServerUser -ServerInstance NAV -Tenant $tenant | Where-Object username -eq $me
        if (!($userexist)) {
            New-NAVServerUser -ServerInstance NAV -Tenant $tenant -WindowsAccount $me
            New-NAVServerUserPermissionSet -ServerInstance NAV -Tenant $tenant -WindowsAccount $me -PermissionSetId SUPER
        }

        $Params = @{}
        if ($CompanyName) {
            $Params += @{ "CompanyName" = $CompanyName }
        }
        if ($MethodName) {
            $Params += @{ "MethodName" = $MethodName }
        }
        if ($Argument) {
            $Params += @{ "Argument" = $Argument }
        }
    
        Invoke-NAVCodeunit -ServerInstance NAV -Tenant $tenant @Params -CodeunitId $CodeUnitId
    
        if (!($userexist)) {
            Remove-NAVServerUser -ServerInstance NAV -Tenant $tenant -WindowsAccount $me -Force
        }

    } -ArgumentList $tenant, $CompanyName, $Codeunitid, $MethodName, $Argument
}
Export-ModuleMember -Function Invoke-NavContainerCodeunit