ForEach is pulling extra results.

I have a list of site names that are being matched to URLs from a file. If there’s a more efficient way to do this, I’m all ears.

The contents of the text file looks like this:

Site1+http://www.site1.com Site2+http://www.site2.com Site3+http://www.site3.com Site4+http://www.site4.com 

Code block:

$siteurls = get-content .siteurls.txt foreach ($site in $sites) { [string]$content = $siteurls | select-string -pattern "^$site" $use = @($content.split('+')) $name = $use[0] $url = $use[1] $instruction = $use[2] if ($use[2]) { "$name`n$url`n$instruction`n`n" } else { "$name`n$url`nUsername: `nPassword: `n`n" } } 

So what the script should do is spit out a nice list of each site name with corresponding URL plus a blank username and password field to be filled in later, like this:

Site1 http://www.site1.com Username: Password: 

However, I’m getting extras at the end. It doesn’t stop at Site4, it wraps around again and starts over with Site1. The end of the output looks more like this:

Site4 http://www.site4.com Username: Password: Site1 http://www.site1.com Site2 http://www.site2.com Site3 Site4 

I’m scratching my head, can’t figure out why it’s spitting out more than is actually there.

submitted by /u/bsnotreallyworking
[link] [comments]

Leave a Reply