1

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?

Kade Williams
  • 1,041
  • 2
  • 13
  • 28
  • 1
    Have you tried piping to `Format-List` or `Get-Member` to see what properties are available? Perhaps `StartType` is there, simply under another name. Try `$s|fl *` and see what properties you have available on the 2012 machine. – TheMadTechnician Mar 06 '18 at 20:01
  • 1
    fl * shows that it's not available. I guess it has to do with Win2012. – Kade Williams Mar 06 '18 at 20:08
  • 1
    @KadeWilliams I can't remember the details of _why_ that is, but you can use [Get-WmiObject](https://stackoverflow.com/a/4306059/5039142) and `StartMode` of the results to work around it. – G42 Mar 06 '18 at 20:16
  • 1
    Fun fact: When you run `Get-Service` locally on a W2K12 server the property StartType does not seem to exist. When you run `Get-Service` remote against the same server the property StartType does exist in fact. – Olaf Mar 06 '18 at 21:02
  • 1
    What is the Powershell version? [$PSVersionTable]. It needs to be v5+ – demokritos Mar 07 '18 at 02:35
  • I tried 5 and 5.1 – Kade Williams Mar 07 '18 at 03:33

2 Answers2

2

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).

mklement0
  • 382,024
  • 64
  • 607
  • 775
1

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.

demokritos
  • 1,416
  • 2
  • 12
  • 34