0

I'd like to increment the number after M by 1 in "$wshell.SendKeys('M1 ABCD')" every loop based on user input. The first loop will type "M1 ABCD", 2nd "M2 ABCD", 3rd "M3 ABCD" until the script reaches the requested number of loop from user input.

Here is a small part of the script (Pre-reqs not included)

Param(
    [Parameter(
        Mandatory=$true, 
        Position=0, 
        HelpMessage="how many times do you want to run?")
    ]
    $Number
)
$i = 0
while($i -lt $number){                    #Number of times the loop repeats
    Write-host "Loop run #$($i + 1)"
    [Clicker]::LeftClickAtPoint(744,277)
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')
    $wshell.SendKeys('{TAB}')
    Sleep -Milliseconds 600
    $wshell.SendKeys('M1 ABCD') 
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')                                                      
    $i++
}
1
  • Just change $wshell.SendKeys('M1 ABCD') to $wshell.SendKeys("M$($i++) ABCD") (note the double quotes!) Commented Mar 27, 2021 at 13:11

2 Answers 2

1

Try this:

Param(
    [Parameter(
        Mandatory=$true, 
        Position=0, 
        HelpMessage="how many times do you want to run?")
    ]
    $Number
)
$i = 1
while($i -le $number){                    #Number of times the loop repeats
    Write-host "Loop run #$($i)"
    [Clicker]::LeftClickAtPoint(744,277)
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')
    $wshell.SendKeys('{TAB}')
    Sleep -Milliseconds 600
    $wshell.SendKeys("M$i ABCD") 
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')                                                      
    $i++
}

Note that I've started the loop with 1 rather than 0, and made a few corresponding adjustments. The $i inside the double quotes will be evaluated each time through to loop.

Sign up to request clarification or add additional context in comments.

Comments

0

I came up with a work around, but isn't the most efficient.

I broke down send.keys into multiple sections and added an increment separate from the string.

Param(
    [Parameter(
        Mandatory=$true, 
        Position=0, 
        HelpMessage="how many times do you want to run?")
    ]
    $Number
)
$a = 1
$i = 0
while($i -lt $number){                    #Number of times the loop repeats
    Write-host "Loop run #$($i + 1)"
    [Clicker]::LeftClickAtPoint(744,277)
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')
    $wshell.SendKeys('{TAB}')
    Sleep -Milliseconds 600
    $wshell.SendKeys('M1')
    $wshell.SendKeys($a)
    $wshell.SendKeys('ABCD') 
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')                                                      
    $i++
    $a++
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.