Submitted By: Marlon Meiklejohn

Lists Active Directory computer accounts. Useful for populating lists for software deployment, patch deployment, etc.

Visual Basic
Edit|Remove
'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'
'
'   Script Name:    GetMachineNames.vbs
'
'   Date:           January 15, 2006
'
'   Purpose:        Pulls object information out of AD
'
'   Author:         Marlon Meiklejohn
'
'
'   Revision History
'   ----------------
'
'

Option Explicit


'   Define Variables
Dim fso
Dim WshShell
Dim oOu
Dim oItem
Dim fsOutputFile
Dim sOutputText
Dim sOutputFileName
Dim sOUPath
Dim sSearch
Dim bRunByCScript
Dim iPos


'   Set up the system variables
Set WshShell = wscript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")


' Check if run from wscript or cscript - Used for debugging.
sSearch = "cscript"
iPos = InStr(Wscript.FullName, sSearch)
If iPos > 0 Then
   bRunByCScript = True
Else
   bRunByCScript = False
End If

'   Define the output file name
sOutputFileName = ".\MachineList.txt"

'   Create the output file
Call CreateOutputFile()

'   Define the OU path
sOUPath = "OU=Production,OU=Workstations,
OU=DivisionName,DC=CompanyName,DC=COM"


'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'
'   Main Logic
'

'   Define the OU object
Set oOu = GetObject("LDAP://" & sOUPath & "")

'   Filter for only the objects you want.
oOu.Filter = Array("Computer")

'   Loop through all objects and grab the
'   information you're looking for.
For Each oItem In oOu

    '   Strip out the "CN=" at the beginning of the line.
    sOutputText = Right(oItem.Name, Len(oItem.Name) - 3)
    
    '   If script was run by CScript, then echo the information to the user.
    If bRunByCScript = True Then
        Wscript.Echo sOutputText
    End If
    
    '   Write to the output file
    Call WriteOutputFile (sOutputText)
Next

'   Close the output file.
Call CloseOutputFile()

'   Notify the user that the script is finished.
WScript.echo "File created: " & sOutputFileName

'   Cleanup and get out.
Call Cleanup()
Wscript.Quit




'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'
'   SubRoutines
'
'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/


'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'
'   Creates the output file.
'
Sub CloseOutputFile()
    fsOutputFile.Close
End Sub




'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'
'   Creates the output file.
'
Sub CreateOutputFile()
    Set fsOutputFile = fso.CreateTextFile(sOutputFileName)
End Sub




'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'
'   Writes to the output file
'
Sub WriteOutputFile(sText)
    fsOutputFile.WriteLine sText
End Sub




'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'
'   Clean up the variables
'
Sub Cleanup()
    On Error Resume Next
    Set fso = Nothing
    Set WshShell = Nothing
    Set oOu = Nothing
    Set oItem = Nothing
    Set fsOutputFile = Nothing
    Set sOutputText = Nothing
    Set sOutputFileName = Nothing
    Set sOUPath = Nothing
    Set sSearch = Nothing
    Set bRunByCScript = Nothing
    Set iPos = Nothing
End Sub



'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'
'   End of Script
'
'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/