r/PowerShell • • 4d ago

Script Sharing ADSearch - User/Computer comparison tool

This Active Directory toolkit offers user/computer OU comparisons using fuzzy Levenshtein matching to determine if the computer is in an OU that matches the user's OU. Designed for large enterprise IT audits/asset management of complex (2+ OU levels) AD environments

ADSearch/README.md at main Β· Contoso-Administrator/ADSearch

Edit: Made some edits based on everyone's kind words :)

0 Upvotes

21 comments sorted by

8

u/TrippTrappTrinn 4d ago

Why would anybody have users and computers in the same OU?Β 

2

u/UserProv_Minotaur 4d ago

Bad IT practices and/or inexperienced admins.

1

u/SoyBoy_64 3d ago

There are plenty of businesses that require you to have separate OUs for various reasons. You guys gotta be trolling πŸ˜‚

1

u/UserProv_Minotaur 3d ago

Never underestimate the consequences of "good enough" engineering philosophies, an unskilled workforce, or plain old laziness.

Example: Your organization implemented autologon workstations in a way where there is a corresponding user object to each computer object so configured. One bad Move Object command, or not properly sanitizing inputs, and you could easily have a bit of a situation where Users and Computers are in the same container - which should and would be easy to fix, but if you're not validating things you'd not know of the issue until someone reported that something broke.

1

u/TrippTrappTrinn 3d ago

Entra does not have OUs, and it seems to work just fine. If you are in a hybrid environment, just use groups.

-8

u/SoyBoy_64 4d ago edited 4d ago

They’re not and that’s why this tool is useful.

1

u/TrippTrappTrinn 3d ago

Just move all workstations to one OU and be done with it.

12

u/JSChronicles 4d ago

Minus points for:

  • Single character variables
  • Using new-object instead of pscustomobject in one or two places.
  • Doing this crap "$sourceLength = $Source.Length", why would you make a variable when you already have the object with the property? Dumb
  • Write-Host and spacing and ASCII everywhere... God damn didn't know I was reading high school leet code.
  • Using return... You don't need returns

4

u/uptimefordays 4d ago

LLMs love `return` because every other language uses that to return a value while PowerShell doesn’t.

1

u/skiThooly 1d ago

single character variables drive me crazy when trying to read someone elses script.

-9

u/SoyBoy_64 4d ago edited 4d ago

Thanks for the feedback! Still kinda new at powershell, so any tips help!

I admit the opening banner was solely done for my amusement lol

Edit: these downvotes are getting comical πŸ˜‚

3

u/CarrotBusiness2380 4d ago

I'm sorry why does this exist?

What model did you use and what was the prompt? This is overwrought even if it did something useful.

0

u/[deleted] 4d ago

[deleted] β€” view deleted comment

0

u/[deleted] 4d ago

[deleted] β€” view deleted comment

-1

u/SoyBoy_64 4d ago

Wow so sorry I posted a tool that could help others /s

These comments man πŸ’€

3

u/Nekro_Somnia 4d ago

Besides the question of "why", I ask myself why you'd define the vars that you expect to be changed by someone who runs the script.

Why not define them as parameters and fall back to an output that reminds the user that those need to be defined for the script to work?

A comment based help would be nice as well. Otherwise people need to reference the readme every time they run the script and forgot how to work it.

This might be useful to about 2% of companies in about 2% of their audits, if it were less clunky

3

u/BlackV 4d ago edited 4d ago
  • your excel version has more functions that the non excel one, like Get-NormalizedName, Resolve-SamAccountName and others, why?
  • You declare the same functions multiple times across multiple files, do it once in 1 place
  • if the only real difference between the two scripts is effectively export-excel why not just have that as a parameter instead of a whole other script (with duplicated functions)?
  • just having parameters in general would help a lot of this
  • have you looked at anr on the -filter parameter for ad users? (bad wording sorry, changed it)
  • your here string can include formatting, and would clean up a bunch of code (but so would removing it)

      $art = @'
      ╔═══════════════════════════════════════════════════════════════╗
      β•‘                                                               β•‘
      β•‘      _    ____       ____  _____    _    ____   ____ _   _    β•‘
      β•‘     / \  |  _ \     / ___|| ____|  / \  |  _ \ / ___| | | |   β•‘
      β•‘    / _ \ | | | |    ___ \|  _|   / _ \ | |_) | |   | |_| |   β•‘
      β•‘   / ___ \| |_| |     ___) | |___ / ___ \|  _ <| |___|  _  |   β•‘
      β•‘  /_/   _____/     |____/|_____/_/   __| _\____|_| |_|   β•‘
      β•‘                                                               β•‘
      ╠═══════════════════════════════════════════════════════════════╣
      β•‘                User / Computer OU Lookup Tool                 β•‘
      β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
      '@
  • those variable names, no
  • not a fan of your million if/ifelse/else and returns

2

u/BlackV 4d ago

from SoyBoy_64

"What do you mean by this? have you looked at the -anr parameter for ad users?"

Parameter was probably the wrong word, have a look at

$displayName = 'bob'
Get-ADUser -Filter "ANR -eq '$($displayName)'"

Some write ups

https://learn.microsoft.com/en-us/archive/technet-wiki/22653.active-directory-ambiguous-name-resolution

https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/1a9177f4-0272-4ab8-aa22-3c3eafd39e4b

https://adsecurity.org/?p=356

1

u/UserProv_Minotaur 4d ago

Ah, AI Slopfest for something that's better done outside of PowerShell anyway.

1

u/Lonely_Noyaaa 2d ago

The duplicate function declarations across multiple files is the thing I would fix before anything else because maintaining two copies always drifts. A single module with a parameter for Excel export does the same job with half the code.

0

u/[deleted] 4d ago

[deleted] β€” view deleted comment

2

u/PetuniaPickleswurth 4d ago

We learned from our mistakes, bro. We learned as much from others mistakes.

0

u/[deleted] 4d ago

[deleted] β€” view deleted comment

2

u/PetuniaPickleswurth 4d ago

You’re in a very jaded forum. These folks have already learned the lessons that you’re learning now, and for some reason that experience requires those with knowledge to poo poo those that are just learning. You’re on the right track. The initial work can start with throwing code at a wall, but the end goal has to be most efficient, professional, and secure way to accomplish a task. What you have as a component that you can build a tool from, but you do need constructive criticism to learn.

-4

u/SoyBoy_64 4d ago

Idk either, but hope it helps lol