# Processing parameters in a function

**URL:** <https://forums.powershell.org/t/processing-parameters-in-a-function/5207>\
**Category:** PowerShell Help\
**Created:** [November 12, 2015, 9:57pm UTC](https://forums.powershell.org/t/processing-parameters-in-a-function/5207 "2015-11-12T21:57:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark-brown-2](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@mark-brown-2](https://forums.powershell.org/u/mark-brown-2)\
**Post date:** [November 12, 2015, 9:57pm UTC](https://forums.powershell.org/t/processing-parameters-in-a-function/5207/1 "2015-11-12T21:57:41Z")

</div>

I have written a simple function. It takes a computer name, and then in the Process {} block I use Invoke-Command to run an action on each machine. My parameter is created like this:  
Param(  
[Parameter(valuefrompipeline=$true,Position=0)]  
[string]$ComputerName=$ThisServer,

So I can supply a single name, a list of names or get a list from the pipeline.  
However, it appears that things get processed in different ways when they get to the Process{} block. A single name is OK, as is input from the pipeline. But a list of machine names fails. Is this because the list is an array, and this is handled differently? How do get round this in a simple way?

Thanks

---

<div class="post-metadata">

**Author:** ![stackexchangeuser3](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@stackexchangeuser3](https://forums.powershell.org/u/stackexchangeuser3)\
**Post date:** [November 12, 2015, 10:25pm UTC](https://forums.powershell.org/t/processing-parameters-in-a-function/5207/2 "2015-11-12T22:25:13Z")

</div>

Can you post the rest of your code? Just be sure to sanitize it so it doesn’t contain any private information. It would be useful to see what the Function actually does instead of just seeing the Parameter.

---

<div class="post-metadata">

**Author:** ![mark-brown-2](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@mark-brown-2](https://forums.powershell.org/u/mark-brown-2)\
**Post date:** [November 12, 2015, 10:33pm UTC](https://forums.powershell.org/t/processing-parameters-in-a-function/5207/3 "2015-11-12T22:33:47Z")

</div>

Here you go Ed, nothing special…

Function Stop-GEOService  
{

```
[CmdletBinding()]
Param(
    [Parameter(valuefrompipeline=$true)]
    [string[]]$ComputerName=($servername.keys | %{if($servername.$_ -eq $env:computername){$_}})
    )
    Begin
    {
     "Preparing to stop service..."
    }
    Process
    {
        $geoServer= $($serverName[$($ComputerName)]) 
        "Processing: $ComputerName System Name: $geoServer"
        try
        {
          Invoke-Command -ComputerName $geoServer { Stop-Service -DisplayName "GEO*" -Confirm} -ErrorAction stop
          Add-VSToolsLog -LogFile VSTools.log -LogText " services were stopped on $ComputerName"
        }
        Catch
        {
            Write-Host "An error occured"
            $errorMessage = $_.Exception.Message
            Write-Host $errorMessage
            Add-VSToolsLog -LogFile VSTools.log -LogText "An error occurred trying to Stop GEO services for $ComputerName, the error was $errorMessage"
        }
        }
    End
    {
    }

```

}

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [November 13, 2015, 1:02am UTC](https://forums.powershell.org/t/processing-parameters-in-a-function/5207/4 "2015-11-13T01:02:05Z")

</div>

SideNote: If you use the “pre” and “/pre” html tags around your code, it will put it in a pretty format like below, making it much easier t read in the post.

Here is an example of your issue.

```
Function Test {
    [CmdLetBinding()]
    Param(
        [Parameter(valuefrompipeline=$true)]
        [String[]]$ComputerName
    )
    Begin{}
    Process {
        Write-Host $ComputerName
    }
    End {}
}

$objects = @(
    [PSCustomObject]@{ComputerName = "Test1"; Date = (Get-Date).AddDays(-1)},
    [PSCustomObject]@{ComputerName = "Test2"; Date = (Get-Date).AddDays(-10)},
    [PSCustomObject]@{ComputerName = "Test3"; Date = (Get-Date).AddDays(-20)}
)

$objects | test
write-host "------------------------------------"
test -ComputerName $objects
```

**Results:**

```
@{ComputerName=Test1; Date=11/12/2015 08:21:25}
@{ComputerName=Test2; Date=11/03/2015 08:21:25}
@{ComputerName=Test3; Date=10/24/2015 08:21:25}
------------------------------------
@{ComputerName=Test1; Date=11/12/2015 08:21:25} @{ComputerName=Test2; Date=11/03/2015 08:21:25} @{ComputerName=Test3; Date=10/24/2015 08:21:25}
```

The reason for this difference is that when you are using the Pipeline, the Process{} script block acts like a foreach loop for the pipeline input, so each input object in processed one at a time. The Process{} block only does this for pipeline input. So when you pass the objects as a parameter, there is nothing looping through the objects individually to process them one at a time. Instead they are being processed as one single array of objects. Thus the result is different.

In order to handle multiple object as input by a parameter instead of via the pipeline, you need to add a foreach loop to the code like this.

```
Function Test {
    [CmdLetBinding()]
    Param(
        [Parameter(valuefrompipeline=$true)]
        [String[]]$ComputerName
    )
    Begin{}
    Process {
        ForEach ($Computer in $ComputerName) {
            Write-Host $Computer
        }
    }
    End {}
}

$objects = @(
    [PSCustomObject]@{ComputerName = "Test1"; Date = (Get-Date).AddDays(-1)},
    [PSCustomObject]@{ComputerName = "Test2"; Date = (Get-Date).AddDays(-10)},
    [PSCustomObject]@{ComputerName = "Test3"; Date = (Get-Date).AddDays(-20)}
)

$objects | test
write-host "------------------------------------"
test -ComputerName $objects
```

## **Results:** @{ComputerName=Test1; Date=11/12/2015 08:28:32} @{ComputerName=Test2; Date=11/03/2015 08:28:32} @{ComputerName=Test3; Date=10/24/2015 08:28:32}

@{ComputerName=Test1; Date=11/12/2015 08:28:32}  
@{ComputerName=Test2; Date=11/03/2015 08:28:32}  
@{ComputerName=Test3; Date=10/24/2015 08:28:32}

---

<div class="post-metadata">

**Author:** ![mark-brown-2](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@mark-brown-2](https://forums.powershell.org/u/mark-brown-2)\
**Post date:** [November 13, 2015, 2:52am UTC](https://forums.powershell.org/t/processing-parameters-in-a-function/5207/5 "2015-11-13T02:52:22Z")

</div>

Great, thanks for the help.

---

<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:44pm UTC](https://forums.powershell.org/t/processing-parameters-in-a-function/5207/6 "2024-05-16T20:44:28Z")

</div>


