4

PowerShell を学んでいて、パラメーター バインディングについて質問があります。簡単な質問かもしれませんが、困っています。

私が入力した場合:

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

どうしてこれなの?と の両方が computernameGet-ProcessGet-Service受け入れ、このパラメーターのヘルプ ファイルは同じように見えます。興味深いことに、同じコードを上記のコマンドに追加-Name bitsして入力すると、サービスの詳細が表示されます。Get-Serviceつまり、オブジェクトをサービス名にバインドしようとしているように見えますが、構文が非常に似ているものでGet-Serviceは発生しませんか?!Get-Process

4

2 に答える 2

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

パラメータの定義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

ご覧のとおり、2 つのコマンドレット間でパラメーターの定義に違いがあります。は、プロパティ名だけでなく値によってもパイプライン入力を受け入れますが、受け入れません。そのため、パイプライン入力を意図したとおりに処理しますが、そうではありません。-NameGet-Service-NameGet-ProcessGet-ProcessGet-Service

この問題を回避するには、取得するサービスを指定する必要があります。すべてのサービスに使用*します。パラメーターを指定すると、意図したとおりに、コンピューター名がプロパティ名によってパラメーター-Nameに渡されます。-ComputerName

Get-ADComputer -Filter 'Name -eq "serverone"' |
  select @{n='ComputerName';e={$_.Name}} |
  Get-Service -Name *
于 2015-05-19T22:15:33.810 に答える
0

コマンドレットにはパラメーター セット (1 つ以上) があります。

Get-Service には、 DefaultDisplayName、およびInputObjectの 3 つがあります。

残念ながら、それらのいずれも使用していません。これが、PowerShell が何かについて不平を言う理由を説明しています。

利用可能なパラメーター セットを次のように使用できます。

  • InputObject : タイプオブジェクトが必要ServiceControllerですが、これはあなたの場合ではありません。
  • DisplayName : これには、次のように明示的にパラメーター名を指定する必要があります。

1 |
select @{l='computername';e={$env:COMPUTERNAME}} |
gsv -DisplayName 'your-service's-display-name'

  • デフォルト: これはおそらくコードに一致するものです。このパラメーター セットを使用すると、ComputerNameパラメーターをオブジェクト プロパティまたは位置パラメーターとして渡すことができます。例えば:

1- 位置パラメーターとして:

1 |
select @{l='computername';e={$env:COMPUTERNAME}} |
gsv alg # 'alg' is the service name; you can choose some other(s)

2- オブジェクト パラメータとして:

1 |
select @{l='name';e={'alg'}},@{l='computername';e={$env:COMPUTERNAME}} |
gsv

この最後のものはあなたのコードに非常に似ていることに注意してください。重要な違いは、オブジェクトのプロパティName ( ComputerName以外) を定義していることです。

于 2015-05-20T02:03:13.003 に答える