StartType not being listed when using Get-Service

泄露秘密 提交于 2020-01-06 06:46:22

问题


I have a script that checks a list of computer names for a list of known services. It gives the status of those services and it also is supposed to list the StartType. Is there a reason why the StartType is not being given in the output?

In the output, the PSComputerName, ServiceName, and Status columns contain data but the StartType column remains blank.

$myServices = $PSScriptRoot + "\services.txt" # $PSScriptRoot references current directory
$myServers = $PSScriptRoot + "\servers.txt"
$Log = $PSScriptRoot + "\svclog.csv"

Remove-Item -Path $Log

$serviceList = Get-Content $myServices

$results = Get-Content $myServers
Invoke-command -ComputerName $results -ScriptBlock {
Param($serviceList)
    Get-Service $serviceList | Select -Property ServiceName, Status, StartType
} -ArgumentList $serviceList,$Null | Select -Property PSComputerName, ServiceName, Status, StartType |
Export-Csv -NoTypeInformation -Path $Log

I have tried it on Version 5 build: 14409 Revision: 1012 & Version 5 build: 10586 Revision 117


回答1:


The Get-Service cmdlet may not return the StartType property, but wmi does house that information. This should work for you:

ForEach ($Service in $myServices)
{
    Get-WmiObject -ComputerName @(Get-Content -Path $myServers) -Class Win32_Service -Filter "Name='$Service'" |
        Select-Object -Property PSComputerName, ServiceName, Status, StartType |
        Export-Csv -NoTypeInformation -Path $Log -Append
}

Update to use Invoke-Command:

Invoke-Command -ComputerName $results -ScriptBlock {
    ForEach ($service in $using:serviceList)
    {
        Get-WmiObject -Class Win32_Service -Filter "Name='$service'" |
            Select-Object -Property PSComputerName, Name, Status, StartMode
    }
} | Export-Csv -NoTypeInformation -Path $Log


来源:https://stackoverflow.com/questions/49369069/starttype-not-being-listed-when-using-get-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!