Remote PowerShell Execution For SharePoint On-Premises

Background

 
We all know PowerShell is mostly used by SharePoint admins to manage their servers in a farm. SharePoint admins perform many activities as part of the SharePoint administration. In this article, we will learn how to execute remote PowerShell commands from one server on a remote server.
 

Why do we need this remote execution of PowerShell script?

  • Managing multiple servers and running a command on every server individually is time-consuming.
  • Centralized server (one server) to run a script on all remote servers gives more control over process.
  • Prevents uneccessary access to all servers.
  • Tighter and secure environment. 
  • Single script to run on one machine and updating multiple servers at once.

Use case

 
For the sake of the article, we will take an example on how to deploy WSP from client to SharePoint Server A.
 

Setup Remote Server

 
Check if WINRM service is started and running. Go to services.msc and see if Windows Remote Management (WS-Management) service is running.

Remote Powershell Exeuction For SharePoint On Premises
 
Enable PowerShell Remoting. Run the below command in PowerShell (as administrator).
  1. Enable-PSRemoting  
Enable Server as Remote server. Run the below command in PowerShell (as administrator).
  1. Enable-WSmanCredSSP -Role Server  
Run the below 2 commands one after another.
  1. winrm set winrm/config/winrs ‘@{MaxShellsPerUser=”25″}’  
  2. winrm set winrm/config/winrs ‘@{MaxMemoryPerShellMB=”600″}’  
Notes
  • Firewall rule must be enabled to allow communication with the server.  
  • Ensure the user has permission to SharePoint content database.

Setup Up Client machine

 
Enable PowerShell Remoting. Run the below command in PowerShell (as administrator).
  1. Enable-PSRemoting  
Enable Client. Run the below command in PowerShell (as administrator).
  1. Enable-WSmanCredSSP -Role "Client" -DelegateComputer "server2.contoso.com" -Force  
We have configured the client to run remote commands on any remote server.
 
Run commands from the client machine. 
 
Get user credentials and store in variables.
  1. $password = ConvertTo-SecureString "your password" -AsPlainText -Force  
  2. $cred = New-Object System.Management.Automation.PSCredential("username",$password)  
Enable SSP and Get Session object to enter to remote server.
  1. $s=new-PSsession -ComputerName serverA -authentication credssp -credential $cred     
Run command via -ScriptBlock attributes. Using script block, you can run multiple commands at once on a remote server.
  1. Invoke-Command -Session $s -ScriptBlock {
  2. Add-PSSnapin Microsoft.SharePoint.PowerShell;
  3. Update-SPSolution -Identity yourfile.wsp -LiteralPath "C:\Program Files (x86)\(location of your file)...\yourfile.wsp" -GacDeployment
  4. }  
Now, enter the session of the remote server.
  1. Enter-PSSession -session $s  
On successfully running this, it will run all the invoke commands on the remote server. Go to the server and check if WSP is updated.
Using the Invoke-Command method, you can run any command available and it will execute in the remote server.
 
Examples of other commands.
  1. Invoke-Command -Session $s -ScriptBlock {get-SPContentDatabase}  
  2. Invoke-Command -Session $s -ScriptBlock {get-spserviceinstance}   

Summary

 
In this article, we have learned how to enable remote execution of PowerShell script on SharePoint server and in an example, deployed WSP from the client to a SharePoint Server.