?
Solved

How to extract just the OU from vb Script

Posted on 2009-05-01
10
Medium Priority
?
398 Views
Last Modified: 2012-06-21
I have this snippet of VB Code:

Which produces the msgbox output.
(See Attachment)
My question is, how can I extract JUST the OU name....WVDP in this case.
Is there a variable that could return just the OU=WVDP portion?

Thanks in advance.

Cheers
Dim wshShell, wshNetwork
Dim strComputerName
' Create Global Objects
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
' Initialize Variables
strComputerName = wshNetwork.ComputerName
wscript.echo "Computer DN: " & GetDN
Function GetDN()
' Use the NameTranslate object to convert the NT name of the computer to
' the Distinguished name required for the LDAP provider. Computer names
' must end with "$". Returns comma delimited string to calling code.
Dim objTrans, objDomain
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Set objTrans = CreateObject("NameTranslate")
Set objDomain = getObject("LDAP://rootDse")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
& strComputerName & "$"
GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
'Set DN to upper Case
GetDN = UCase(GetDN)
End Function

Open in new window

ScreenShot001.bmp
0
Comment
Question by:andeporter
  • 5
  • 4
10 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24282153

If it were the part directly above you could have used Parent. As it is you're better doing a bit of a split.

strComputerName = wshNetwork.ComputerName
WScript.echo "Computer DN: " & GetDN
WScript.Echo "OU: " & Split(strComputerName, ",")(3)

If you wanted just the name there:

WScript.Echo "OU: " & Replace(Split(strComputerName, ",")(3), "OU=", "")

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24282171

Sorry, there's a mistake above. Corrected here:

strComputerName = wshNetwork.ComputerName
strComputerDN = GetDN
WScript.echo "Computer DN: " & strComputerDN
WScript.Echo "OU: " & Split(strComputerDN, ",")(3)

Or just the OU name again:

WScript.Echo "OU: " & Replace(Split(strComputerDN, ",")(3), "OU=", "")

Chris
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 24282202
This will look at the last OU or CN name before the domain name section...

Dim wshShell, wshNetwork
Dim strComputerName
' Create Global Objects
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
' Initialize Variables
strComputerName = wshNetwork.ComputerName
strDN = GetDN
wscript.echo "Computer DN: " & strDN
wscript.echo "OU: " & GetOU(strDN)
Function GetDN()
' Use the NameTranslate object to convert the NT name of the computer to
' the Distinguished name required for the LDAP provider. Computer names
' must end with "$". Returns comma delimited string to calling code.
Dim objTrans, objDomain
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Set objTrans = CreateObject("NameTranslate")
Set objDomain = getObject("LDAP://rootDse")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
& strComputerName & "$"
GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
'Set DN to upper Case
GetDN = UCase(GetDN)
End Function
 
function GetOU(DN)
    strValue = trim(mid(DN,1,instr(DN,",DC=")-1))
    for n = len(strValue) to 1 Step -1
        if mid(strValue,n,1) = "=" then
            exit for
        end if
        strReturnValue = mid(strValue,n,1) & strReturnValue
    next
    GetOU = strReturnValue
End function

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Author Comment

by:andeporter
ID: 24282208
Hmm..syntax error maybe? I'm getting:
ScreenShot002.bmp
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24282226

Yeah, sorry, that was the mistake. I forgot to fix the variable name. The second version will be fine :)

That's this one:

strComputerName = wshNetwork.ComputerName
strComputerDN = GetDN
WScript.echo "Computer DN: " & strComputerDN
WScript.Echo "OU: " & Split(strComputerDN, ",")(3)

Or just the OU name again:

WScript.Echo "OU: " & Replace(Split(strComputerDN, ",")(3), "OU=", "")

Chris
0
 
LVL 6

Author Comment

by:andeporter
ID: 24282242
Maybe I should have said that my effort here is to get the OU=WVDP and set it equal to a variable in order to use it against a case statement for mapping network drives....just to clarify.
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1000 total points
ID: 24282278

Cool, then we would do that instead of echoing it. Like...

strComputerDN = GetDN
strOU = Split(strComputerDN, ",")(3)

You know, it might be of interest... You don't need NameTranslate to get the computer DN if this is a logon script. This works as well and is much shorter :)

Set objADSysInfo = CreateObject("ADSystemInfo")
strComputerDN = objADSysInfo.ComputerName

Then we can continue on and get the part you're after:

strOU = Split(strComputerDN, ",")(3)

The user is accessible in the same way using the same object:

strUserDN = objADSysInfo.UserName

Chris
0
 
LVL 6

Author Comment

by:andeporter
ID: 24282317
Chris,

That worked great. I appreciate your help!
0
 
LVL 6

Author Closing Comment

by:andeporter
ID: 31597500
Great Help
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24282765

You're welcome :)

Chris
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question