Solved

VBS Organisation Units

Posted on 2004-10-04
2
135 Views
Last Modified: 2010-05-02
Hi there,

I was wondering if anyone knows the script i need for a case statement depending on what OU a 'machine' is in, in my AD.

I have a VBS script that maps the printer but i need a new script for every printer the company has. So what i want is a script to do the following:

Ask what OU the local machine is in
Case in OU 'Group 1' map Printer1
Case in OU 'Group 2' map printer 2
....and so on.

So what i really need is the case statement and details on how it would identify what OU a machie is in.

Thanks very much.
0
Comment
Question by:clivecrawdevil
2 Comments
 
LVL 3

Accepted Solution

by:
domj earned 250 total points
ID: 12221379
This should work for you:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim ArgObj
Dim objEnv ' as collection
Dim objUser 'As IADsUser
Dim objMailbox 'As CDOEXM.IMailboxStore
Dim sUserLDAPName 'As String
Dim DCServer 'As String

 

' Get the Arguments object
Set ArgObj = WScript.Arguments

 

If ArgObj.Count < 1 Then
   Set WNetwork = CreateObject("WScript.Network")
      strComp=WNetwork.ComputerName

else
   strComp = UCase(ArgObj(0))
End If

sCompLDAPName = QueryActiveDirectory()

Select Case True
Case Instr(sCompLDAPName,"ou={InsertOUtoCompare}")>0
      'Action to complete if true
Case Instr(sCompLDAPName,"ou={InsertOUtoCompare}")>0
      'Action to complete if true
Case Else
      'Action to complete if none are true
End Select


Public Function QueryActiveDirectory()

set fso=CreateObject("Scripting.FileSystemObject")
set fsoFile=fso.CreateTextFile("C:\export.csv",True)
 
    Dim oAD 'As IADs
    Dim oGlobalCatalog 'As IADs
    Dim oRecordSet 'As Recordset
    Dim oConnection 'As New Connection
    Dim strADsPath 'As String
    Dim strQuery 'As String
    Dim strUPN 'As String

 

    set oRecordSet = CreateObject("ADODB.Recordset")
    set oConnection = CreateObject("ADODB.Connection")

 

    'Determine the global catalog path
    Set oAD = GetObject("GC:")
    For Each oGlobalCatalog In oAD
        strADsPath = oGlobalCatalog.AdsPath
    Next
    'Initialize the ADO object
    oConnection.Provider = "ADsDSOObject"
    'The ADSI OLE-DB provider
    oConnection.Open "ADs Provider"
    'Create the search string
    strQuery = "<" & strADsPath & _
      ">;(&(objectCategory=computer)(cn=" & strComp & "));cn,distinguishedName,mail;subtree"  
        'Execute the query

    Set oRecordSet = oConnection.Execute(strQuery)
    If oRecordSet.EOF And oRecordSet.BOF Then    
       'An empty recordset was returned
      msgBox "No records found"
    Else    'Records were found; loop through them
        While Not oRecordSet.EOF
            QueryActiveDirectory = oRecordSet.Fields("distinguishedName")
            oRecordSet.MoveNext
        Wend
    End If
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dom
0
 

Author Comment

by:clivecrawdevil
ID: 12309562
Thanks very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vba find the last empty column 10 88
How to Add / Edit Windows Menu 4 52
Added a column screws up code 5 50
Problem to With line 4 43
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now