Hello,
I´m fairly new to scripting/programming in general (including Powershell), trying to move away from being a GUI-based technician. While doing my best to get my first cmdlets to work I came across an unexpected behavior, I hope someone can give me an explanation for this. I made two nearly identical functions to illustrate my problem:
function Test-Name
{
Param
(
[Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Alias("Name")]
$ComputerName
)
$ComputerName
}
function Test-Name2
{
Param
(
[Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Alias("ComputerName")]
$Name
)
$Name
}
And then I tested a few things:
PS U:> Get-ADComputer Test | Test-Name
Test-Name : Cannot bind argument to parameter ‘ComputerName’ because it is null.
At line:1 char:23
- Get-ADComputer Test | Test-Name
-
~~~~~~~~~
- CategoryInfo : InvalidData: (CN=Test,CN=Computers,DC=test,DC=ing:PSObject) [Test-Name], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Test-Name
PS U:> Get-ADComputer Test | Select-Object Name | Test-Name
Test
PS U:> Get-ADComputer Test | Test-Name2
Test
PS U:> (Get-ADComputer Test).gettype()
IsPublic IsSerial Name BaseType
True False ADComputer Microsoft.ActiveDirectory.Management.ADAccount
PS U:> (Get-ADComputer Test | Select-Object *).gettype()
IsPublic IsSerial Name BaseType
True False PSCustomObject System.Object
I red in a post, that I unfortunately can´t find right now, that objects from ActiveDirectory module cmdlets acts weird in the pipeline and that using Select-Object was a way around it. I can see that the object types are different, but why does it work when the parameter name match the property name from the pipeline?