You might my previous post about Dynamics NAV deployments and sharing ports. If you didn’t read the post, please do so, so you know how to do it in the first place.

Well, waldo wouldn’t be waldo if he wouldn’t try to find out how to do this with PowerShell ;-).

Sure, Port Sharing still isn’t really “encouraged” by Microsoft. But off the record (and with a really big disclaimer: I don’t support this either ;-)) – this works for our customers very well, and more importantly, it works for our internal development environments as well (+100 NST’s on one server) .. never had any problems related to this.

Problem

You can run about any dos command in powershell .. Sometimes it’s a real dos-exe, but most of the time, it’s a PowerShell alias. Problem in this case was that I needed to be able to use the command “sc”, but in PowerShell, this “sc” was an alias for “Set-Content” .. which doesn’t have anything remotely to do with the dos-version of the command (which is “configure a service”).

Solution

It was actually one of my developers that pointed me in the right direction. Why not just point to the exe, by not using “sc”, but “sc.exe”. Would it really be that simple? Uhm .. Yes .. actually it was. So I’ve spent some time in creating a function to cover about anything there is to cover for setting up “Port Sharing” for a certain ServerInstance. Here is the function:

function Enable-NAVServerInstancePortSharing
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$false, Position=0)]
        [System.String]
        $ServiceInstance = 'DynamicsNAV90'
    )
    write-Host -ForegroundColor Green "Enabling PortSharing for $ServiceInstance"
    Set-NAVServerInstance -ServerInstance $ServiceInstance -Stop -ErrorAction SilentlyContinue

    $null = sc.exe config (get-service NetTcpPortSharing).Name Start= Auto
    $null = Start-service NetTcpPortSharing
    
    $null = sc.exe config (get-service  "*$ServiceInstance*").Name depend= HTTP/NetTcpPortSharing
    
    Set-NAVServerInstance -ServerInstance $ServiceInstance -Start 
}

As you can see, the commandline that I’m using is actually just the same as I was doing before .. but to be sure, I just enable the NetTcpPortSharing just in case, and restart the ServerInstance within the same function as well.