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