Kade Williams
Kade Williams

Reputation: 1181

Cannot get service StartType in PowerShell

I have a script that scans a list of servers/computers for a service and displays the status.

I am running PS version 5 Build 10586 Revision 117 on both of my computers.

On my Windows 7 computer running PSv5, the StartType is outputted to the .txt

MachineName  ServiceName                 Status StartType
-----------  -----------                 ------ ---------
srvcomp0201 My.ServiceName.Here          Stopped Stopped         
srvcomp0202 My.OtherServiceName          Running Running  

When I run this same script on my Windows 2012 computer with PSv4 or v5, the StartType is not outputted to the .txt or .csv

MachineName  ServiceName                 Status StartType
-----------  -----------                 ------ ---------
srvcomp0201 My.ServiceName.Here          Stopped          
srvcomp0202 My.OtherServiceName          Running          

I can change the order that is supposed to be displayed like:

$s | select MachineName, StartType, ServiceName, Status

And it still doesn't show anything when run on the Win2012 computer.

Why is it doing this?

$serviceList = Get-Content C:\services.txt

$results = Get-Content C:\servers.txt | ForEach-Object {

    foreach ($service in $serviceList) {
        if ($s=get-service -computer $_ -name $service -ErrorAction SilentlyContinue)
        {
            $s | select MachineName, StartType, ServiceName, Status | Out-File C:\test.txt -Append
        } else {
            "$_ - Service '$service' does not exist."
        }
    }
}

UPDATE

This doesn't write the StartType to the file either:

$serviceList = Get-Content C:\services.txt

$results = Get-Content C:\servers.txt | ForEach-Object {

    foreach ($service in $serviceList) {
        if ($s=get-service -computer $_ -name $service -ErrorAction SilentlyContinue)
        {
            $s | select MachineName, StartType | Out-File C:\test.txt -Append
        } else {
            "$_ - Service '$service' does not exist."
        }
    }
}

This doesn't display the StartType on my 2012 computer either:

get-content c:\servicelist\computers.txt | % {
    if ($s=get-service -computer $_ -name W3SVC* -ErrorAction SilentlyContinue)  # change -name * to name of service
    {
        $s | select MachineName, ServiceName, StartType, Status
    }
    else {"Service is not available on $_"}
    }

Is this a Windows 2012 thing?

Upvotes: 1

Views: 2303

Answers (2)

mklement0
mklement0

Reputation: 439123

Windows PowerShell has a range of cmdlets that do not use PowerShell remoting (based on [MS-PRSP], the PowerShell Remoting Protocol specification), typically identifiable by having their own -ComputerName parameter (as opposed to invoking them via "meta"-invocation cmdlets such as Invoke-Command -ComputerName).

Use of this obsolescent, DCOM-based, per-cmdlet remoting should be abandoned in favor of the part-of-the-plumbing PowerShell remoting.
In fact, the per-cmdlet form of remoting is no longer available in PowerShell Core.

Therefore, instead of:

Get-Service -ComputerName $_ -name $service

use:

Invoke-Command -ComputerName $_ { Get-Service $service }

The latter uses PowerShell remoting, whose requirements differ from the DCOM-based form remoting and may therefore require additional setup; see Get-Help about_Remoting_FAQ

I cannot personally verify this, but based on Olaf's comment on the question, this may implicitly solve your problem (for which I have no explanation).

Upvotes: 2

demokritos
demokritos

Reputation: 1446

The default PowerShell version delivered with the OS would make a difference. [see https://4sysops.com/wiki/differences-between-powershell-versions/#powershell-and-windows-versions] Windows 2012 comes with v3, but Windows 10 comes with v5, where StartType property is added.

Upvotes: 1

Related Questions