System.Collections.Hashtable

Hi developer,
I have already made script in windows powershell.
For example: Two integers variables then sum ( {5,5} make 10. )
Here is my script:

function TwoValues($input)
{
    $regex = "({.*?})"
    
    $matchInfo = @($input | Select-String -Pattern  $regex -AllMatches)
    $dictionary = @{}
    foreach($minfo in $matchInfo)
    {
        foreach($match in @($minfo.Matches | Foreach {$_.Groups[0].value}))
        {
            $element = $match.Substring(1, $match.Length - 2);
        
            [array]$value = $element.Split(','); 
       
            [int]$add = [int]::Parse($value[0]) + [int]::Parse($value[1])

            $dictionary.Add($add)
        }
    }
    return $dictionary
}

Write-Host "Number of Arg=" $args.Length

foreach($arg in $args)
{
    $twoValues = TwoValues($arg)
    Write-Host "$twoValues"
}

The function is fine but the only problem is command line argument.
 I put  .\....ps1 {5,5} in powershell command line argument. i got display output and it says:

Number of Arg= 1
System.Collections.Hashtable

Any Idea?

Thanks

May 20th, 2015 1:13am

.\....ps1 "{5,5}"

Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 10:02am

Hi IamGuy84,

Please refer to the script below, I have tested without issue:

function TwoValues($inp)
{
    $regex = "({.*?})"
    
    $matchInfo = @($inp | Select-String -Pattern  $regex -AllMatches)
    $dictionary = @()
    foreach($minfo in $matchInfo)
    {
        foreach($match in @($minfo.Matches | Foreach {$_.Groups[0].value}))
        {
            $element = $match.Substring(1, $match.Length - 2);
        
            [array]$value = $element.Split(','); 
       
            [int]$add = [int]::Parse($value[0]) + [int]::Parse($value[1])

            $dictionary+=$add
        }
    }
    return $dictionary
}

Write-Host "Number of Arg=" $args.Length

foreach($arg in $args)
{
    $twoValues = TwoValues($arg)
    Write-Host "$twoValues"
}

Then you can call the file with .\....ps1 "{5,5}"

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

June 2nd, 2015 8:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics