Question

I am using the following Powershell script in hopes of identifying Started service applications by server.

Spot checking the results shows something unexpected. For instance, I know Lotus Notes Connector is Stopped on all servers in my farm but the script returns it.

What other additional properties must I test for to be sure I am seeing the exact same results as shown in Central Admin "Manage Services on Servers" page?

[System.Collections.ArrayList]$ReportInfo = New-Object System.Collections.ArrayList($null)


$servers = (get-spfarm).servers
foreach ($server in $servers)
{


    foreach($service in $server.serviceinstances)
    {
        if($service.TypeName -eq "Lotus Notes Connector"){
            Write-Host stop;
        }

        if ($service.status = "Online")
        {
            Write-Host "Server" $server.Name "`tService: " $service.TypeName;

            $servicInfo = @{}
            $servicInfo.Server = $service.Server.Name;
            $servicInfo.Service = $service.TypeName;

            $ReportInfo.Add((New-Object PSObject -Property $servicInfo))>$null;
        }
    }
}

Write-Host "Exporting CSV"

$ReportInfo | Export-Csv  "Services.csv" -NoTypeInformation -Encoding UTF8 -Delimiter '|'
Was it helpful?

Solution

What is wrong with simply using

Get-SPServiceInstance | select typename, status, server | Export-Csv test.csv

to get the data?

OTHER TIPS

When using PowerShell to get the service instances, they all seem to come back online, as you describe. However, if we do it in a C# method, it comes back correctly. Here is what I did to make them come back correctly:

$Assem = ( 
    "Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" 
    ) 

$Source = @" 
using Microsoft.SharePoint.Administration; 
using Microsoft.SharePoint; 
using System.Collections.Generic;
using System; 

namespace ReportingStuff 
{ 
    public static class Servers  
    { 
        public static List<SPServiceInstance> GetInstances() 
        { 
            List<SPServiceInstance> items = new List<SPServiceInstance>();

            foreach (SPServer server in SPFarm.Local.Servers)
            {
                foreach (SPServiceInstance instance in server.ServiceInstances)
                {
                    if ((!instance.SystemService && !(instance.GetType().FullName.Contains("SPAdministrationServiceInstance"))) && (!(instance is SPTimerServiceInstance) && (server.ServiceInstances.GetValue<SPTimerServiceInstance>() != null)))
                        items.Add(instance);
                }
            }

            return items;
        } 
    } 
} 
"@ 

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp  

$output = [ReportingStuff.Servers]::GetInstances()
Write-Output $output

You can use the method to get your collection of instances and then do your filtering, outputting, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top