# Help with ByPropertyName

**URL:** <https://forums.powershell.org/t/help-with-bypropertyname/2805>\
**Category:** PowerShell Help\
**Created:** [July 10, 2014, 3:42am UTC](https://forums.powershell.org/t/help-with-bypropertyname/2805 "2014-07-10T03:42:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike-chasse](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@mike-chasse](https://forums.powershell.org/u/mike-chasse)\
**Post date:** [July 10, 2014, 3:42am UTC](https://forums.powershell.org/t/help-with-bypropertyname/2805/1 "2014-07-10T03:42:46Z")

</div>

I have a script that I’m trying to use AD computer objects to populate a computername parameter. The script is called servicecheck.ps1 and it’s designed to see if a service is present, not present, or if computers are unreachable.

[cmdletbinding()]  
param (  
[parameter(mandatory=$true)][string]$servicename,

```
[parameter(mandatory=$true,valuefrompipelinebypropertyname=$true)]
[alias('Name')][string[]]$computername

```

)  
begin {}  
process{  
foreach ($single in $computername) {  
Try {  
invoke-command -ComputerName $single -ScriptBlock {param($rservicename) get-service `  
-name $rservicename} -ArgumentList $servicename -ErrorAction stop | out-null  
$success += “$single`r`n”  
} catch {  
if ($_.exception -like “cannot find any service”) {$serviceerror += “$single`r`n”}  
if ($_.exception -like “_connecting to remote_”) {$pserror += “$single`r`n”}  
}  
}  
}  
end {  
if ($success -eq $null) {$success = “None`r`n”}  
if ($serviceerror -eq $null) {$serviceerror = “None`r`n”}  
if ($pserror -eq $null) {$pserror = “None`r`n”}

```
write-output "Computers with $servicename present"
write-output "$success&#8175;r"
write-output "Computers without $servicename present"
write-output "$serviceerror&#8175;r"
write-output "Computers that couldn't be contacted"
write-output "$pserror"

```

}

When I try to run the command using the following line: Get-ADComputer -Filter \* | .\servicecheck.ps1 -servicename bits  
I get the following error for each computer that is fetched from get-adcomputer.

E:\servicecheck.ps1 : Cannot bind argument to parameter ‘computername’ because it is an empty array.  
At line:1 char:28

- Get-ADComputer -Filter \* | .\servicecheck.ps1 -servicename bits
- 

```
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidData: (CN=LON-DC1,OU=D…C=Adatum,DC=com:PSObject) [servicecheck.ps1], ParameterB  
indingValidationException
  - FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAllowed,servicecheck.ps1

When I run the script like this: Get-ADComputer -Filter \* | select @{n=‘name’;e={$\_.name}} | .\servicecheck.ps1 -servicename bits  
It works fine. I don’t want to have to resort to using a calculated property so am wondering why the name property in the adcomputer object isn’t matching up bypropertyname with the computername property I’ve setup with the alias of ‘name’

Thanks in advance for any insight anyone might have

---

<div class="post-metadata">

**Author:** ![matthieu-buisson](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@matthieu-buisson](https://forums.powershell.org/u/matthieu-buisson)\
**Post date:** [July 10, 2014, 4:46am UTC](https://forums.powershell.org/t/help-with-bypropertyname/2805/2 "2014-07-10T04:46:02Z")

</div>

If this :

```
Get-ADComputer -Filter * | select @{n='name';e={$_.name}} | .\servicecheck.ps1 -servicename bits
```

it means that the alias “Name” for your $computername parameter is not the problem.

The problem comes most likely from the objects which are in the pipeline when you run servicecheck.ps1.

Maybe, the objects in the pipeline after “Get-ADComputer -Filter \*” don’t actually present the “Name” property.  
If this is the case, the following should solve your issue :

```
Get-ADComputer -Filter * -Property * | .\servicecheck.ps1 -servicename bits
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 10, 2014, 4:56am UTC](https://forums.powershell.org/t/help-with-bypropertyname/2805/3 "2014-07-10T04:56:20Z")

</div>

That’s an annoying bit of behavior with the Active Directory cmdlets. They don’t play very well with the pipeline, but you can fix it by sticking Select-Object between the AD cmdlet and the command that’s to receive input:

```
Get-ADComputer -Filter * | Select-Object * | .\servicecheck.ps1 -servicename bits
```

---

<div class="post-metadata">

**Author:** ![mike-chasse](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@mike-chasse](https://forums.powershell.org/u/mike-chasse)\
**Post date:** [July 10, 2014, 6:35am UTC](https://forums.powershell.org/t/help-with-bypropertyname/2805/4 "2014-07-10T06:35:51Z")

</div>

What was driving me nuts is the name property is one of the ten properties that get-adcomputer retrieves by default.

Dave, thanks for the tip that the AD cmdlets don’t behave normally. When I tried inserting the select \* in between my script worked. Not ideal but it lets me know the parameters in my script are correct.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [July 10, 2014, 9:13am UTC](https://forums.powershell.org/t/help-with-bypropertyname/2805/5 "2014-07-10T09:13:21Z")

</div>

Just another note that if you only need the Name property, you will have some better performance just getting the required properties.

```
Get-ADComputer -Filter Name | Select-Object Name | .\servicecheck.ps1 -servicename bits
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:47pm UTC](https://forums.powershell.org/t/help-with-bypropertyname/2805/6 "2024-05-16T20:47:51Z")

</div>

Closed on May 16, 2024, 8:47pm UTC


