Taking a closer look at ways to run PowerShell on remote machines on your network.

But first, if you think you need more study to start with this, then please check out this free online course, offered by Microsoft and Edx:
Windows PowerShell Fundamentals

Customer Story:

The customer’s IT administrator want to remotely check Microsoft Dynamics NAV Server instances on their network. And they want to be able to script tasks and run those scripts from their laptop. So connecting to each (NAV) server with RDC is not an option. In fact now we are at it, why not remote from your mobile phone?

Basic installation guide for Windows PowerShell Web Access

How to do PowerShell on your phone

Customer Story II (The Sequel):

A problem of some kind has occurred on one of the installations and services need to get restarted/updated/tenants mounted. The customer’s IT administrator is in a traffic jam. A long phone call with instructions on what to press or not is one option. But PowerShell web access from the mobile, and simple web-run remote management while still stuck in that traffic jam might be a better one. More about this to come soon!


Next time you need to restart a NAV Server, change a config value or do any other (NAV) admin task that you can do with PowerShell, try to do it without actually connecting (RDC) to the machine. Do it via a PowerShell prompt on your machine.


PowerShell Remoting:

Imagine that you are sitting with your laptop, and from there you need to access (NAV) services on various different machines on your domain.

Option 1: -Computername
Many cmdlets implement the -Computername parameter, for example:

$RemoteServer = “MyServerName”
Get-Service -ComputerName $RemoteServer # shows you Windows services on [MyServerName]

This is the simplest way to run a cmdlet remotely. But there is no general rule that every cmdlet must implement the -ComputerName parameter or if they do, then how.


Option 2: Invoke-Command
Invoke-Command is a cmdlet that will send whatever command to the remote machine. Example:
Invoke-Command -ComputerName $RemoteServer -ScriptBlock{get-service}
or run anything that you are used to run from a cmd prompt
Invoke-Command -ComputerName $RemoteServer -ScriptBlock{ipconfig}

Invoke-Command -ComputerName $RemoteServer -ScriptBlock{dir C:\}
or even (though a bit pointless)
Invoke-Command -ComputerName $RemoteServer -ScriptBlock{calc}

If you want to prompt for credentials on the remote machine:

Invoke-Command {Get-Service} -computername $RemoteServer -Credential Get-Credential

Invoke-Command creates a new session, so to run a NAV cmdlet we first need to import the NAV management module and then run our NAV cmdlet. Each command separated by semi-colon:
Invoke-Command -ComputerName $RemoteServer `
-ScriptBlock {Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\80\Service\Microsoft.Dynamics.Nav.Management.dll’ `
; Get-NAVServerInstance | Format-Table -AutoSize}

Note that in this example you do not need to have NAV on the machine you are runningfrom. The only cmdlet that runs on your machine is Invoke-Command.

You can also run a script remotely. If you first create a script like this:

Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\80\Service\Microsoft.Dynamics.Nav.Management.dll’
Get-NAVServerInstance | Format-Table -AutoSize

Save it as c:\Tech\GetNAVServers.ps1

Then run it:

Invoke-Command -ComputerName $RemoteServer -FilePath{C:\Tech\GetNAVServers.ps1}

Notice that the script is saved locally on your machine, but it runs on the remote machine.

We can add parameters to this script by adding -ArgumentList Arg1, Arg2, Argn. Let’s make a small addition to the script by adding a Microsoft Dynamics NAV Server instance name as a parameter:

Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\80\Service\Microsoft.Dynamics.Nav.Management.dll’
Get-NAVServerInstance $NAVInstanceName | Format-Table -AutoSize

(if you want more than one parameter, just comma-separate them like thisParam($NAVInstanceName, $Tenant, $UserName, … etc)

Try to just run the script (locally):

CD C:\Tech\

.\GetNAVServers.ps1 -NAVInstanceName DynamicsNAV80

And remotely:

Invoke-Command -ComputerName $RemoteServer -FilePath{C:\Tech\GetNAVServers.ps1} -ArgumentList Dynamicsnav80


Option 3: PSSession
As mentioned before, Invoke-Command will create a new session. But we can also create a new session separately, and then re-use this session. Why?

  •   It takes time to open a session, so we can potentially save some time by re-using one session.
  •   The session is persistent (keeps variable values), which could also be of use.

Creating a remote session of course requires authentication. As long as we are remoting to a machine on our domain then Windows will take care of this. Creating a remote session to a machine in the cloud requires certificate. For this, see the previous blog (linked in the top of this one). But here we just want to open session(s) on our own network. But notice that once you have your PSSession object, then the rest of the syntax is exactly the same, whether the session is to a machine under your desk, or to a machine in an Azure data centre. Create a session to a machine ($RemoteServer) on your network could not be easier:

$MySession = New-PSSession $RemoteServer

Now you can (re) use this session with Invoke-Command. So in the examples above, just use -Session parameter instead of -ComputerName:
Invoke-Command -Session $MySession -ScriptBlock{get-service}

The next example will show what it means that the session is persistent:
Invoke-Command -Session $MySession -ScriptBlock{$i++;$i}

Run that one a few times, and see that $i increments every time.

Or you can enter a remote manual session:

Enter-pssession $MySession

Notice that the first part of the prompt changes to show you that the prompt is now running on the remote machine. To leave this machine and return PowerShell to your local machine, run EXIT.


On some (client) OS, PowerShell remoting is not enabled by default. PowerShell remoting runs under a service called WinRM. To test it:

Test-connection -ComputerName $RemoteServer
Test-wsman -ComputerName $RemoteServer

And as also mentioned by Pallea in comments to the blog linked above (thanks!), if remoting is not enabled you can enable it in this way:


Source: https://community.dynamics.com/nav

Leave a Reply

Your email address will not be published.