rustyvs
Goto Top

VBSkript um Attribute für AD-User zu ändern

Hallo zusammen,

ich versuche ein VBSkript zu erstellen, dass für alle Benutzer in unserer AD das extensionAttribut10 setzt.
Dazu hab ich bereits eine CSV-Datei (BoxNr.csv) mit allen Benutzern und dem dazu gehörenden Wert, welcher als Attribut gesetzt werden soll, angelegt.

Beispiel:
extensionAttribute10;CN
3000;Meyer\, Marko
3001;Müller\, Maria
...


Als weiteres habe ich auch schon ein Script, mit dem ich einzelne Benutzer bearbeiten kann.

Set objUser = GetObject _
   ("LDAP://CN=Meyer\, Marko,OU=Benutzer,OU=produktion,DC=firma,DC=local")   

 
objUser.Put "extensionAttribute10", "3000"  

 
objUser.SetInfo

Nun würde ich gerne das Skript umschreiben, damit die Benutzer und Attribute aus der CSV-Datei ausgelesen werden und die Attribute in der AD automatisch gesetzt werden.

Ich wäre sehr Dankbar, wenn mir hier jemand helfen könnte!

MFG Rusty

Content-Key: 257504

Url: https://administrator.de/contentid/257504

Printed on: April 18, 2024 at 08:04 o'clock

Mitglied: 114757
114757 Dec 12, 2014 updated at 12:46:59 (UTC)
Goto Top
Moin,
z.B. so
set fso = CreateObject("Scripting.FilesystemObject")  
arrContent = Split(fso.OpenTextFile("C:\BoxNr.csv",1).ReadAll(),vbNewline)  

for i = 1 to UBound(arrContent)
  if arrContent(i) <> "" then  
    arrCols = Split(arrContent(i),";")  
    Set objUser = GetObject("LDAP://cn=" & arrCols(1) & ",OU=Benutzer,OU=produktion,DC=firma,DC=local")   
    objUser.put "extensionAttribute10",arrCols(0)  
    objUser.SetInfo
  End if
Next
msgbox "Feddich"  
Gruß jodel32
Member: colinardo
Solution colinardo Dec 12, 2014 updated at 13:38:22 (UTC)
Goto Top
Hallo Rusty,
ich würde es zwar mit Powershell machen (ist da ein Einzeiler), aber hier noch eine VBS Lösung wenn vorher nicht klar ist in welcher OU sich der User befindet:
Hinweis: Für dieses Script musst du keinen Backslash für den Usernamen in der CSV-Datei setzen.
Const CSV = "C:\BoxNr.csv"  
Dim fso, arrContent,arrCols,objUser

set fso = CreateObject("Scripting.FilesystemObject")  
arrContent = Split(fso.OpenTextFile(CSV,1).ReadAll(),vbNewline)

for i = 1 to UBound(arrContent)
  if arrContent(i) <> "" then  
    arrCols = Split(arrContent(i),";")  
    Set objUser = FindAccount(Trim(arrCols(1)))
    if not objUser is nothing then
	objUser.put "extensionAttribute10",Trim(arrCols(0))  
    	objUser.SetInfo
    End if
  End if
Next

Function FindAccount(cn)
	On Error Resume Next
	Dim adoCommand, adoConnection
	Dim varBaseDN, varFilter
	Dim objRootDSE, varDNSDomain, strQuery, adoRecordset

	Set adoCommand = CreateObject("ADODB.Command")  
	Set adoConnection = CreateObject("ADODB.Connection")  
	adoConnection.Provider = "ADsDSOObject"  
	adoConnection.Open "Active Directory Provider"  
	Set adoCommand.ActiveConnection = adoConnection
	
	' Search entire Active Directory domain.  
	Set objRootDSE = GetObject("LDAP://RootDSE")  
	
	varDNSDomain = objRootDSE.Get("defaultNamingContext")  
	varBaseDN = "<LDAP://" & varDNSDomain & ">"  
	
	' Filter for user objects.  
	varFilter = "(&(objectCategory=person)(objectClass=user)(cn=" & cn & "))"  
	
	' Construct the LDAP syntax query.  
	adoCommand.CommandText = varBaseDN & ";" & varFilter & ";ADSPath;Subtree"  
	adoCommand.Properties("Page Size") = 2  
	adoCommand.Properties("Timeout") = 20  
	adoCommand.Properties("Cache Results") = False  
	Set adoRecordset = adoCommand.Execute
	adoRecordset.MoveFirst

	If adoRecordset.RecordCount > 0 Then
		set FindAccount = GetObject(adoRecordset("ADSPath"))  
	else
		set FindAccount = Nothing
	End If
	
	adoRecordset.Close
	adoConnection.Close
End Function
Grüße Uwe

-edit- hier noch als Ergänzung eine Lösung mit Powershell (hier ebenfalls in der CSV-Datei keinen Backslash im CN setzen):
import-csv "C:\BoxNr.csv" -Delimiter ";" | %{$wert=$_.extensionAttribute10; get-aduser -Filter "cn -eq '$($_.cn)'" | set-aduser -Replace @{"extensionAttribute10"=$wert}}  
Member: RustyVS
RustyVS Dec 12, 2014 at 13:40:37 (UTC)
Goto Top
Vielen Dank für die schnellen Lösungen!

Hat perfekt funktioniert.


MfG Rusty