使用Get-Service时未列出StartType

时间:2018-03-19 17:33:44

标签: powershell powershell-v4.0

我有一个脚本,用于检查已知服务列表的计算机名列表。它给出了这些服务的状态,它也应该列出StartType。是否有理由不在输出中给出StartType?

在输出中,PSComputerName,ServiceName和Status列包含数据,但StartType列仍为空白。

$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

我在版本5版本上尝试过:14409修订版:1012 &安培; 版本5版本:10586修订版117

1 个答案:

答案 0 :(得分:1)

Get-Service cmdlet可能不会返回StartType属性,但wmi会保存该信息。这应该适合你:

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
}

更新以使用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