powershell - Get-Service 参数绑定(bind)未按预期工作

标签 powershell parameterbinding

我正在学习 PowerShell,并且对参数绑定(bind)有疑问。这可能是一个简单的问题,但我不知所措。

如果我输入:

get-adcomputer -filter 'name -eq "serverone"' |
  select @{name='computername';e={$_.name}} |
  get-process

这给了我“serverone”上的进程列表并且工作正常。但如果我输入:
get-adcomputer -filter 'name -eq "serverone"' |
  select @{name='computername';e={$_.name}} |
  get-service

然后我收到以下错误:
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

为什么是这样?两个Get-ProcessGet-Service接受计算机名,并且此参数的帮助文件看起来相同。有趣的是,如果我输入相同的代码但添加 -Name bitsGet-Service上面的命令,它会返回服务详细信息。所以它看起来像 Get-Service正在尝试将对象绑定(bind)到服务名称,但 Get-Process 不会发生这种情况在语法上看起来非常相似?!

最佳答案

您将管道输入提供给 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

如您所见,-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 *

关于powershell - Get-Service 参数绑定(bind)未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30333996/

相关文章:

Powershell 脚本在显示输出之前终止

powershell - 如何获取目标用户(不同)域的 FQDN?

powershell - PS 流水线多参数绑定(bind)

parameterbinding - 参数绑定(bind)不适用于 SQLite PRAGMA table_info

powershell - 通过管道传递多个值

php - PDO 不绑定(bind)参数

.net - 如何为windows服务添加命令行界面

powershell - 替换配置文件中的变量

powershell - Sitecore安装框架因 “Path”错误而失败