Solved

VBS Organisation Units

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now