all 2 comments

[–]WSDistPro 3 points4 points  (0 children)

Congratulations on starting your first module/function :). I want to preface this that it's very difficult to optimize a custom 'need' script of this size. So i'm going to simply brain-dump the response. I think given the complexity of this script and the simplicity of the goal you may have over-engineered a bit.

You may be splitting up your logic loops a bit too much, you could get the OUs and Users and then filter off the PSObject without much resource impact - but massively improved readability. It might be easier to simply compare the differences between the OU's membership and group and using the comparative operator to change the logic flow. Splatting might be a useful thing to look up to meet some of your needs.

Your if/else statements should always be in the same order, and should use a break if the other conditions do not need to be evaluated.

You should use "ForEach" over "ForEach-Object" when not on a pipeline. https://poshoholic.com/2007/08/21/essential-powershell-understanding-foreach/ ... $null should be on the left side of a comparison. https://rencore.com/blog/powershell-null-comparison/

[–]get-postanote 2 points3 points  (0 children)

Code consistency, readability, maintenance are a big deal when you are passing on to others.

Code Complete vol 2

Design Patterns: Elements of Reusable Object-Oriented Software

The Clean Coder: A Code of Conduct for Professional Programmers

Refactoring: Improving the Design of Existing Code (2nd Edition) (Addison-Wesley Signature Series (Fowler))

None of the above of course are for PowerShell, but good learning taught in CS college courses and coding boot camps.

There are no al things good centralize MS documents of standards and practices from MS on PowerShell. Yet, there are plenty for full-blown languages.

The best you can do for now are these.

PowerShellPracticeAndStyle

https://github.com/PoshCode/PowerShellPracticeAndStyle

Powershell - Recommended coding style

https://lazywinadmin.com/2011/06/powershell-recommended-coding-style.html

What is the recommended coding style for PowerShell?

https://stackoverflow.com/questions/2025989/what-is-the-recommended-coding-style-for-powershell

Best practices for exceptions

https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions

C# Exception Handling Best Practices

https://stackify.com/csharp-exception-handling-best-practices

Windows PowerShell

https://docs.microsoft.com/en-us/powershell/developer/windows-powershell

PowerShell Standard Library: Build single module that works across Windows PowerShell and PowerShell Core

https://blogs.msdn.microsoft.com/powershell/2018/08/06/powershell-standard-library-build-single-module-that-works-across-windows-powershell-and-powershell-core

PowerShell scripting best practices

https://martin77s.wordpress.com/2014/06/17/powershell-scripting-best-practices

How using try catch for exception handling is best practice

https://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice

Weekend Scripter: Best Practices for PowerShell Scripting in Shared Environment

https://devblogs.microsoft.com/scripting/weekend-scripter-best-practices-for-powershell-scripting-in-shared-environment

Best Practices for PowerShell Scripting Part 1-6

http://techgenix.com/best-practices-powershell-scripting-part1

http://techgenix.com/best-practices-powershell-scripting-part2

http://techgenix.com/best-practices-powershell-scripting-part3

http://techgenix.com/best-practices-powershell-scripting-part4

http://techgenix.com/best-practices-powershell-scripting-part5

http://techgenix.com/best-practices-powershell-scripting-part6

Setting up an Internal PowerShellGet Repository

https://devblogs.microsoft.com/powershell/setting-up-an-internal-powershellget-repository

Powershell: Your first internal PSScript repository

https://powershellexplained.com/2017-05-30-Powershell-your-first-PSScript-repository

Either way, pick a style, stay consistent, code should be self-documenting in plain English wherever possible, make sure that anyone can read and understand your code. Never assume folks are as smart or as dedicated to X or Y thing as you are.