使用vbs修改AD用户属性

 以下代码给AD中OU为总部的用户设置密码、显示名称等。

 

' VBScript source code
' Determine DNS domain name (this could be hard coded).
Option Explicit
Dim objUser, objRootDSE,objCommand,objConnection,objRecordSet
Dim strContainer,strDNSDomain, intCounter, intAccValue,strBase,strFilter,strAttributes,strQuery,strDN

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

strContainer = "OU=总部,"
'intAccValue = 544
intAccValue = 512
strContainer = strContainer & strDNSDomain


' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

' Search the entire domain.
strBase = "<LDAP://" & strContainer & ">"
wscript.echo strBase
' Filter to retrieve only user objects.
'strFilter = "(& (objectCategory=peron)(objectClass=user))"
strFilter = "(objectClass=user)"
' Retrieve the distinguishedName and physicalDeliveryOfficeName attributes.
strAttributes = "distinguishedName"
' Construct the LDAP query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

' Enumerate the recordset.
intCounter = 0
Do Until objRecordSet.EOF
' For each user, retrieve DN and office.
strDN = objRecordSet.Fields("distinguishedName").Value
Set objUser = GetObject("LDAP://" & strDN)
    objUser.SetPassword "pass@word1"
    objUser.SetInfo
    objUser.Put "userAccountControl", intAccValue
    objuser.Put "userPrincipalName",objUser.get("sAMAccountName") & "@yudeandemo.com" 

	objuser.Put "givenName", left(objuser.get("displayName"), 1)
   objuser.Put "sn", right(objuser.get("displayName"), len(objuser.get("displayName")) -1) '名
    objUser.SetInfo
    intCounter = intCounter +1
' Go to the next record in the recordset.
objRecordSet.MoveNext
If intAccValue=514 Then
	'WScript.Echo " 用户 " &objUser.get("name") & " 已经被禁用.当前状态码为:" _
	'& intAccValue
         'WScript.Echo " 用户 " &objUser.get("userPrincipalName") &objUser.get("sAMAccountName")
Else 
	'WScript.Echo " 用户 " &objUser.get("name") & " 下次登陆必须修改密码.当前状态码为:" _
	'& intAccValue
End If
Loop
WScript.Echo "总共修改 "& intCounter & " 个用户"
WScript.Quit


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值