IISRESET in SharePoint Using Powershell Script

Introduction

Consider a scenario in which you are working on a large Farm and when troubleshooting it's required to do an IISRESET. Doing it manually is a boring task. You can run a PowerShell script to do it. This article outlines how to do an IISRESET using a PowerShell script.

IISRESET

An IIS Reset does 2 important things concerning SharePoint:

  • It recycles all your application pools at one go and clears the memory objects.
  • It reloads all DLLs from the Global Assembly Cache (GAC) in the Farm servers.

When you do an IISreset, your web sites and applications become unavailable, all sessions connected to your web server are dropped and you lose any existing state in your applications. Changes to the metabase can be lost. Your web sites and applications will be unavailable until the affected internet services are restarted.

The IISreset command stops and restarts the IIS Admin Service, the Windows Process Activation Service (WAS), and the World Wide Web Publishing Service (WWW Service).

Consider the preceding items before performing an IISRESET in a production environment.

Code block

The following piece of code helps in performing an IISRESET throughout the Farm.

  1. $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"  
  2. $farm = get-spfarm  
  3. foreach($server in $farm.Servers)  
  4. {  
  5.     foreach($instance in $server.ServiceInstances)  
  6.     {  
  7.         # If the server has the timer service then stop the service  
  8.         if($instance.TypeName -eq $timerServiceInstanceName)  
  9.         {  
  10.             [string]$serverName = $server.Name  
  11.             write-host "Performimg IISRESET on the server " $serverName -fore magenta  
  12.             IISRESET $ServerName /noforce  
  13.             write-host "IISRESET performed on the server " $servername -fore green  
  14.             write-host "IIS status for server " $serverName  
  15.             IISRESET $serverName /Status  
  16.         }  
  17.     }  
  18. }  
Complete code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\PerformIISRESETPatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.   
  14. write-host "TESTING FOR LOG FOLDER EXISTENCE" -fore yellow  
  15. $TestLogFolder = test-path -path $scriptbase\Logs  
  16. if($TestLogFolder)  
  17. {  
  18.     write-host "The log folder already exist in the script location" -fore yellow  
  19.     $clearlogfolder = read-host "Do you want to clear the log folder (y/n)"  
  20.     if($clearlogfolder -eq 'y')  
  21.     {  
  22.         write-host "The user choosen to clear the log folder" -fore yellow  
  23.         write-host "Clearing the log folder" -fore yellow  
  24.         remove-item $scriptbase\Logs\* -recurse -confirm:$false  
  25.         write-host "Log folder cleared" -fore yellow  
  26.     }  
  27.     else  
  28.     {  
  29.         write-host "The user choosen not to clear the log files" -fore yellow  
  30.     }  
  31. }  
  32. else  
  33. {  
  34.     write-host "Log folder does not exist" -fore yellow  
  35.     write-host "Creating a log folder" -fore yellow  
  36.     New-Item $Scriptbase\Logs -type directory  
  37.     write-host "Log folder created" -fore yellow  
  38. }         
  39.  
  40. #moving any .rtf files in the scriptbase location  
  41. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  42. if($FindRTFFile)  
  43. {  
  44.     write-host "Some old log files are found in the script location" -fore yellow  
  45.     write-host "Moving old log files into the Logs folder" -fore yellow  
  46.     foreach($file in $FindRTFFile)  
  47.         {  
  48.             move-item -path $file -destination $scriptbase\logs  
  49.         }  
  50.     write-host "Old log files moved successfully" -fore yellow  
  51. }  
  52.   
  53. start-transcript $logfile  
  54.   
  55. $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"  
  56. $farm = get-spfarm  
  57.   
  58. foreach($server in $farm.Servers)  
  59. {  
  60.     foreach($instance in $server.ServiceInstances)  
  61.     {  
  62.         # If the server has the timer service then stop the service  
  63.         if($instance.TypeName -eq $timerServiceInstanceName)  
  64.         {  
  65.             [string]$serverName = $server.Name  
  66.             write-host "Performimg IISRESET on the server " $serverName -fore magenta  
  67.             IISRESET $ServerName /noforce  
  68.             write-host "IISRESET performed on the server " $servername -fore green  
  69.             write-host "IIS status for server " $serverName  
  70.             IISRESET $serverName /Status  
  71.         }  
  72.     }  
  73. }  
  74.   
  75. write-host ""  
  76. write-host "SCRIPT COMPLETD" -fore green  
  77. stop-transcript  
Execution procedure
  1. Download and copy the script to the SharePoint server.
  2. Navigate to the script path.
  3. Execute the script.



Conclusion

Thus this article outlines how to do an IISRESET using a PowerShell script.

Up Next
    Ebook Download
    View all
    Learn
    View all