Connecting to a Raspberry Pi using PowerShell

Connecting to a raspberry pi using PowerShell seems incredibly complicated. Really, it's overly complicated. I think it's because the messages you get back just don't really mean much. I've had to do this a few times now to install custom drivers on it. The pain of connecting is really a one-time pain per computer but when I swap computers I have to go through the whole process again. This time I decided to keep track of the commands I'm running. So here is a console output of my path to being able to connect to a Raspberry Pi 2 in PowerShell.

PS C:\WINDOWS\system32> Enter-PSSession -ComputerName minwinpc -Credential minwinpc\Administrator -Authentication Basic

Enter-PSSession : Connecting to remote server minwinpc failed with the following error message : The WinRM client
cannot process the request. Unencrypted traffic is currently disabled in the client configuration. Change the client
configuration and try the request again. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ Enter-PSSession -ComputerName minwinpc -Credential minwinpc\Administr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<del>
    + CategoryInfo          : InvalidArgument: (minwinpc:String) [Enter-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

I researched the error message: "Unencrypted traffic is currently disabled in the client configuration. Change the client configuration and try the request again."
and found out how to change my client configuration using WSMan.

PS C:\WINDOWS\system32> cd WSMan:\localhost\Client

Start WinRM Service
WinRM service is not started currently. Running this command will start the WinRM service.

Do you want to continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
cd : Cannot find path 'WSMan:\localhost\Client' because it does not exist.
At line:1 char:1
+ cd WSMan:\localhost\Client
+ </del>~~~~~~~~~~~~~~~~~~~~~~<del>
    + CategoryInfo          : ObjectNotFound: (WSMan:\localhost\Client:String) [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

Well, that didn't work but I'm at least at a WSMan prompt.

PS WSMan:\localhost> dir

Start WinRM Service
WinRM service is not started currently. Running this command will start the WinRM service.

Do you want to continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

Start WinRM Service
WinRM service is not started currently. Running this command will start the WinRM service.

Do you want to continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):
dir : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is
accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If
the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm
quickconfig".
At line:1 char:1
+ dir
+ </del>~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetChildItemCommand

Based on the error message I ran winrm quickconfig, still not knowing what I'm even configuring.

PS WSMan:\localhost> winrm quickconfig

WinRM is not set up to receive requests on this machine.
The following changes must be made:

Start the WinRM service.
Set the WinRM service type to delayed auto start.

Make these changes [y/n]? y

WinRM has been updated to receive requests.

WinRM service type changed successfully.
WinRM service started.
WinRM is not set up to allow remote access to this machine for management.
The following changes must be made:

Enable the WinRM firewall exception.

Make these changes [y/n]? y

WinRM has been updated for remote management.

WinRM firewall exception enabled.

Finally I was able to use WSMan.

PS WSMan:\localhost> dir

   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   MaxEnvelopeSizekb                              500
System.String   MaxTimeoutms                                   60000
System.String   MaxBatchItems                                  32000
System.String   MaxProviderRequests                            4294967295
Container       Client
Container       Service
Container       Shell
Container       Listener
Container       Plugin
Container       ClientCertificate

Now that I could use it, I went to the Client directory which is where I originally tried to go.

PS WSMan:\localhost> cd Client

PS WSMan:\localhost\Client> dir

   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   NetworkDelayms                                 5000
System.String   URLPrefix                                      wsman
System.String   AllowUnencrypted                               false
Container       Auth
Container       DefaultPorts
System.String   TrustedHosts

The original error said I needed to Allow Unencrypted traffic.

PS WSMan:\localhost\Client> set-item .\AllowUnencrypted $true

PS WSMan:\localhost\Client> dir

   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   NetworkDelayms                                 5000
System.String   URLPrefix                                      wsman
System.String   AllowUnencrypted                               true
Container       Auth
Container       DefaultPorts
System.String   TrustedHosts

Now that I did what the error message said I needed to do, I tried to remotely connect again.

PS C:\WINDOWS\system32> Enter-PSSession -ComputerName minwinpc -Credential minwinpc\Administrator -Authentication Basic*

Enter-PSSession : Connecting to remote server minwinpc failed with the following error message : The WinRM client
cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not
joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts
configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not
be authenticated. You can get more information about that by running the following command: winrm help config. For
more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ Enter-PSSession -ComputerName minwinpc -Credential minwinpc\Administr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (minwinpc:String) [Enter-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

Now the error message is saying that my Raspberry Pi (minwinpc) is not a trusted host. So I looked in the WSMan directory again for that.

PS C:\WINDOWS\system32> cd WSMan:\localhost\Client

PS WSMan:\localhost\Client> dir

   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   NetworkDelayms                                 5000
System.String   URLPrefix                                      wsman
System.String   AllowUnencrypted                               true
Container       Auth
Container       DefaultPorts
System.String   TrustedHosts

Then I just set the value to be the Raspberry Pi device name.

PS WSMan:\localhost\Client> set-item .\TrustedHosts 'minwinpc'

WinRM Security Configuration.
This command modifies the TrustedHosts list for the WinRM client. The computers in the TrustedHosts list might not be
authenticated. The client might send credential information to these computers. Are you sure that you want to modify
this list?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
PS WSMan:\localhost\Client> dir


   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   NetworkDelayms                                 5000
System.String   URLPrefix                                      wsman
System.String   AllowUnencrypted                               true
Container       Auth
Container       DefaultPorts
System.String   TrustedHosts                                   minwinpc

After a warning I had my trusted sites set and it was back to trying to connect remotely.

PS WSMan:\localhost\Client> Enter-PSSession -ComputerName minwinpc -Credential minwinpc\Administrator* [minwinpc]: PS C:\Data\Users\Administrator\Documents>

Now I am remotely connected to the Raspberry Pi. For me, then I could run the devcon tool that I wanted to run to add a driver. Here is that command I made, just to complete the loop here.

[minwinpc]: PS C:\Data\Users\Administrator\Documents> cd c:\windows\system32

[minwinpc]: PS C:\windows\system32> devcon -dp_add .\NXPPN71x0.inf

********************************** WARNING **********************************

This Devcon command should only be used for development/testing purposes.



Driver package INF files should be added/updated into the offline OS image

using the supported offline package deployment process in order to ensure

that they are properly configured and integrated into the Windows OS state.



Updating driver package INF files from within the runtime environment of

this OS product may lead to unexpected OS state conflicts, resulting in the

inability to apply future updates to core OS components and/or drivers on

retail/consumer systems.

********************************** WARNING **********************************
Driver package 'oem0.inf' added.
[minwinpc]: PS C:\windows\system32>

That's it. Hopefully This made it a bit easier for you to connect.