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+ Site2+ Site3+ Site4+ 

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 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 Username: Password: Site1 Site2 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