none
Dynamic Parameters in Powershell RRS feed

  • Question

  • Hello,

    How can I achieve this in powershell

    using System; class Program { static void Main() { int sum1 = SumParameters(1); int sum2 = SumParameters(1, 2); int sum3 = SumParameters(3, 3, 3); int sum4 = SumParameters(2, 2, 2, 2); Console.WriteLine(sum1); Console.WriteLine(sum2); Console.WriteLine(sum3); Console.WriteLine(sum4); } static int SumParameters(params int[] values) { int total = 0; foreach (int value in values) { total += value; } return total; } }

    Output

    1
    3
    9
    8
    Monday, December 30, 2013 1:24 AM

Answers

  • Even easier:

    function SumParameters{
         param(
              [int[]]$values
         ) 
         
         [int]$total=0
         foreach($value in $values){
              $total+=$value;
         }
         return $total
    }

    SumParameters 1,2,3,4,5

    help about_functions -full


    ¯\_(ツ)_/¯





    • Edited by jrv Monday, December 30, 2013 2:09 AM
    • Marked as answer by BJKP Monday, December 30, 2013 2:19 AM
    Monday, December 30, 2013 2:04 AM
  • It's very similar in PowerShell :

    Function SumParameters 
    {
        param ([int[]]$values)
        $total = 0
        foreach ($value in $values)
        {
            $total += $vaue 
        }
        return $total
    }

    And after dot sourcing the above function ...you can call it as you wish:

    PS C:\> SumParameters 1
    1
    
    PS C:\> SumParameters 1,2
    3
    
    PS C:\> SumParameters 1,2,3
    6
    Hope this helps


    Knowledge is Power{Shell}.

    EDIT : Submittted it late .. :)
    • Edited by DexterPOSH Monday, December 30, 2013 2:07 AM
    • Marked as answer by BJKP Monday, December 30, 2013 2:20 AM
    Monday, December 30, 2013 2:06 AM

All replies

  • That is easy:

    $code=@'
    using System;
    
    public class Program{
    
        public static void Main(){
    	 
    	int sum1 = SumParameters(1);
    	int sum2 = SumParameters(1, 2);
    	int sum3 = SumParameters(3, 3, 3);
    	int sum4 = SumParameters(2, 2, 2, 2);
    	 
    	Console.WriteLine(sum1);
    	Console.WriteLine(sum2);
    	Console.WriteLine(sum3);
    	Console.WriteLine(sum4);
        }
    
        static int SumParameters(params int[] values){
    	 
    	int total = 0;
    	foreach (int value in values){
    	    total += value;
    	}
    	return total;
        }
    }
    '@
    Add-Type $code
    [program]::Main()
    


    ¯\_(ツ)_/¯

    Monday, December 30, 2013 1:34 AM
  • Thanks for your reply.

    With out invoking C# code,  Can I directly write this function in power shell 

    static int SumParameters(params int[] values){
    	 
    	int total = 0;
    	foreach (int value in values){
    	    total += value;
    	}
    	return total;
        }

     

    And passing parameters 

    SumParameters 1

    SumParameters 1 2  

    SumParameters 1 2 3  

    Monday, December 30, 2013 1:55 AM
  • Even easier:

    function SumParameters{
         param(
              [int[]]$values
         ) 
         
         [int]$total=0
         foreach($value in $values){
              $total+=$value;
         }
         return $total
    }

    SumParameters 1,2,3,4,5

    help about_functions -full


    ¯\_(ツ)_/¯





    • Edited by jrv Monday, December 30, 2013 2:09 AM
    • Marked as answer by BJKP Monday, December 30, 2013 2:19 AM
    Monday, December 30, 2013 2:04 AM
  • It's very similar in PowerShell :

    Function SumParameters 
    {
        param ([int[]]$values)
        $total = 0
        foreach ($value in $values)
        {
            $total += $vaue 
        }
        return $total
    }

    And after dot sourcing the above function ...you can call it as you wish:

    PS C:\> SumParameters 1
    1
    
    PS C:\> SumParameters 1,2
    3
    
    PS C:\> SumParameters 1,2,3
    6
    Hope this helps


    Knowledge is Power{Shell}.

    EDIT : Submittted it late .. :)
    • Edited by DexterPOSH Monday, December 30, 2013 2:07 AM
    • Marked as answer by BJKP Monday, December 30, 2013 2:20 AM
    Monday, December 30, 2013 2:06 AM
  • Of course in Powerhell we would do it the PowerShell way:

    (1,2,3,4,5,6|Measure -sum).Sum


    ¯\_(ツ)_/¯

    Monday, December 30, 2013 2:13 AM
  • All parameters are basically dynamic if used that way:

    function x{
        ($args | Measure -sum).Sum
    }

    x 1 2 3 4 5


    ¯\_(ツ)_/¯

    Monday, December 30, 2013 2:15 AM
  • Thanks both for your quick replies. 

    Monday, December 30, 2013 2:19 AM