So firstly apologises, this is going to be abit of a long thread to read as i’m going to try be as detailed as possible.
I have been doing Powershell properly for around 6 months I would say and i can pretty much accomplish what i need to do. At the beginning this was fine and enough. I would write my script and it would work so that’s brilliant.
As time has gone by, I’ve seen myself having to revisit scripts, improving them adding in error handling etc… and that is fine. However i have also noticed that i just do not know how to structure my code properly. I also don’t know how to pull out areas of my script and make them into a function and how to call them from other parts of the script.
I have attached the code below - however its not actually that big, instead its been repeated almost 4 times! Why? Because i dont know how to segregate this and pull out the function and call the function in the necessary order.
This is why i would love some advice - be brutal that’s fine - I really want to become a better scripter so all the advice i can get here is great.
The core of what i would like to know is the following:
- How would you pull out the repetitive code below and call it?
- What would you improve in this code?
- What is done very badly in this code.
This is a first draft also - so i’ve literally just got it to work - no error handling, no parameters etc… done yet.
What exactly does the code below do?
Essentially it is checking the permissions for each folder and then ensuring they have particular keywords such as ‘Everyone’ - if they have all the necessary keywords, that is compliant. If they do not, they are not in compliance and something has gone wrong with the permissions.
The child folder should not have ‘Everyone’ and if it does, then that is also out of compliance.
First we are reading from a CSV file a list of project names
We find out which VNX share we are searching 1 or 2 and then call the function (which is a terrible function because its doing all sorts of crap)
If we head over to the function now it contains two switches representing VNX01 and VNX02
The first thing to note is switch 1 is identical to switch 2 - the difference is VNX01 or VNX02 thats it - so i have repeated out code already once here
Next we have two for each statements - one for the parent, one for the child - again they are identical, the child is just looking at the child folder instead
The first foreach statement first checks every folder and outputs the result to a text file. It then checks to see if its matching and if it does it continues and if it doesn’t then its flagged up.
The same concept for child.
The reason there is two foreach statements is because, something weird was going on with $project variable when i moved from parent to child and i wasn’t looping through the list properly so i had to call the foreach statement again to ensure nothing is going weird.
- that is another problem, when code is so big and repetitive its just bad… its bad for maintenance, troubleshooting and just general usage.
Finally - For anyone that takes the time to read this and help me, a massive thank you and very highly appreciated.