You might remember these blogposts:
- Start Dynamics NAV Windows Client (RTC) from PowerShell
- Start Dynamics NAV Development Client (C/SIDE) from PowerShell
I wanted to go a step further. Namely, I wanted to open/run a specific object from PowerShell.
Scenario’s
There are two scenario’s where I wanted to apply this:
- First of all the Test Toolkit. I’m sure we’re all using the testability framework, right? ;-). Well, automation of this framework makes sense. Automation with PowerShell makes sense as well. And running the Test codeunits with the CmdLet “Invoke-NAVCodeunit” is just not done, because the testability (and especially the test pages) needs to run in an environment WITH a User Interface. Invoke-NAVCodeunit is a background process, therefore not suitable to run Tests of the Testability framework. So I want to run a codeunit (For example the Test Runner) like I would do a Run of a codeunit in C/SIDE.
- In a demo scenario, I usually import objects and run a codeunit to import data into tables. I wanted to automate all: import objects, run codeunit AND show the data in the table that I just created. In this case, I wanted to run a table like I would run it in C/SIDE.
In other words, I would like to create a function like:
Start-NAVApplicationObjectInWindowsClient
That says it all, doesn’t it? I didn’t care too much on mandatory parameters and such .. but I tried to create a function that also works in a Multitenant environment. Here is how it looks:
function Start-NAVApplicationObjectInWindowsClient { [cmdletbinding()] param( [string]$ServerName=[net.dns]::Gethostname(), [int]$Port=7046, [String]$ServerInstance, [String]$Companyname, [string]$Tenant='default', [ValidateSet('Table','Page','Report','Codeunit','Query','XMLPort')] [String]$ObjectType, [int]$ObjectID ) $ConnectionString = "DynamicsNAV://$Servername" + ":$Port/$ServerInstance/$Companyname/Run$ObjectType"+"?$ObjectType=$ObjectID&tenant=$tenant" Write-Verbose "Connectionstring: $ConnectionString ..." Start-Process $ConnectionString }
There are only a few tricks that I would like to mention:
- In my parameters, I’m using the “ValidateSet” attribute to provide a predefined set of values for “ObjectType” variable – which makes a lot of sense, obviously
- I couldn’t find anywhere how to provide both tenant and objectid in the URL. I checked MSDN and tried to search online, but nowhere. The problem is, you can provide the tenant with a “?” in the URL, like:
DynamicsNAV://WIN-K5JLU49T31O:7046/DynamicsNAV90/CRONUS BELGIË NV/?tenant=default
You can run a codeunit by also using that questionmark, like:
DynamicsNAV://WIN-K5JLU49T31O:7046/DynamicsNAV90/CRONUS BELGIË NV/RunCodeunit?Codeunit=50000
But how to combine the two? How to run a codeunit in a company of a specific tenant?? Apparently, the solution was quite simple: it’s just a matter of combining the two with an ampersand (&), like:
DynamicsNAV://WIN-K5JLU49T31O:7046/DynamicsNAV90/CRONUS BELGIË NV/RunCodeunit?Codeunit=50000&tenant=default
Here are a few examples on how you can run the function.
Start-NAVApplicationObjectInWindowsClient ` -ServerInstance DynamicsNAV90 ` -ObjectType Codeunit ` -ObjectID 50000 ` -Verbose
Or with tenant:
Start-NAVApplicationObjectInWindowsClient ` -ServerName ([net.dns]::GetHostName()) ` -ServerInstance DynamicsNAV90 ` -Tenant MyTenantID ` -ObjectType Page ` -ObjectID 23
Enjoy!