您将管道输入提供给Get-Service
没有任何其他参数,因此管道对象被传递给接受它们的第一个参数,即-Name
. 由于对象没有属性Name
,因此它们被整体传递并转换为字符串,因此它们显示为@{computername=SERVERONE}
. Get-Service
然后查找具有该名称的服务,这当然会失败,从而导致您观察到的错误。
Get-Service
(斜体的相关特征)的参数定义:
PS C:\> Get-Help Get-Service -Parameter Name
-Name
Specifies the service names of services to be retrieved. Wildcards
are permitted. By default, Get-Service gets all of the services on
the computer.
Required? false
Position? 1
Default value All services
Accept pipeline input? true (ByPropertyName, ByValue)
Accept wildcard characters? true
PS C:\> Get-Help Get-Service -Parameter ComputerName
-ComputerName
Gets the services running on the specified computers. The default
is the local computer.
Type the NetBIOS name, an IP address, or a fully qualified domain
name of a remote computer. To specify the local computer, type the
computer name, a dot (.), or "localhost".
This parameter does not rely on Windows PowerShell remoting. You
can use the ComputerName parameter of Get-Service even if your
computer is not configured to run remote commands.
Required? false
Position? named
Default value Local computer
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
Get-Process
(斜体的相关特征)的参数定义:
PS C:\> Get-Help Get-Process -Parameter Name
-Name
Specifies one or more processes by process name. You can type
multiple process names (separated by commas) and use wildcard
characters. The parameter name ("Name") is optional.
Required? false
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? true
PS C:\> Get-Help Get-Process -Parameter ComputerName
-ComputerName
Gets the processes running on the specified computers. The default
is the local computer.
Type the NetBIOS name, an IP address, or a fully qualified domain
name of one or more computers. To specify the local computer, type
the computer name, a dot (.), or "localhost".
This parameter does not rely on Windows PowerShell remoting. You
can use the ComputerName parameter of Get-Process even if your
computer is not configured to run remote commands.
Required? false
Position? named
Default value Local computer
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false
如您所见,两个 cmdlet 之间的参数定义存在差异。不仅可以按属性名称接受管道输入,还可以按值接受管道输入,但不接受。这就是为什么按预期处理您的管道输入,而没有。-Name
Get-Service
-Name
Get-Process
Get-Process
Get-Service
为避免此问题,您需要指定要获取的服务。用于*
所有服务。指定参数后,计算机名称将按照您的预期按属性名称-Name
传递给参数:-ComputerName
Get-ADComputer -Filter 'Name -eq "serverone"' |
select @{n='ComputerName';e={$_.Name}} |
Get-Service -Name *