lunes, 11 de abril de 2011

Script: List Active Directory computer objects

A la hora de analizar la cantidad de maquinas presentes en un dominio y poder conocer sus caracteristicas rapidamente, nada me ha dado mejor resultado que este script que he ido "tuneando" y mejorando a lo largo de los diferentes usos.
Espero este sea el primer post de una serie de scripts que le sean útiles al resto de la comunidad.

Aqui va:

'*******************************************************************
'*******************************************************************
'List computers to txt File
'Version: 0.3
'Author: Pablo Pagani
'Change Log:
'       V2: Format of the file changed to csv
'    v3: Added more properties
'*******************************************************************
'*******************************************************************


Const ADS_SCOPE_SUBTREE = 2
'*******************************************************************
'Definir el nombre del dominio
strDomain = "contoso.ccf"
'*******************************************************************

Set Shell = CreateObject("WScript.Shell")

'*******************************************************************
file=Shell.ExpandEnvironmentStrings("%temp%") &"\Computers.csv"
'*******************************************************************

Set Fso = CreateObject("Scripting.FileSystemObject")

If Fso.FileExists(file) Then
    Fso.DeleteFile file, True    'True forces override of Read-Only
end if

Set TxtS0 = Fso.CreateTextFile(file, ForWriting)

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, Location, operatingSystem, operatingSystemServicePack, operatingSystemVersion, whenCreated,

whenChanged, description from 'LDAP://" & strDomain & "' " _
        & "Where objectCategory='computer'" 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

'********* HEADING *************************
line= "Computer; Location; SO; SO SP; SO Version; When Created; When Modified; Description" & vbcrlf

'********** DETAIL ************************
Do Until objRecordSet.EOF
    line = line & objRecordSet.Fields("name").Value & ";" & objRecordSet.Fields("location").Value & ";" _
    & objRecordSet.Fields("operatingSystem").Value & ";" & objRecordSet.Fields("operatingSystemServicePack").Value & ";" _
    & objRecordSet.Fields("operatingSystemVersion").Value & "; " & ";" & cstr(objRecordSet.Fields("whenCreated").Value) & ";" _
    & objRecordSet.Fields("whenChanged").Value & ";" & vbcrlf

'& objRecordSet.Fields("description").Value

    objRecordSet.MoveNext
Loop


TxtS0.WriteLine line
TxtS0.Close

set shell = nothing

msgbox "END!!"

No hay comentarios.:

Publicar un comentario