Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

ValueFromPipeLine and ValueFromPipeLineByPropertyName

$
0
0

Hello

I am trying to pass on a parameter down the pipeline to the next piece of code.

However I get an error, and I think I have an understanding of why but not completely sure

if I do Get-ADComputer it returns and object with a number of members (properties etc.) as follows

DistinguishedName
DNSHostName
Enabled
Name
ObjectClass
ObjectGUID
SamAccountName
SID
UserPrincipalName
WriteDebugStream
WriteErrorStream
WriteVerboseStream
WriteWarningStream

I note the first one in the list is "DistinguishedName" this may be relevant latter on.

One of the properties is labeled 'Name' e.g. the NetBIOS name of the Server e.g. Server1

so next I write a simple test function as follows:

function test {
[cmdletbinding()]
param(
[parameter(ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true)]
[alias("name")]$ComputerName
)

Get-Service -ComputerName $ComputerName

}

So basically get-service passing it the -computername

as for the $Computername parameter in the params section, I have stated

ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true
and
alias("name")

So I am thinking if an object comes down the pipeline which has a 'property' labeled 'name' this will then match up to $Computername (due to the alias) then therefore the following

Get-ADComputer -filter {Name -eq 'Server1'} | Test

I would expect to get a list of Services on Server1

However I get the following error

Get-Service : Cannot open Service Control Manager on computer 'CN=Server1,OU=Servers,DC=Test,DC=local. This operation might require other privileges.
At C:\temp\test1.ps1:8 char:1
+ Get-Service -ComputerName $ComputerName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Service], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetServiceCommand

I note from the above is using the DistinguishedName (I am logged in as domain admin, so no privileges issues) and passing that to $ComputerName (or appears to be) rather than mapping the name property to computername.

I note as above  DistinguishedName  is the first property in the list above, so I assume if tried to bind property by property from the top down list starting with DistinguishedName. However if there is no binding why does it not move on the next one in the list etc. until it can bind name to computername via the alias

any advise most welcome

AAnotherUser

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles