Services: Services enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted and does not show any user interface.

The PowerShell explained in this article will capture the following details:
  1. $global:timerServiceName = "SharePoint 2010 Timer"
  2. $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"
  3. # Get the local farm instance
  4. [Microsoft.SharePoint.Administration.SPFarm]$farm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
  5. Function SharePointServicesReport([Microsoft.SharePoint.Administration.SPFarm]$farm)
  6. {
  7. Write-Host ""
  8. write-host "Generating SharePoint services report" -fore Magenta
  9. $output = $scriptbase + "\" + "SharePointServices.csv"
  10. "ServiceName" + "," + "ServiceStatus" + "," + "StartUpType" + "," + "LogOnAs" + "," + "MachineName" | Out-File -Encoding Default -FilePath $Output;
  11. foreach($server in $farm.Servers)
  12. {
  13. foreach($instance in $server.ServiceInstances)
  14. {
  15. if($instance.TypeName -eq $timerServiceInstanceName)
  16. {
  17. [string]$serverName = $server.Name
  18. write-host "Generating SP services report for server" $serverName -fore yellow
  19. #$Monitor = "SPAdminV4" , "SPTimerV4" , "SPTraceV4" , "SPUserCodeV4" , "SPWriterV4" , "OSearch14" , "W3SVC" , "IISADMIN" , "C2WTS" , "FIMService" , "FIMSynchronizationService"
  20. Foreach($serviceName in get-content $scriptbase\Services.txt)
  21. {
  22. $service = Get-Service -ComputerName $serverName -Name $serviceName -ea silentlycontinue
  23. $StartUpType = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -ComputerName $serverName
  24. $serviceName + "," + $service.status + "," + $StartUpType.StartMode + "," + $StartUpType.startname + "," + $service.MachineName | Out-File -Encoding Default -Append -FilePath $Output;
  25. }
  26. write-host "SP services report generated" -fore green
  27. }
  28. }
  29. }
  30. }
Complete Code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
  2. $LogFile = ".\ServicesReportPatch-$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. import-module WebAdministration
  8. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  9. Set-Location $scriptBase
  10. write-host "TESTING FOR LOG FOLDER EXISTENCE" -fore yellow
  11. $TestLogFolder = test-path -path $scriptbase\Logs
  12. if($TestLogFolder)
  13. {
  14. write-host "The log folder already exist in the script location" -fore yellow
  15. $clearlogfolder = read-host "Do you want to clear the log folder (y/n)"
  16. if($clearlogfolder -eq 'y')
  17. {
  18. write-host "The user choosen to clear the log folder" -fore yellow
  19. write-host "Clearing the log folder" -fore yellow
  20. remove-item $scriptbase\Logs\* -recurse -confirm:$false
  21. write-host "Log folder cleared" -fore yellow
  22. }
  23. else
  24. {
  25. write-host "The user choosen not to clear the log files" -fore yellow
  26. }
  27. }
  28. else
  29. {
  30. write-host "Log folder does not exist" -fore yellow
  31. write-host "Creating a log folder" -fore yellow
  32. New-Item $Scriptbase\Logs -type directory
  33. write-host "Log folder created" -fore yellow
  34. }
  35. #moving any .rtf files in the scriptbase location
  36. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  37. if($FindRTFFile)
  38. {
  39. write-host "Some old log files are found in the script location" -fore yellow
  40. write-host "Moving old log files into the Logs folder" -fore yellow
  41. foreach($file in $FindRTFFile)
  42. {
  43. move-item -path $file -destination $scriptbase\logs
  44. }
  45. write-host "Old log files moved successfully" -fore yellow
  46. }
  47. start-transcript $logfile
  48. $global:timerServiceName = "SharePoint 2010 Timer"
  49. $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"
  50. # Get the local farm instance
  51. [Microsoft.SharePoint.Administration.SPFarm]$farm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
  52. Function SharePointServicesReport([Microsoft.SharePoint.Administration.SPFarm]$farm)
  53. {
  54. Write-Host ""
  55. write-host "Generating SharePoint services report" -fore Magenta
  56. $output = $scriptbase + "\" + "SharePointServices.csv"
  57. "ServiceName" + "," + "ServiceStatus" + "," + "StartUpType" + "," + "LogOnAs" + "," + "MachineName" | Out-File -Encoding Default -FilePath $Output;
  58. foreach($server in $farm.Servers)
  59. {
  60. foreach($instance in $server.ServiceInstances)
  61. {
  62. if($instance.TypeName -eq $timerServiceInstanceName)
  63. {
  64. [string]$serverName = $server.Name
  65. write-host "Generating SP services report for server" $serverName -fore yellow
  66. #$Monitor = "SPAdminV4" , "SPTimerV4" , "SPTraceV4" , "SPUserCodeV4" , "SPWriterV4" , "OSearch14" , "W3SVC" , "IISADMIN" , "C2WTS" , "FIMService" , "FIMSynchronizationService"
  67. Foreach($serviceName in get-content $scriptbase\Services.txt)
  68. {
  69. $service = Get-Service -ComputerName $serverName -Name $serviceName -ea silentlycontinue
  70. $StartUpType = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -ComputerName $serverName
  71. $serviceName + "," + $service.status + "," + $StartUpType.StartMode + "," + $StartUpType.startname + "," + $service.MachineName | Out-File -Encoding Default -Append -FilePath $Output;
  72. }
  73. write-host "SP services report generated" -fore green
  74. }
  75. }
  76. }
  77. }
  78. SharePointServicesReport $farm
  79. Stop-transcript
Execution Procedure
  1. Download and copy the PowerShell script to the server
  2. Input the “services.txt” file with the list of services for which you need to generate the report
  3. Launch PowerShell console
  4. Navigate to the script path and execute the script as in the following:


Conclusion

Thus this article outlines how to generate a SharePoint services report using a PowerShell script.