使用Get-Service查询多服务器服务时StartType列空白的问题
为什么StartType列没有输出?
这是远程服务查询里很常见的小坑!我来帮你拆解原因和解决办法:
最可能的原因:Get-Service远程查询丢失属性
如果你是用Get-Service -ComputerName(这个参数在新版PowerShell里已经被标记为弃用)或者通过Invoke-Command远程调用Get-Service,返回的服务对象是序列化后的简化版本——PowerShell默认只会保留常用属性,像StartType这类非核心属性会被自动丢弃,自然输出时这一列就是空白的。
举个你可能踩中的代码例子:
Invoke-Command -ComputerName (Get-Content $myServers) -ScriptBlock { Get-Service -Name (Get-Content $using:myServices) }
这种写法返回的对象里根本没有StartType属性,输出时当然看不到内容。
另一个可能:查询时漏掉了StartType属性
如果你用了CIM/WMI查询,但在Select-Object里没明确指定StartType,也会出现这个问题。比如类似这样的代码:
Get-CimInstance Win32_Service -ComputerName $server | Select-Object PSComputerName, ServiceName, Status # 这里没把StartType加入选择列表,输出自然空白
解决办法
方法1:改用Get-CimInstance查询Win32_Service类
这是最可靠的远程获取服务完整属性的方式,Win32_Service类原生包含StartType,而且不会丢失任何属性:
# 读取服务器和服务列表 $servicesList = Get-Content $myServices $serversList = Get-Content $myServers # 批量查询服务信息 foreach ($server in $serversList) { # 单服务查询写法 Get-CimInstance Win32_Service -ComputerName $server -Filter "Name='$servicesList'" | Select-Object PSComputerName, ServiceName, Status, StartType # 多服务可以用这种Filter写法 # $filter = "Name IN ('$($servicesList -join "','")')" # Get-CimInstance Win32_Service -ComputerName $server -Filter $filter | Select-Object ... }
方法2:如果坚持用Get-Service,显式保留属性
要是你一定要用Get-Service,可以在远程脚本块里手动构建包含StartType的自定义对象:
Invoke-Command -ComputerName $serversList -ScriptBlock { $services = Get-Service -Name $using:servicesList $services | Select-Object @{Name='PSComputerName';Expression={$env:COMPUTERNAME}}, ServiceName, Status, StartType }
不过要注意,Get-Service的远程支持已经被微软弃用,未来版本可能会移除,还是更推荐用CIM的方式。
额外检查点
- 确认你的账号有远程服务器的WMI/CIM访问权限(通常需要管理员权限)
- 检查目标服务器的WinRM服务是否正常运行(CIM依赖WinRM)
- 确保
services.txt里的服务名称完全正确(如果服务不存在,也可能导致属性为空)
内容的提问来源于stack exchange,提问作者Kade Williams
相关产品推荐
相关产品推荐

