Start Dynamics NAV Windows Client (RTC) from PowerShell

After my sessions during “conference season”, I promised to blog about my scripts. I decided to do this on small pieces of scripts .. and this one is so useful, though I only realised it was useful until I wanted to do a gimmick during a script. I’m actually using it all the time now :-).

In short …

When playing around with PowerShell, you might want to create testing environments and such by very simply:

  • Restoring a backup
  • Create a service tier
  • Do whatever you want to do with it, for example:
    • turn it into a MultiTenant environment
    • Use it to upload objects to start solving conflicts of a merge

It is just useful to, at the end of your script, start your Windows Client right away, to not loose time in trying to figure out the “connection string” and such.. .

And it’s simple to do so in PowerShell

I’m using the fact that it’s possible to start NAV with a shortcut like “DynamicsNAV://”… . This way, it as easy as opening any URL from PowerShell.

I wrote a small function that covers it .. :

function Start-NAVWindowsClient
{
    [cmdletbinding()]
    param(
        [string]$ServerName, 
        [int]$Port, 
        [String]$ServerInstance, 
        [String]$Companyname, 
        [string]$tenant='default'
        )

    if ([string]::IsNullOrEmpty($Companyname)) {
       $Companyname = (Get-NAVCompany -ServerInstance $ServerInstance -Tenant $tenant)[0].CompanyName
    }

    $ConnectionString = "DynamicsNAV://$Servername" + ":$Port/$ServerInstance/$MainCompany/?tenant=$tenant"
    Write-Verbose "Starting $ConnectionString ..."
    Start-Process $ConnectionString
}

Simply start the “url” with “Start-Process” and off you go.. . 

Enjoy!

Ps, there is even a gimmick within this gimmick: look at the way the company is handled. When there is no provided with the function, the script will try to find the “first” CompanyName by doing this oneliner:

(Get-NAVCompany -ServerInstance $ServerInstance)[0].CompanyName

How cool is that? 🙂

5.00 avg. rating (97% score) - 1 vote

Permanent link to this article: https://www.waldo.be/2015/02/20/start-dynamics-nav-windows-client-rtc-from-powershell/

3 comments

2 pings

    • Peter Tijsma on February 21, 2015 at 7:54 am
    • Reply

    Hi Eric. Great Tip

  1. Hi Waldo,
    Im trying to combine this post whit this other http://www.waldo.be/2014/12/17/running-powershell-from-nav/ in order to start Dynamics Nav client from Nav 2016 companies databases.

    I have this piece of code in NAV 2016

    OpenRunspace(PSMSetup.Servername,PSMSetup.Username,PSMSetup.Password);

    LoadModule(PSMSetup.”Module Path”);
    PSMPowershellWrapper.ClearCommands;

    connectionString := ‘DynamicsNAV://localhost:7046’+’/’+parServerInstance+’/”‘+parCompanyName+'”/’;
    AddCommand(‘Start-Process’);
    AddParameterWithValue(‘FilePath’, connectionString);

    InvokeCommands(PSMPsObjectCollection);

    CloseRunspace();

    the variable connectString has this value in the runtime DynamicsNAV://localhost:7046/NAV2016SP/”PRUEBAS SP2″/

    If I execute Start-Process -FilePath DynamicsNAV://localhost:7046/NAV2016SP/”PRUEBAS SP2″/ in powershell everythis its OK and NAV 2016 starts on “PRUEBAS SP2” company

    but when I try to execute in NAV 2016 nothing is happen

    Thx in advance

      • waldo on April 1, 2016 at 11:25 am
        Author

      The only thing I can think of: your PowerShell is executed by the Server – so your client should/would try to start on the server. I’m not sure you’ll be able to do all this from the client .. but then again, why start the client with PowerShell – just start a Hyperlink, no?

  1. […] Continue reading » […]

  2. […] Start Dynamics NAV Windows Client (RTC) from PowerShell […]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.