Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
while running this in PowerShell

$command1 = 'git shortlog branch..branch1 --oneline --pretty="%an committed %h %s"'
$A=iex $command1


Write-Host "Starting"

Write-Host "testing "
foreach($i in $A)
{$pos = $i.IndexOf(":")
$leftPart = $i.Substring(0, $pos)
$rightPart = $i.Substring($pos+1)
$output += $rightPart
}
$output

Write-Host "SEPERATED"

$c=$rightPart|Select-Object -Unique
$c


Getting below error
Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At line:10 char:1
+ $leftPart = $i.Substring(0, $pos)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException


What I have tried:

We have tried with
foreach($i in $A)
but still we are seeing the same error
Posted
Updated 19-Jan-22 19:49pm

1 solution

The error message is clear.
The second argument (length) to Substring() is out of range, specifically it is negative.
You have provided $pos as second argument, so the error message is telling you that $pos is negative at that point.
You set $pos from a call to IndexOf(), and IndexOf() returns -1 if the search character is not found....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900