SOLVED

Script from 2 csv - URL list and Folder list

%3CLINGO-SUB%20id%3D%22lingo-sub-2596541%22%20slang%3D%22en-US%22%3EScript%20from%202%20csv%20-%20URL%20list%20and%20Folder%20list%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2596541%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20there!%3C%2FP%3E%3CP%3EI%20have%202%20csv%20files.%26nbsp%3B%3C%2FP%3E%3CP%3Eone%20has%2082%20rows%20with%20unique%20site%20address.%20like%20below%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22rjack_0-1627678486711.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F299593i51C55FF4F8D0A462%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22rjack_0-1627678486711.png%22%20alt%3D%22rjack_0-1627678486711.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20other%20has%201314ish%20folder%20names%20with%20path.%20like%20below%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22rjack_1-1627678486799.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F299594i44341F9C4C3E6BB7%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22rjack_1-1627678486799.png%22%20alt%3D%22rjack_1-1627678486799.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPreviously%2C%20I%20used%20to%20create%201000s%20of%20folder%20per%20site%20per%20script%20and%20now%20I%20have%20to%20create%20the%20same%20set%20of%20folders%20on%2082%20sites.%20so%20was%20trying%20to%20improvise%20the%20script%20by%20importing%20a%20csv%20and%20added%20'for%20each'.%20the%20script%20works%20but%20skips%20all%20sites%20and%20goes%20to%20the%20last%20site%20in%20the%20list%20and%20creates%20the%201314%20folders%20with%20correct%20permission.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ebelow%20is%20what%20I%20added%20to%20the%20script%20trying%20to%20trigger%20the%20loop.%26nbsp%3B%20Please%20help!%3C%2FP%3E%3CP%3EI%20have%20attached%20the%20full%20script%20as%20well.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24csvs%3D%20Import-CSV%20%24csvfilepathsite%0Aforeach%20(%24row%20in%20%24csvs)%0A%7B%0A%24url%20%3D%20%24row.siteurl%0A%24GroupNames%3D%24row.GroupNames%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2596541%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%20Office%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2605273%22%20slang%3D%22en-US%22%3ERe%3A%20Script%20from%202%20csv%20-%20URL%20list%20and%20Folder%20list%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2605273%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%26nbsp%3BThank%20you%20for%20pointing%20the%20issue.%20moved%20the%20foreach%20into%20the%20function%20and%20its%20working%20like%20clockwork.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOnly%20problem%20is%20intermittently%20I%20get%20429%20but%20what%20can%20we%20do%20if%20server%20stops%20responding.%20or%20can%20we%20add%20something%20like%20'wait%20until%20response'%20(if%20it%20exists)%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ewarning!%20I%20have%20zero%20knowledge%20in%20coding%20and%20scripts.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2597092%22%20slang%3D%22en-US%22%3ERe%3A%20Script%20from%202%20csv%20-%20URL%20list%20and%20Folder%20list%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2597092%22%20slang%3D%22en-US%22%3E%3CP%3EI%20took%20a%20quick%20sneak%20peek%20at%20your%20code%20and%20I%20don't%20think%20it%20will%20work.%3C%2FP%3E%3CP%3EYour%20code%20is%20structure%20like%20this%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EForeach%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20Variable%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BFunction%20DoSomething%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BFunction%20DoSomethingMore%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3EThis%20is%20not%20correct%2C%3C%2FP%3E%3CP%3E-%20Don't%20place%20the%20Function%20inside%20the%20Foreach%2C%20Foreach%20should%20be%20inside%20the%20function.%3C%2FP%3E%3CP%3E-%20If%20you%20need%20the%20Function%20to%20respone%20to%20a%20value%2C%20you%20need%20to%20set%20parameter%20to%20the%20function%2C%20something%20like%20DoSomething%20-FolderName%20%24URL%2C%20and%20define%20the%20FolderName%20in%20the%20Function.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20the%20function%20are%20not%20needed%2C%20just%20remove%20them%20and%20keep%20the%20code%20of%20the%20function%20inside%20the%20foreach.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ealso%20you%20can%20use%20ISE%20and%20debug%20your%20code%20and%20you%20will%20see%20that%20the%20function%20block%20is%20not%20actually%20executed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E-------------------%3C%2FP%3E%3CP%3EHope%20this%20help%2C%20if%20this%20answer%20help%2C%20please%20mark%20this%20answer%20as%20Best%20Reponse.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello there!

I have 2 csv files. 

one has 82 rows with unique site address. like below

rjack_0-1627678486711.png

 

 

the other has 1314ish folder names with path. like below

rjack_1-1627678486799.png

 

Previously, I used to create 1000s of folder per site per script and now I have to create the same set of folders on 82 sites. so was trying to improvise the script by importing a csv and added 'for each'. the script works but skips all sites and goes to the last site in the list and creates the 1314 folders with correct permission. 

 

below is what I added to the script trying to trigger the loop.  Please help!

I have attached the full script as well. 

 

$csvs= Import-CSV $csvfilepathsite
foreach ($row in $csvs)
{
$url = $row.siteurl
$GroupNames=$row.GroupNames

 

2 Replies
best response confirmed by rjack (Occasional Contributor)
Solution

I took a quick sneak peek at your code and I don't think it will work.

Your code is structure like this

 

Foreach{

  Variable

     Function DoSomething{

         Function DoSomethingMore{

        }

      }

}

This is not correct,

- Don't place the Function inside the Foreach, Foreach should be inside the function.

- If you need the Function to respone to a value, you need to set parameter to the function, something like DoSomething -FolderName $URL, and define the FolderName in the Function.

 

If the function are not needed, just remove them and keep the code of the function inside the foreach.

 

also you can use ISE and debug your code and you will see that the function block is not actually executed.

 

-------------------

Hope this help, if this answer help, please mark this answer as Best Reponse.

 

@farismalaeb Thank you for pointing the issue. moved the foreach into the function and its working like clockwork. 

 

Only problem is intermittently I get 429 but what can we do if server stops responding. or can we add something like 'wait until response' (if it exists)? 

 

warning! I have zero knowledge in coding and scripts.