Add one variable to another loop

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Add one variable to another loop

Post by thatsameer »

Hi,

Im very confused.

I've read the webbrowser help but haven't got anywhere.

Here is my situaton:

I have 2 WebBrowser objects placed in my Sapien form ($webbrowser1 and $webbrowser 2).

I have a list of links in c:\users\admin\desktop\url.txt (https://google.co.uk and https://bbc.co.uk).

I want each line of the text file to be navigated by each $webbrowser in order.

$url = get-content c:\users\admin\desktop\url.txt
$webbrowser1.navigate($url)

This works if there is only https://google.co.uk in the txt file.

How can I make a kind of foreach loop for both variables, so for each $link in $url navigate to each $webbrowser1, 2, 3, 4 etc?

I need both variables to loop together, i dont know how.

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add one variable to another loop

Post by jvierra »

Just enumerate the contents of the file in a ForEach-Object.

help foreach -online

Read the examples to learn how to use it.

Or use foreach()

help about_foreach
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Re: Add one variable to another loop

Post by thatsameer »

Im sorry but I still don't understand how to make each line from text file .navigate to each sequential $webbrowserX
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add one variable to another loop

Post by jvierra »

You also have to select the browser.

Another way is:

$urls = Get-Content file
$webBrowser1.navigate($urls[0])
$webBrowser2.navigate($urls[1])
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Re: Add one variable to another loop

Post by thatsameer »

Thanks, your replies allowed me to probe further.
I've solved this in a clever way, by creating a new tab for each line in the notepad, adding to the form and adding webbrowser on top. Works very well.

I have another question on the tabcontrol but will post new topic as not very related to this one. thanks
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked