使用VB删除ActiveDirectory中的主目录

时间:2022-06-23 02:59:33

I want to use VB to search the AD for disabled accounts (older than 90 days and delete their home directory. The following script works fine but not recursively :(

我想使用VB在AD中搜索已禁用的帐户(超过90天并删除其主目录。以下脚本工作正常但不递归:(

Can anyone help to make this working? Any help would he highly appreciated :)

任何人都可以帮助使这个工作吗?任何帮助他会高度赞赏:)

Option Explicit

Dim objOU, objUser, objFSO, strHomeDirectory, objTextFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOU = GetObject("LDAP://OU=users,DC=corp,DC=contoso,DC=com")

objOU.Filter = Array("user")

For Each objUser In objOU
if ( objuser.useraccountcontrol = 514 Or objuser.useraccountcontrol = 66050 ) And DateDiff("d", objUser.WhenChanged, Now) > 90 Then 
    ' Retrieve home directory.
    strHomeDirectory = objUser.homeDirectory
    ' Replace %username% with value of sAMAccountName attribute.
     strHomeDirectory = Replace(strHomeDirectory, "%username%", objUser.sAMAccountName)
    ' Delete the folder.
     objFSO.DeleteFolder strHomeDirectory
else
wscript.sleep 100
End If
Next

1 个解决方案

#1


1  

You'd normally do that with an LDAP query. If you'll forgive the shameless plug, I wrote a class for AD queries a while ago to simplify this kind of task. You could copy/paste the code to your script and use it like this:

您通常使用LDAP查询来执行此操作。如果你原谅无耻的插件,我前段时间为AD查询写了一个类来简化这种任务。您可以将代码复制/粘贴到脚本中,并像这样使用它:

Set fso = CreateObject("Scripting.FileSystemObject")

Set qry = New ADQuery
qry.Filter = "(&(objectClass=User)(objectCategory=Person)" & _
             "(userAccountControl:1.2.840.113556.1.4.803:=2))"
qry.Attributes = Array("sAMAccountName", "homeDirectory", "whenChanged")

Set rs = qry.Execute

If Not rs Is Nothing Then
  Do Until rs.EOF
    If DateDiff("d", rs.Fields("whenChanged").Value, Now) > 90 Then
      homedir = rs.Fields("homeDirectory").Value
      homedir = Replace(homedir, "%username%", rs.Fields("sAMAccountName").Value)
      If fso.FolderExists(homedir) Then fso.DeleteFolder homedir
    End If
    rs.MoveNext
  Loop
  rs.Close
End If

#1


1  

You'd normally do that with an LDAP query. If you'll forgive the shameless plug, I wrote a class for AD queries a while ago to simplify this kind of task. You could copy/paste the code to your script and use it like this:

您通常使用LDAP查询来执行此操作。如果你原谅无耻的插件,我前段时间为AD查询写了一个类来简化这种任务。您可以将代码复制/粘贴到脚本中,并像这样使用它:

Set fso = CreateObject("Scripting.FileSystemObject")

Set qry = New ADQuery
qry.Filter = "(&(objectClass=User)(objectCategory=Person)" & _
             "(userAccountControl:1.2.840.113556.1.4.803:=2))"
qry.Attributes = Array("sAMAccountName", "homeDirectory", "whenChanged")

Set rs = qry.Execute

If Not rs Is Nothing Then
  Do Until rs.EOF
    If DateDiff("d", rs.Fields("whenChanged").Value, Now) > 90 Then
      homedir = rs.Fields("homeDirectory").Value
      homedir = Replace(homedir, "%username%", rs.Fields("sAMAccountName").Value)
      If fso.FolderExists(homedir) Then fso.DeleteFolder homedir
    End If
    rs.MoveNext
  Loop
  rs.Close
End If