Get-Service参数绑定未按预期工作

PSh*_*bie 4 powershell parameterbinding

我正在学习PowerShell并且对参数绑定有疑问.这可能是一个简单的问题,但我很茫然.

如果我输入:

get-adcomputer -filter 'name -eq "serverone"' |
  select @{name='computername';e={$_.name}} |
  get-process
Run Code Online (Sandbox Code Playgroud)

这给了我一个关于"serverone"的进程列表,并且运行正常.但如果我输入:

get-adcomputer -filter 'name -eq "serverone"' |
  select @{name='computername';e={$_.name}} |
  get-service
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误:

get-service : Cannot find any service with service name
'@{computername=SERVERONE}'. At line:1 char:93
+ ... e={$_.name}} | get-service
+                    ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{computername=SERVERONE}:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Run Code Online (Sandbox Code Playgroud)

为什么是这样?双方Get-ProcessGet-Service接受计算机名,以及该参数的帮助文件看起来相同.有趣的是,如果我键入相同的代码但添加-Name bitsGet-Service上面的命令,它会返回服务详细信息.所以它看起来像是Get-Service试图将对象绑定到服务名称,但是这种情况Get-Process在语法上看起来非常相似?

Ans*_*ers 5

您将管道输入提供给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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

正如你可以看到有在的定义有区别-Name的两个cmdlet之间的参数.不仅通过属性名称而且通过值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 *
Run Code Online (Sandbox Code Playgroud)