PowerShell:- Pass By Reference & Command Line Arguments

Background

I have a small networking project that I am working on.

There are ways to get data via tools, but I will like to examine how I can get identical data via scripts.

And, so here I googled on certain keywords.

Found a very worthy sample code.

But, also saw that a bit more abstraction will likely be helpful.

Towards that goal, I cleaned up the command-line interface and replaced the write-host route of passing back results from a function.

I prefer to use traditional pass by reference; especially to support swapping values.

 

Code



param ( `
              [Parameter(Mandatory)] [string] $operation  `
            , [Parameter(Mandatory)] [int] $num1 `
            , [Parameter(Mandatory)] [int] $num2 `
      )
      
Set-StrictMode -Version Latest;

[int] $iNumberofCmdArgs =0;
[int] $result = $null;

[string] $logQuestion = $null;
[string] $logAnswer = $null;
[string] $log = $null;

[int] $num1Original = 0;
[int] $num2Original = 0;

function calc {
param
( 
  [Parameter(Position=0)]
  [ref]$result 
  
, [Parameter(Position=1)]
  [string] $operation
  
, [Parameter(Position=2)]
  [ref] $num1 

, [Parameter(Position=3)]
  [ref] $num2     

)
	
	[int] $temp = 0;
  
    if ($operation -eq "swap")
    {
		  
		$temp = $num1.value;
		 
		$num1.value = $num2.value;

		$num2.value = $temp;

    } 
  
    elseif ( `
                    ( $operation -eq "+") `
                -or ( $operation -eq "plus") `
           )
  
    {
	  
		$result.value = $num1.value + $num2.value;
	 
    } 

    elseif ( `
                    ( $operation -eq "-") `
                -or ( $operation -eq "minus") `
           )
    {
	  
		$result.value = $num1.value - $num2.value;
	 
    } 
  

    elseif ( `
                    ( $operation -eq "*") `
                -or ( $operation -eq "times") `
           )
    {
	  
		$result.value = $num1.value * $num2.value;
	 
    }


    elseif ( `
    
                    ( $operation -eq "/") `
                -or ( $operation -eq "divide") `
           )
    {
	  
	    if ($num2.value -ne 0)
		{
			$result.value = $num1.value / $num2.value;
	    }
		
    } 	
  
}

# save data
$num1Original = $num1;
$num2Original = $num2;

#call function
calc -result ([ref]$result) `
     -operation ($operation) `
     -num1 ( [ref]$num1 ) `
     -num2 ( [ref]$num2 );

$logQuestion = "Operation {0} involving num1 {1} and num2 {2} yields " `
                    -f $operation, $num1Original, $num2Original;

if ($operation -eq "swap")
{

    $logAnswer = "{0} {1}" -f $num1, $num2;

}
elseif ( `
                ( $operation -eq "+") `
            -or ( $operation -eq "plus") `
       )
{

    $logAnswer = "{0}" -f $result;

}

elseif ( `
                ( $operation -eq "-") `
            -or ( $operation -eq "minus") `
       )
{
     
    $logAnswer = "{0}" -f $result;   
    
}   


elseif ( `
              ($operation -eq "*") `
            -or ($operation -eq "times") `
       )
{
  
	$logAnswer = "{0}" -f $result;  
 
} 


elseif ( `
                ( $operation -eq "/") `
            -or ( $operation -eq "divide") `
       )
{
	
	$logAnswer = "{0}" -f $result;  
 
}

$log = $logQuestion + $logAnswer;

Write-Output $log;

Explanation

  1. Command Line Arguments
    • Attribute
      • Named Command Line Arguments
    • Arguments
      • $operation
        • Attribute:- Mandatory, String
      • $num1
        • Attribute:- Mandatory, int
      • $num2
        • Attribute:- Mandatory, int
  2. Function:- calc
    • Functionality
      • Accepts operation and numbers ( num1 and num2 )
      • Returns result
    • Arguments
      • $operation
        • Attribute:- String
      • $result
        • Attribute:- Reference
      • $num1
        • Attribute:- Reference
      • $num2
        • Attribute:- Reference
    • Operation
      • If the operation is “swap”
        • Save data in pointer num1 =>value into temp variable
        • Copy data from pointer num2 => value into pointer num1 => value
        • Copy data from temp variable into pointer num2 => value
  3. Main
    • Invoke method calc
    • Pass along arguments
      • Attribute
        • Named Arguments
      • Arguments
        • -result ( [ref]$result)
        • -operation ($operation)
        • -num1 ( [ref]$num1 )
        • -num2 ( [ref]$num2 )

 

Source Code

GitHub

Gist

DanielAdeniji/powershellPassByRef.ps1
Link

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s