Named Parameter not working in my script

I'm trying to write a PS function that takes a -Computername parameter. Here is the code I'm using:

function Get-Foo {
param(
    [Parameter(Mandatory=$False,Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
    [string[]]$Computername = $env:COMPUTERNAME
)

However, when I run the function as Get-Foo -Computername BAZ it never passes the named parameter BAZ, but instead always uses the value of $env:COMPUTERNAME. I'm generally new to PowerShell, and definitely to writing functions, so hopefully someone can quickly see where I'm going wrong here. I cut and modified the param line from another script that does work, so I don't know why it doesn't work here... Thanks.

Answers


How are you calling it? It worked for me in all the various types of way it could be set:

function Get-Foo {
    param(
        [Parameter(Mandatory=$False,Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string[]]$Computername = $env:COMPUTERNAME

    )
    Write-Host $Computername
}

Get-Foo -COmputername "hi"
"hi" | Get-Foo
$obj = New-Object PsObject -Property @{ Computername='hi' }
$obj | Get-Foo

All of which output "hi".


Need Your Help

Windows C++ CMD window switching

c++ windows cmd pipe

Hello fellow programmers, I have a problem with some console applications in a C++ program, and my objective is as follows.

AngularJS and MVC Framework, kind of projects?

jquery asp.net-mvc model-view-controller angularjs angular-ui

I'm new into web dev, I'm making my first steps into this world, and I was wondering something, I saw on Wikipedia that Angular was for running "single-page applications"