Solved

VBS Organisation Units

Posted on 2004-10-04
2
137 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

831 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