PowerShell Script to Generate SharePoint 2010 Service Report

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:

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


Similar Articles