Miscbrah Posted March 14, 2013 Posted March 14, 2013 Hi dark-art-of-scripting people. I need a script that'll give me the OU of the user currently logged on to a machine. I've currently got: Set objSysInfo = Createobject("ADSystemInfo") Wscript.Echo "DN of current user: " & objSysInfo.UserName Which echos everything from the username name up, but what I really want echoed is just the first "OU=???" above the CN=%username% part. Anyone able to sort me? Thanks in advance for reading and thinking about it!
LosOjos Posted March 14, 2013 Posted March 14, 2013 (edited) Perhaps the Split() function would help? VBScript Split Function If you want more help with the implementation, please post an example of the result you get (remove any actual names if you want to, just need to see the format of the string you're getting) Edited March 14, 2013 by LosOjos 1
Miscbrah Posted March 14, 2013 Author Posted March 14, 2013 Oooh look at that! Well that certainly looks like what I'll need in there somewhere. I'll take a little down time (if I get any) and get my head around it. Thanks!
Rich Posted March 14, 2013 Posted March 14, 2013 Set objSysInfo = Createobject("ADSystemInfo") strName = objSysInfo.UserName arrDN = Split(strName, ",")'Split the Users Distinguished Name into an array DNCount = Len(arrDN(1)) 'Determines how many characters in the 2nd element of the array which is the where the OU name is (Ex. 'OU=Teachers' would be 11 characters) So DNCount=11 NewDNCount = DNCount - 3 ' This takes the DNCount and subtracts 3. Now we know how many characters there are in the string 'OU=Teachers' ( NewDNCount=8 ) minus the 'OU=' part Wscript.Echo Right(arrDN(1), NewDNCount) 'Now we print 'OU=Teachers' except for the 3 characters at the beginning. So we print 'OU=Teachers' from the Right side but only print 8 characters which leaves us with 'Teachers' You can also create a variable strOU = Right(arrDN(1), NewDNCount) I've also done it this way: Set objSysInfo = CreateObject("ADSystemInfo") strName = objSysInfo.UserName arrUserName = Split(strName, ",") 'Split the User Distinguished Name into array based on commas arrOU = Split(arrUserName(1), "=") 'takes the 'OU=Teachers' and splits it into two parts based on the "=" sign wscript.echo arrOU(1) 'Echos the 2nd element in the two part array which is 'Techers' You can also create a variable from this strOU = arrOU(1) Either way works but it's nice to see two ways to accomplish the same task. I do use the 2nd method to map printers to computers based on their OU name and it works great.
Arthur Posted March 15, 2013 Posted March 15, 2013 Split the User Distinguished Name into array based on commas Splitting based on commas is not a good idea. Unfortunately, though, there’s a potential problem with that approach, a problem revolving around the fact that it’s perfectly permissible to use a comma as part of a user’s CN. Suppose our user has this distinguished name: CN=Ken Myer, Jr.,OU=Finance,OU=North America,OU=Pacific Coast,dc=fabrikam,dc=com
mac_shinobi Posted March 15, 2013 Posted March 15, 2013 (edited) VBScript - Find which OU a user belongs to Const ADS_SCOPE_SUBTREE = 2 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConnection [b]Set objUser = CreateObject("Wscript.Network") FindUser = objUser.UserName [/b]'InputBox("Please Enter A UserName", "Find User OU") If FindUser = "" Then MsgBox("No UserName Was Added") WScript.Quit Else strUser = FindUser End If objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.CommandText = _ "SELECT distinguishedName FROM 'LDAP://[b]dc=yourdomain,dc=com[/b]'WHERE objectCategory='user'AND sAMAccountName='" & strUser & "'" Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst Do Until objRecordSet.EOF strDN = objRecordSet.Fields("distinguishedName").Value arrPath = Split(strDN, ",") intLength = Len(arrPath(1)) intNameLength = intLength - 3 Wscript.Echo "User :" & strUser & vbcrlf & "OU is : " & Right(arrPath(1), intNameLength) objRecordSet.MoveNext Loop Made the relevant sections bold, you need to adjust the LDAP path according to your domain so dc=my,dc=domain,dc=com or whatever it is I have also taken away the InputBox prompt and replaced this with Set objUser = CreateObject("Wscript.Network") objUser.UserName Which returns the currently logged on username correctly, if you use the inputbox and enter the username in wrong then you get an error. Original link that I found that had similar code was here : http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/21/how-can-i-determine-the-ou-a-user-account-belongs-to.aspx Although I could not get it to work as it kept failing on line 10 due to unterminated string etc , didn't have time to look at the code snippet correctly so found the above one which I got working a bit easier. Edited March 15, 2013 by mac_shinobi
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now