Enabling Remote PowerShell for SharePoint Maintainence and Deployments

While working with higher number of servers in the farm it some times become hard to execute maintainence scripts on every server in the farm including SharePoint, SQL and OWA Servers.  I recently worked on script to enable PowerShell remoting from the working server.  The process will be the following

Step 1: Enable Delegation for Credentials

First I will Enable the Working server to do enable delegation for credentials.  This basicly allows me to execute script with any user who has access to SharePoint. 

Step 2: Copy Script Files on all SharePoint Servers

This step will allow you to copy scripts folder to all servers you have specified.  You can completly automate this step if you are executing the copy script on one of the SharePoint Servers using Get-SPServer but In the example I was running the scripts out of SharePoint Farms.  I have another blog that

Step 3: Enable Remote PowerShell

Execute the 4 lines PowerShell script that will enable PowerShell Remoting and increase the memory and shell per users.

Step 4: Test the Remote PowerShell is Working

This script will perform some basic testing if the Remote PowerShell is working.  For SharePoint Servers it will Load the SharePoint Snapin and then check the version of SharePoint Farm as well as List the content database title and description.  For Non SharePoint Server it will try to get the server name using Get-WmiObject.

The scripts are given below.

1-Enable-DeletgationforSearchServersOnToolsServer.ps1

Write-Host "==================================================================" -ForegroundColor Green
Write-Host "You may need to Restart the Tools server for changes to take effect..." -ForegroundColor Yellow
Write-Host "==================================================================" -ForegroundColor Green
$Servers = @("Server1", "Server2", "Server3", "Server4")
foreach($Server in $Servers)
{
    $FQDN = $Server + ".fullyqualitydomain.contoso.com"
    Write-Host $FQDN -ForegroundColor Yellow
    Enable-WSManCredSSP -Role client -DelegateComputer $FQDN
    Write-Host "Delegation is Completed for $Server" -ForegroundColor Green
}
Write-Host "Delegation is Completed for all servers..."
Write-Host "Run the 2nd Copy-RemoteScript script and then Restart the Tools Server..." -ForegroundColor Red

2-Copy-RemoteScriptFolderToAllServers.ps1

Write-Host "===================================================" -ForegroundColor Green
Write-Host "Copying Files to all servers" -ForegroundColor Green
Write-Host "===================================================" -ForegroundColor Green
$Servers = @("Server1", "Server2", "Server3", "Server4")
foreach($Server in $Servers)
{
    $Location = "\\$Server\e$\RemoteScripts"
    Write-Host $Location
    Robocopy.exe E:\RemoteScripts\ $Location /MIR 
}
Write-Host "Files are Copy to All serversin E:\RemoteScripts..." -ForegroundColor Green

3-Enable-RemotingOnThisServer.ps1 (Execute on All Servers one by one)

Write-Host "===================================================" -ForegroundColor Green
Write-Host "Enabling Remote PowerShell and setting WRM Memory" -ForegroundColor Green
Write-Host "===================================================" -ForegroundColor Green
Set-ExecutionPolicy -ExecutionPolicy unrestricted
winrm quickconfig
Enable-WSManCredSSP -Role server
winrm set winrm/config/winrs '@{MaxShellsPerUser="25"}'
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="600"}'
Write-Host "Enabling Remote PowerShell and setting WRM Memory Completed.  Run the same on other servers one by one" -ForegroundColor Green

4-Test-RemotePowerShellOnAllServers.ps1 (Execute this script on Working Server from where you want to execute the script)

Write-Host "==================================================================" -ForegroundColor Green
Write-Host "You Must be a SharePoint Administrator to Test this script" -ForegroundColor Green
Write-Host "==================================================================" -ForegroundColor Green
Write-host "Press enter if you are a SharePoint Administrator or Press Ctrl + C to Cancel" -ForegroundColor Yellow
Read-Host 
Write-Host "Testing Remote PowerShell Completed on all Non SharePoint Servers..." -ForegroundColor Green

$farm = Get-Credential
$Servers = @("Server1", "Server2")
foreach($Server in $Servers)
{
    $FQDN = $Server + ".fullyqualitydomain.contoso.com"
    Write-Host "Testing Remote PowerShell on SharePoint : $FQDN" -ForegroundColor Yellow
    $s = New-PSSession -ComputerName $FQDN -Authentication CredSSP -Credential $farm
    Invoke-Command -Session $s -ScriptBlock { add-pssnapin Microsoft.SharePoint.PowerShell -ea 0 }
    Invoke-Command -Session $s -ScriptBlock { get-spfarm | Select Version }
    Invoke-Command -Session $s -ScriptBlock { get-spcontentdatabase | Select Id, Name }
    Get-PSSession | Remove-PSSession
    Write-Host "Testing Remote PowerShell Completed on $Server" -ForegroundColor Green
}

Write-Host "Testing Remote PowerShell Completed on SharePoint Servers..." -ForegroundColor Green
Write-Host "Starting on Non SharePoint Servers..." -ForegroundColor Yellow
$Servers = @("Server3", "Server4")
foreach($Server in $Servers)
{
    $FQDN = $Server + ".fullyqualitydomain.contoso.com"
    Write-Host "Testing Remote PowerShell on Non SharePoint : $FQDN" -ForegroundColor Yellow   
    $s = New-PSSession -ComputerName $FQDN -Authentication CredSSP -Credential $farm
    Write-Host "Getting Server name using WMI..." -ForegroundColor Yellow
    Invoke-Command -Session $s -ScriptBlock { $(Get-WmiObject Win32_Computersystem).name }
    Get-PSSession | Remove-PSSession
    Write-Host "Testing Remote PowerShell Completed on $Server" -ForegroundColor Green
}
Write-Host "Testing Remote PowerShell Completed on all Non SharePoint Servers..." -ForegroundColor Green

Note: If you do not see the right output fromt his command you must restart the server.  I have to restart the working server in 2 seperate occasions.  Now you can do anything on SharePoint Servers without even loggin to the servers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s