powershell -match stopped working.

Ronald Clifford 21 Reputation points
2022-07-09T19:28:46.977+00:00

I've been working with this script most of the day. All of a sudden it stopped working. The script below
is not the greatest coding. I'm just trying to get the output correct then fix the syntax. But the failure is in
the foreach loop where it is searching thru $results1 looking for "toolpath". Not sure why it stopped working.
Even when I replaced $line with "toolpath". It still fails.
What is a tag? Having problem posting.

$regDocs="HEIGHT|RETRACT|THICKNESS|WIDTH|ZERO|grid_spacing"
$regFonts="font_height|id|geometryType"
$regTools1="diameter|Finish_Allowance|coner_radius|length|overall_length| Plungerate|Slot_depth|slot_feedrate|surfacing_feedrate|
surfacing_stepover"
$regtools="end_depth|start_depth|step|tab|tolerance"
$regSpeeds = "feedrate|plungerate"
$reguuids = "uuid"
$FD=0
$RPM=0
$tool=0
$i=0
$file=$args[0] + ".c2d"
If (-not(test-path $file)) {
write-host "$file File not found"
exit
}

write-host $file

$results1= "test|test1|test2"

$results

Display($results)
AddInch $results ""

$results1

write-host $results1.length

for ( $i=0; $i -le $results1.length) {

foreach ($line in $result1) {

$line = "toolpath"

write-host $line
if ($line -match "toolpath_group") {
for ($x=0; $x -eq 3;$x++){
$data = $results1[$i].split(":")
write-host $data[0] " " $data1
}
$i++
continue
}
if ($line -match "toolpath" ) {
$i++

write-host "calling Addinch" $i $line

AddInch($regtools1," ")

}
$i++
If ($line -match "model") {
exit
}
}

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,906 Reputation points
    2022-07-11T02:45:43.117+00:00

    Line 22 declares a string variable: $results1 = "test|test1|test2", and on line 31 you have a "for" loop with no increment: for ( $i = 0; $i -le $results1.length) {, and on line 32 you have a foreach: foreach ($line in $result1) { .

    $result1 is a scalar not a list, so that foreach on line 32 will run once.

    Line 35 if ($line -match "toolpath_group") { will never be true because the string "test|test1|test" is never going to be equal to "toolpath group". The same is true for the conditional statements on lines 43 and 49.

    On lines 40, 44,48 $i++ you're modifying the control variable of the "for" loop on line 31. You shouldn't be doing that.

    And because none of the conditions in the "foreach" loop on line 32 will ever be true, the $i for loop control on line 31 will be incremented only on line 48. The loop on line 31 will probably run 16 times (the number of characters in the $results1 variable) and accomplish nothing.

    Given all that, I don't know what you're trying to do, but I can't see how that code ever produced anything!

    0 comments No comments