Smarter ideas worth writing about.

SharePoint Administration with PowerShell - Part I

Welcome to the first in a series of “SharePoint Administration with PowerShell" posts.  We’ve all been there when working on a large farm, installing an update, making an environment change, whatever it might be, requires an IIS reset.  

When you have a 3-server farm this is not such a big deal.  However, when we’re talking about farms of 6, 8 or even 10 servers it becomes more of a challenge. This post will review the script that will make the process much more manageable and ensure you don’t forget to reset a server.

Here is the script in its entirety:

#Adding this to all SharePoint scripts makes it easier to engage
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#The Servers in the farm.  Each server will need to allow remote PowerShell commands
[array]$servers="Server1","Server2","Server3","Server4","Server5","Server6","Server7","Server8"

# The following blocks are used when you want to reset additional services
<#
foreach ($server in $servers)
{
    Write-Host "Stopping SharePoint Timer Service on $server"
    Get-Service -Name "SharePoint Timer Service" -ComputerName $server | Stop-Service
}


foreach ($server in $servers)
{
    Write-Host "Stopping SharePoint User Code Host on $server"
    Get-Service -Name "SharePoint User Code Host" -ComputerName $server | Stop-Service
}
#>


Write-Host "Performing IIS Reset on Farm"

foreach ($server in $servers)
{
    Write-Host "Performing IISReset on $server"
    IISReset $server
    Write-Host "IISReset Status on $server"
    IISReset $server /status
} 


# Restart the additional services
<#
foreach ($server in $servers)
{
    Write-Host "Starting SharePoint Timer Service on $server"
    Get-Service -Name "SharePoint Timer Service" -ComputerName $server | Start-Service
}


foreach ($server in $servers)
{
    Write-Host "Starting SharePoint User Code Host on $server"
    Get-Service -Name "SharePoint User Code Host" -ComputerName $server | Start-Service
}
#>

Now let’s break the script down. 

I like developing/executing my scripts from the PowerShell ISE window.   This makes it necessary to include this line.  If you prefer to run using the SharePoint Management Shell this line is not necessary.
 

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Here we name all of the servers in the farm. Each of the servers named here will need to allow remote PowerShell commands. Here is an article describing the steps to configure.

[array]$servers="Server1","Server2","Server3","Server4","Server5","Server6","Server7","Server8"

In this section I illustrate how you’d reset a service on the target server.  You can use this method to manage any service on the target.  Here, I shutdown the Time Service and the User Code Host.  When managing SharePoint services, I have found it best to shut them down prior to the IIS Reset.  Uncomment these lines if you decide to use.

# The following blocks are used when you want to reset additional services
<#
foreach ($server in $servers)
{
    Write-Host "Stopping SharePoint Timer Service on $server"
    Get-Service -Name "SharePoint Timer Service" -ComputerName $server | Stop-Service
}


foreach ($server in $servers)
{
    Write-Host "Stopping SharePoint User Code Host on $server"
    Get-Service -Name "SharePoint User Code Host" -ComputerName $server | Stop-Service
}
#>

This block performs the actual IIS Reset and outputs each of the steps in the reset process.

Write-Host "Performing IIS Reset on Farm"

foreach ($server in $servers)
{
    Write-Host "Performing IISReset on $server"
    IISReset $server
    Write-Host "IISReset Status on $server"
    

This block restarts the services shutdown earlier in the script.


<#
foreach ($server in $servers)
{
    Write-Host "Starting SharePoint Timer Service on $server"
    Get-Service -Name "SharePoint Timer Service" -ComputerName $server | Start-Service
}


foreach ($server in $servers)
{
    Write-Host "Starting SharePoint User Code Host on $server"
    Get-Service -Name "SharePoint User Code Host" -ComputerName $server | Start-Service
}
#>


In my opinion, whether you have a 2-server farm or 10, using a script for management provides the most dependable method for execution. All SharePoint administrators should have a stable of PowerShell scripts to manage their environment.

Our next post we’ll use some of the methods covered here and apply them to reset the SharePoint Farm Cache.


Share:

About The Author

Principal Consultant
Chad is a Principal Consultant in Cardinal’s Raleigh Office.  He has worked 15 years within the Microsoft Technology stack and specializes in architecting SharePoint solutions for Cardinal’s clients.