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
- $global:timerServiceName = "SharePoint 2010 Timer"
- $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"
-
- # Get the local farm instance
- [Microsoft.SharePoint.Administration.SPFarm]$farm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
-
- Function SharePointServicesReport([Microsoft.SharePoint.Administration.SPFarm]$farm)
- {
-
- Write-Host ""
- write-host "Generating SharePoint services report" -fore Magenta
-
- $output = $scriptbase + "\" + "SharePointServices.csv"
- "ServiceName" + "," + "ServiceStatus" + "," + "StartUpType" + "," + "LogOnAs" + "," + "MachineName" | Out-File -Encoding Default -FilePath $Output;
-
- foreach($server in $farm.Servers)
- {
- foreach($instance in $server.ServiceInstances)
- {
-
- if($instance.TypeName -eq $timerServiceInstanceName)
- {
- [string]$serverName = $server.Name
- write-host "Generating SP services report for server" $serverName -fore yellow
- #$Monitor = "SPAdminV4" , "SPTimerV4" , "SPTraceV4" , "SPUserCodeV4" , "SPWriterV4" , "OSearch14" , "W3SVC" , "IISADMIN" , "C2WTS" , "FIMService" , "FIMSynchronizationService"
- Foreach($serviceName in get-content $scriptbase\Services.txt)
- {
- $service = Get-Service -ComputerName $serverName -Name $serviceName -ea silentlycontinue
-
- $StartUpType = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -ComputerName $serverName
- $serviceName + "," + $service.status + "," + $StartUpType.StartMode + "," + $StartUpType.startname + "," + $service.MachineName | Out-File -Encoding Default -Append -FilePath $Output;
- }
-
- write-host "SP services report generated" -fore green
- }
- }
- }
-
- }
Complete Code
- $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
- $LogFile = ".\ServicesReportPatch-$LogTime.rtf"
-
- # Add SharePoint PowerShell Snapin
-
-
- if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
- Add-PSSnapin Microsoft.SharePoint.Powershell
- }
- import-module WebAdministration
-
- $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
- Set-Location $scriptBase
-
- write-host "TESTING FOR LOG FOLDER EXISTENCE" -fore yellow
- $TestLogFolder = test-path -path $scriptbase\Logs
- if($TestLogFolder)
- {
- write-host "The log folder already exist in the script location" -fore yellow
- $clearlogfolder = read-host "Do you want to clear the log folder (y/n)"
- if($clearlogfolder -eq 'y')
- {
- write-host "The user choosen to clear the log folder" -fore yellow
- write-host "Clearing the log folder" -fore yellow
- remove-item $scriptbase\Logs\* -recurse -confirm:$false
- write-host "Log folder cleared" -fore yellow
- }
- else
- {
- write-host "The user choosen not to clear the log files" -fore yellow
- }
- }
- else
- {
- write-host "Log folder does not exist" -fore yellow
- write-host "Creating a log folder" -fore yellow
- New-Item $Scriptbase\Logs -type directory
- write-host "Log folder created" -fore yellow
- }
-
- #moving any .rtf files in the scriptbase location
- $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
- if($FindRTFFile)
- {
- write-host "Some old log files are found in the script location" -fore yellow
- write-host "Moving old log files into the Logs folder" -fore yellow
- foreach($file in $FindRTFFile)
- {
- move-item -path $file -destination $scriptbase\logs
- }
- write-host "Old log files moved successfully" -fore yellow
- }
-
- start-transcript $logfile
-
- $global:timerServiceName = "SharePoint 2010 Timer"
- $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"
-
- # Get the local farm instance
- [Microsoft.SharePoint.Administration.SPFarm]$farm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
-
- Function SharePointServicesReport([Microsoft.SharePoint.Administration.SPFarm]$farm)
- {
-
- Write-Host ""
- write-host "Generating SharePoint services report" -fore Magenta
-
- $output = $scriptbase + "\" + "SharePointServices.csv"
- "ServiceName" + "," + "ServiceStatus" + "," + "StartUpType" + "," + "LogOnAs" + "," + "MachineName" | Out-File -Encoding Default -FilePath $Output;
-
- foreach($server in $farm.Servers)
- {
- foreach($instance in $server.ServiceInstances)
- {
-
- if($instance.TypeName -eq $timerServiceInstanceName)
- {
- [string]$serverName = $server.Name
- write-host "Generating SP services report for server" $serverName -fore yellow
- #$Monitor = "SPAdminV4" , "SPTimerV4" , "SPTraceV4" , "SPUserCodeV4" , "SPWriterV4" , "OSearch14" , "W3SVC" , "IISADMIN" , "C2WTS" , "FIMService" , "FIMSynchronizationService"
- Foreach($serviceName in get-content $scriptbase\Services.txt)
- {
- $service = Get-Service -ComputerName $serverName -Name $serviceName -ea silentlycontinue
-
- $StartUpType = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -ComputerName $serverName
- $serviceName + "," + $service.status + "," + $StartUpType.StartMode + "," + $StartUpType.startname + "," + $service.MachineName | Out-File -Encoding Default -Append -FilePath $Output;
- }
-
- write-host "SP services report generated" -fore green
- }
- }
- }
-
- }
-
- SharePointServicesReport $farm
-
- Stop-transcript
Execution Procedure
- Download and copy the PowerShell script to the server
- Input the “services.txt” file with the list of services for which you need to generate the report
- Launch PowerShell console
- 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.