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.
Enable PowerShell Remoting. Run the below command in PowerShell (as administrator).
Enable-PSRemoting
Enable Server as Remote server. Run the below command in PowerShell (as administrator).
Enable-WSmanCredSSP -Role Server
Run the below 2 commands one after another.
winrm set winrm/config/winrs '@{MaxShellsPerUser="25"}'
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).
Enable-PSRemoting
Enable Client. Run the below command in PowerShell (as administrator).
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.
$password = ConvertTo-SecureString "your password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("username",$password)
Enable SSP and Get Session object to enter to remote server.
$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.
Invoke-Command -Session $s -ScriptBlock {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
Update-SPSolution -Identity yourfile.wsp -LiteralPath "C:\Program Files (x86)\(location of your file)...\yourfile.wsp" -GacDeployment
}
Now, enter the session of the remote server.
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.
Invoke-Command -Session $s -ScriptBlock {get-SPContentDatabase}
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.