Tarek EZZAT
Tarek EZZAT

Reputation: 353

How to fix a Powersell bug

I have this powershell example that does not throw any error or exception.

However, NOTHING is printed on the output.

I am not able to find the reason

Maybe I don't call the Add-Values function correctly?

<#
.Synopsis
   To add two integer values
.DESCRIPTION
   Windows PowerShell Script Demo to add two values
   This accepts pipeline values
.EXAMPLE
   Add-Values -Param1 20 -Param2 30
.EXAMPLE
   12,23 | Add-Values
#> 

function Add-Values
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        #Accepts Only Integer
        [int]$Param1,

        #Accepts only interger
        [Parameter(Mandatory=$true,
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [int]$Param2
    )

    Begin
    {
        "Script Begins"
    }
    Process
    {
        $result = $Param1 + $Param2
    }
    End
    {
        $result
    }
}

pause

Upvotes: 0

Views: 169

Answers (1)

&#216;yvind
&#216;yvind

Reputation: 123

Remove the pause? Then paste your whole function into powershell. After that, try your function.

It kinda works for me.

PS ~>Add-Values -Param1 1 -Param2 2
Script Begins
3

parameter binding by datatype will give you the wrong result (it will use the last value for both parameters.

PS ~>1, 2 | Add-Values
Script Begins
4

Upvotes: 1

Related Questions