Link to home
Start Free TrialLog in
Avatar of sanderson321
sanderson321

asked on

Set Manager attribute to null when account is disabled using Powershell

We would like to run a nightly job that sets the "manager" attribute to NULL if the account is disabled.  A Powershell script is preferred.  Can someone help?
Avatar of Shyjin Varaprath
Shyjin Varaprath
Flag of India image

* How about a VB Script that would enumerate the domain and look for all disabled accounts and then if found, set their "Manager" attribute in the Organization tab as NULL (basically the CLEAR function of ADUC User account property)

* While entering the domain name, provide the FQDN of the domain.

Hope this helps (^_^)
Option Explicit
 
Const ADS_PROPERTY_CLEAR = 1
 
Dim strDomain, objComputer, objUser, oRoot,strDefaultNamingContext, username, objUser1
 
Do
  strDomain = inputbox( "Please enter a domainname", "Input" )
Loop until strDomain <> ""
 
     'Finding our default naming context
    Set oRoot = GetObject("LDAP://rootDSE")
    strDefaultNamingContext = oRoot.Get("defaultNamingContext")
    Set oRoot = Nothing
 
 
ListUsers( strDomain )
 
Sub ListUsers( strDomain )
	Set objComputer = GetObject("WinNT://" & strDomain )
	objComputer.Filter = Array( "User" )
	
	
	For Each objUser In objComputer
 
		username = ""&objuser.name
	
            	If objUser.AccountDisabled = TRUE Then
 
			Set objUser1 = GetObject(getLdapUN(username))
			
			objUser1.PutEx ADS_PROPERTY_CLEAR,"manager",0
			objuser1.Setinfo
		End If
	Next
	
	MsgBox "The Script has finished working"
End Sub
 
'Function module for getLdapUN()
Function getLdapUN(strUN)
Dim oConnect, Command, strLdapQuery, Rs
getLdapUN = False
Set oConnect = CreateObject("ADODB.Connection")
Set Command = CreateObject("ADODB.Command")
'--- search for object in AD ---
strldapquery = "<LDAP://" & strDefaultNamingContext & ">;" & _
"(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & _
strUN & "));ADsPath,cn;subtree"
 
oConnect.Provider = "ADsDSOObject"
oConnect.Open "Active Directory Provider"
Set Command.ActiveConnection = oConnect
Command.CommandText = strldapquery 'strSQL
Set Rs = Command.Execute 'Execute the query
 
'WScript.Echo "Records: " & Rs.RecordCount
If Rs.RecordCount > 0 Then
    getLdapUN = rs("AdsPath")
End If
 
Set oConnect = Nothing
Set Command = Nothing
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

And if you happen to prefer to use Quest's tools...

http://www.quest.com/activeroles-server/arms.aspx

Then...

Chris

PS both of these options will leave you with the manager attribute unset (we're not inserting a blank value in its place)
Get-QADUser -Manager * -Disabled | Set-QADUser -objectAttributes @{manager=''}

Open in new window