Problem getting CSV data in my foreach loop

Hi there people!

I have a somewhat unusual script im working on.

I have to find a specific fontawesome script across a bunch of websites handled through the MainWP control system for wordpress sites.

The goal is to deactivate the caching plugin (rocket, at is masks the code in minification), search for the script and catch that, and then to reactivate this plugin again.

The script does work quite well, but, for now only when the CSV file have a single line.

As soon as several lines are present, the values “csvid and csvurl” is seen as one, long, value each.
Which natually cant work.

The system needs an ID of the site to identify it in MainWP, and it need the URL address to make the invoke-restmethod.

I cant seem to figure out why it will not identify each line, as being one object of the loop.
The “write progress” part, so show me a load bar, can identify the different lines with no issue.

I would really appreaciate if someone here can help me figure out what i’m not seeing.

Here is a link to the script:

I don’t see anything glaring in your code to explain this which means it could be the data. Can you provide example data that causes this issue?

Hi Ralphmwr!

I just tried a different approach, which sort of works, but not really.

I have changed the Gist.

The input from the CSV is only as such:

235,domainone.com
355,domaintwo.com

And, well, now it runs, but it just loops the last line in the CSV file, four times?

My write-host output is this:

Now Processing: 977
Deactivation complete
Search complete
activation complete
Now Processing: 977
Deactivation complete
Search complete
activation complete
Now Processing: 977
Deactivation complete
Search complete
activation complete
Now Processing: 977
Deactivation complete
Search complete
activation complete

And the final output is just the same site 4 times.

Any ideas?

Okay hold on :smiley:

I now just changed to foreach ($data in $csv)

Now it still loops, BUT, only twice per output

Like so:

Now Processing: 1326
Deactivation complete
Search complete
activation complete
Now Processing: 1326
Deactivation complete
Search complete
activation complete
Now Processing: 977
Deactivation complete
Search complete
activation complete
Now Processing: 977
Deactivation complete
Search complete
activation complete

Hmm it seems, that foreach sees the value of the CSV af being “for each comma”

Must explain it…

How do i make it understand that i only want to loop for each line?

Alright taking a harder look at your code I have some questions. Did you write this or are you modifying something already written? I ask because there are some peculiar things that don’t make sense and some, not all, could be causing this issue.

  1. On line 10 you write out the entire path that you just assigned to a variable on line 9. Why not just use the variable?
  2. On line 26 you reference the variable $data but it is never defined in the script. What is $data and where did you assign it?
  3. On line 28 you create a nested loop to iterate over the exact same data you are already iterating over on line 24. This will cause your multiple data issue. Why did you write this nested loop?

Well, it is rather messy i wont deny that.

  1. i need the $csvnosort in define in line 29 (for $myoutput) is does not want to recongnite $csv

  2. $data is the value taken from $csv to loop over.
    Output of data looks like this = @{id=1326; domainurl=https://domainname.dk/} (which sort of is a line)

  3. if this $myoutput section is not defined then my output wont be caught for the $myOutput | Out-GridView or the export-csv

Maybe i need to use foreach-object for this instead?

i have updated the gist to latest mini-corrections

Also to answer your earlier question, this is a combination of 2 of my other scripts that have had very different functions.
I figured it easiest to recycle some of these two, im sure that shows :slight_smile:

So, currently the output is like this:

Now Processing: 1326
Deactivation complete
Search complete
activation complete
Now Processing: 1326
Deactivation complete
Search complete
activation complete
Now Processing: 977
Deactivation complete
Search complete
activation complete
Now Processing: 977
Deactivation complete
Search complete
activation complete
(console wise)

CSV output wise:
WebsiteID, pluginpath, deactivation, activation, statusdescriptionvalue, domainname, iscodepresent

1326 wp-rocket/wp-rocket.php Process ran. Process ran. 200 SUCCESS https://domainone/ False
1326 wp-rocket/wp-rocket.php Process ran. Process ran. 200 SUCCESS https://domainone/ False
977 wp-rocket/wp-rocket.php Process ran. Process ran. 200 SUCCESS https://domaintwo/ True
977 wp-rocket/wp-rocket.php Process ran. Process ran. 200 SUCCESS https://domaintwo/ True

It’s running like this because you have a nested foreach loop. Remove that and do everything in the main loop.

Hmm, i just tried that, im guessing you refer to line 27:
$myOutput += foreach ($line in (Get-Content $csvnosort)) {

If i remove this i wont catch the data i need.

How would you do this differently?

  • i am quite a novice so my syntax understanding is still in early development stage :smiley:

I figured it out!

I changed the separat nested loop to be included in the main like so:
$myOutput += foreach ($data in $csv) {

I updated the Gist to show how :slight_smile:

Thanks alot for your input!

1 Like