Hi, I need to get the OU of a user. I have tried various scripts on the web but none work completely.
Here is what I finally arrived at, which works great, but I need to have all 3 commands in a single line.
This is a job for calculated properties. Its pretty simple, check this link out so you can understand the technique. Below is an example that should get you in the right direction.
Like Logan says calculated properties are the way to go.
However, [1…-1] returns something weird because it counts backward from 1 to -1:
1: the OU name
0: the username
-1: the last item of the array which is probably the last member of the FDQN of your company (probably “dc=com”).
Hi Logan, Thanks for your reply and the tip on calculated properties!!!
I see that the number of comas have been hardcoded to 1. Although I need to be able to keep that as a variable as some names contain comma and some do not.
Hence I had ($user.Name -split ‘,’).count instead of 1.
[quote quote=133497]Hi Logan, Thanks for your reply and the tip on calculated properties!!!
I see that the number of comas have been hardcoded to 1. Although I need to be able to keep that as a variable as some names contain comma and some do not.
Hence I had ($user.Name -split ‘,’).count instead of 1.
That [1…] in my code is not “hardcoding the commas”, its indexing into the 2nd item in the array. Since the first index (0) is the CN=User Name. I can see what you are doing in your code, but its pointless and really confusing. Consider what your code is doing…
As you can see splitting it by a comma doesn’t achieve anything because there is no comma to split on, you are just needlessly inconveniencing electrons.
We know the structure of a distinguishedName, so its reasonable to split one into an array based on commas and index into position 1 to omit the user name.
Here is some reading to help in your understanding of working with Arrays and working with strings
Actually there is a comma in most names. I mentioned it in my earlier message too “Although I need to be able to keep that as a variable as some names contain comma and some do not”. Why would I want to reinvent the wheel if there was no comma?
Anyway its all cool as I have it just the way I need it.
[quote quote=133736]Actually there is a comma in most names. I mentioned it in my earlier message too “Although I need to be able to keep that as a variable as some names contain comma and some do not”.
Anyway its all cool as I have it just the way I need it.
[/quote]
I totally missed that post My apologies! Glad you got it working.