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