Remote PowerShell Execution For SharePoint On-Premises
Background
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
Setup Remote Server
- 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″}’
- 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
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.
Invoke-Command -Session $s -ScriptBlock {get-SPContentDatabase}
Invoke-Command -Session $s -ScriptBlock {get-spserviceinstance}
Summary
Note – This article was originally published at this link.