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!

Challenge:

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}
or

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:

Param($NAVInstanceName)
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.

Troubleshooting:

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:

Enable-PSRemoting

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

18 responses to “Coffee Break – Windows PowerShell Remoting

  1. The crux of your writing whilst sounding reasonable at first, did not really sit perfectly with me after some time. Someplace throughout the sentences you actually were able to make me a believer but just for a while. I nevertheless have a problem with your leaps in assumptions and you might do nicely to fill in those gaps. In the event you actually can accomplish that, I will surely be fascinated.

  2. Very nice post. I just stumbled upon your blog and wanted to say that I’ve truly enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again soon!

  3. Attractive section of content. I just stumbled upon your blog and in accession capital to assert that I get actually enjoyed account your blog posts. Anyway I’ll be subscribing to your feeds and even I achievement you access consistently rapidly.

  4. I was speaking to a buddy of my own regarding this article and also regarding account recovery as well. I believe you made a lot of good points in this case, we are excited to keep reading material from you.

  5. An exciting discussion is worth comment. I believe that you simply ought to write additional on this subject, it could not be a taboo topic but usually persons aren’t sufficient to speak on such topics. Towards the next. Cheers

  6. You can definitely see your enthusiasm in the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. Always follow your heart.

  7. Hey! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading through your blog posts. Can you suggest any other blogs/websites/forums that cover the same topics? Thanks a ton!

  8. You actually make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complicated and extremely broad for me. I’m looking forward for your next post, I will try to get the hang of it!

Leave a Reply

Your email address will not be published.

Shares