Problems with Account Deletion Script
- From: meridean <chris.john.flynn@xxxxxxxxx>
- Date: Tue, 31 Jul 2007 00:13:10 -0700
Can anyone help with the following script? It does not appear to
delete the user. A single user or multiple users from a file. If
anyone can shed any light on why this is not working I would
appreciate it. Also does anyone know how to connect to a specific
Domain Controller within the AD to action the request?
Here is the Script Code I am currently using:
______________________________________________________________________________________________
<html>
<head>
<title>User Deletion Utility</title>
<HTA:APPLICATION
ID="objDeleteAccount"
APPLICATIONNAME="AccountDeletionScript"
BORDER="thin"
CAPTION="yes"
ICON="SETUP.ICO"
SHOWINTASKBAR="yes"
SCROLL="NO"
SINGLEINSTANCE="yes"
SYSMENU="yes"
WINDOWSTATE="normal"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="no"
</head>
<style>
BODY
{
background-color: "#E4EAF6";
font-family: Helvetica;
font-size: 10pt;
color: "#000080";
margin-top: 5%;
margin-left: 5%;
margin-right: 5%;
margin-bottom: 5%;
}
</STYLE>
<SCRIPT Language="VBScript">
'========================================================================================================================================================
'Sub to intialise and load the HTA Script Window.
'========================================================================================================================================================
sub window_onload
self.focus()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From
Win32_DesktopMonitor")
For Each objItem in colItems
intHorizontal = objItem.ScreenWidth
intVertical = objItem.ScreenHeight
Next
'Msgbox intHorizontal & VBTAB & intVertical
intLeft = (intHorizontal - 600) / 2
intTop = (intVertical - 600) / 2
window.resizeTo 600,600
'window.moveTo intLeft, intTop
txtusername.value = ""
txtusername.disabled = false
btnUserOption.disabled = false
btnReset.disabled = false
btnClose.disabled = false
End sub
'========================================================================================================================================================
'Sub to load option selected by user to rename and delete user
accounts.
'========================================================================================================================================================
Sub btnRunScript_Click
username = txtusername.value
btnUserOption.disabled = true
btnReset.disabled = false
btnClose.disabled = false
If RadioDisable.Checked = true Then
Call DisableAccount(username)
ElseIf RadioDelete.Checked = true Then
If AccSingleRadio.Checked Then
Call DeleteAccountAD(username)
AccMultiRadio.Checked = false
AccSingleRadio.Disabled = true
txtusername.disabled = false
ElseIf AccMultiRadio.Checked Then
Call DeleteMultiAccountAD
AccSingleRadio.Checked = false
AccMultiRadio.Disabled = true
txtusername.disabled = true
Else
AccSingleRadio.Checked = false
AccMultiRadio.Checked = false
txtusername.disabled = false
btnUserOption.disabled = false
btnReset.disabled = false
btnClose.disabled = false
Exit Sub
End If
Else
MsgBox "You have not selected any actions, please select an Action
to perform."
End If
txtusername.value = ""
RadioDisable.Checked = false
RadioDelete.Checked = false
AccSingleRadio.Checked = false
AccMultiRadio.Checked = false
AccSingleRadio.Disabled = false
AccMultiRadio.Disabled = false
txtusername.disabled = false
RadioDisable.Disabled = false
RadioDelete.Disabled = false
btnUserOption.disabled = false
btnReset.disabled = false
btnClose.disabled = false
End Sub
'========================================================================================================================================================
'Sub to set the format for the dropdown box, and enable the remaining
buttons/Fields.
'========================================================================================================================================================
Sub dropdown_click
btnUserOption.disabled = false
btnReset.disabled = false
btnClose.disabled = false
End Sub
'========================================================================================================================================================
'Sub to search for the AD account and then disable it using input from
the Administrator.
'========================================================================================================================================================
Sub DisableAccount(username)
On Error Resume Next
Dim objUser, objNewUser, objContainer
strNTDomain = "DOMAIN"
'***Check to see if the user exists in the domain.
Set objUser = GetObject("WinNT://" & strNTDomain & "/" & username &
",user")
'***If the User does not exist, exit the Script.
If Err.Number = -2147022675 Then
On Error GoTo 0
MsgBox "The " & username & " account does not exist."
txtusername.Value = ""
btnUserOption.disabled = false
btnReset.disabled = false
btnClose.disabled = false
Exit Sub
End If
'***Disable the user Account in the domain.
objUser.Accountdisabled = TRUE
objUser.SetInfo
If Err.Number = -2147024891 Then
On Error GoTo 0
MsgBox "You do not have access to modify the account: " & username &
". Please contact 3rd Line with the Account Details."
txtusername.Value = ""
btnUserOption.disabled = false
btnReset.disabled = false
btnClose.disabled = false
Exit Sub
End If
MsgBox "Account: " & username & " has been disabled."
End Sub
'========================================================================================================================================================
'Sub to search for the AD account and then delete it using input from
the Administrator.
'========================================================================================================================================================
Sub DeleteAccountAD(username)
On Error Resume Next
'Open connection to AD using LDAP
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
'***Set the ou and gets the Parent ID based on the username***
strBase = "<LDAP://DOMAIN>"
strFilter = "(&(objectCategory=person)(sAMAccountName=" & username &
"))"
strAttributes = "sAMAccountName,Name,AdsPath"
strQuery = strBase & ";" & strFilter & ";" & strAttributes
objCommand.CommandText = strQuery
Set ADSIRecordSet = objCommand.Execute
strName = ADSIRecordSet.Fields("sAMAccountName").Value
strUserName = ADSIRecordSet.Fields("AdsPath").Value
errorNum = err.number
errorDesc = err.description
if err.number = 3021 then
msgbox "The account could not be found for " & username
exit sub
end if
strAdsPath = ADSIRecordSet.Fields("AdsPath").Value
strusername = strAdsPath
i = 1
If instr(strusername,"\,") then i = 2
MyArray = Split(strAdsPath,",",-1,1)
intUbound = Ubound(MyArray)
Do While i <= intUbound
If i <> intUbound Then
strParentDN = strParentDN & MyArray(i) & ","
i = i + 1
Else
strParentDN = strParentDN & MyArray(i)
i = i + 1
End If
Loop
set objCont = GetObject("LDAP://" & strParentDN)
objCont.Delete "user", strUserName
MsgBox "Account: " & username & " has been deleted."
End sub
'========================================================================================================================================================
'Sub to search for the AD account and then delete it using input from
a file.
'========================================================================================================================================================
Sub DeleteMultiAccountAD
On Error Resume Next
'Set and Open Excel File
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open ("UsersForDeletion.xls")
strErrorLog = "DeletionLogFile.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strErrorLog, True)
intRow = 2
Do Until objExcel.Cells(intRow,1).Value = ""
username = objExcel.Cells(intRow, 1).Value
'#Set Organisational Unit within Active Directory#
'Open connection to AD using LDAP
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
'***Set the ou and gets the Parent ID based on the username***
strBase = "<LDAP://DOMAIN>"
strFilter = "(&(objectCategory=person)(sAMAccountName=" & username &
"))"
strAttributes = "sAMAccountName,Name,AdsPath"
strQuery = strBase & ";" & strFilter & ";" & strAttributes
objCommand.CommandText = strQuery
Set ADSIRecordSet = objCommand.Execute
strName = ADSIRecordSet.Fields("sAMAccountName").Value
strUserName = ADSIRecordSet.Fields("AdsPath").Value
errorNum = err.number
errorDesc = err.description
if err.number = 3021 then
msgbox "The account could not be found for " & username
exit sub
end if
strAdsPath = ADSIRecordSet.Fields("AdsPath").Value
strusername = strAdsPath
i = 1
If instr(strusername,"\,") then i = 2
MyArray = Split(strAdsPath,",",-1,1)
intUbound = Ubound(MyArray)
Do While i <= intUbound
If i <> intUbound Then
strParentDN = strParentDN & MyArray(i) & ","
i = i + 1
Else
strParentDN = strParentDN & MyArray(i)
i = i + 1
End If
if err.number = 3021 then
objFile.WriteLine "User NOT found" & VBTAB & username & VBCRLF
Else
set objCont = GetObject("LDAP://" & strParentDN)
objCont.Delete "user","cn=" & strName 'username
objFile.WriteLine username & VBTAB & "Deleted" & VBCRLF
End If
Loop
intRow = intRow + 1
Loop
'#Close workbook and quit Excel#
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
'#Clean up#
Set objExcel = Nothing
Set obj*** = Nothing
Set objUser = Nothing
MsgBox "Multiple User Accounts have been deleted. Please refer to the
log file for further information. (" & strErrorLog & ")"
End Sub
'========================================================================================================================================================
'Sub to set fields for radio buttons.
'========================================================================================================================================================
Sub Disable_Click
RadioDisable.Checked = true
RadioDelete.Checked = false
AccSingleRadio.Checked = false
AccMultiRadio.Checked = false
AccSingleRadio.Disabled = true
AccMultiRadio.Disabled = true
txtusername.disabled = false
End Sub
'========================================================================================================================================================
'Sub to set fields for radio buttons.
'========================================================================================================================================================
Sub Delete_Click
RadioDisable.Checked = false
RadioDelete.Checked = true
AccSingleRadio.Checked = false
AccMultiRadio.Checked = false
AccSingleRadio.Disabled = false
AccMultiRadio.Disabled = false
txtusername.disabled = false
End Sub
'========================================================================================================================================================
'Sub to set fields for radio buttons.
'========================================================================================================================================================
Sub Delete_Single_Click
AccSingleRadio.Checked = true
AccMultiRadio.Checked = false
txtusername.disabled = false
End Sub
'========================================================================================================================================================
'Sub to set fields for radio buttons.
'========================================================================================================================================================
Sub Delete_Multiple_Click
AccSingleRadio.Checked = false
AccMultiRadio.Checked = true
txtusername.disabled = true
End Sub
'========================================================================================================================================================
'Sub to reset the HTA Script Window
'========================================================================================================================================================
sub btnReset_click
txtusername.value = ""
AccSingleRadio.Checked = false
AccMultiRadio.Checked = false
RadioDisable.Checked = false
RadioDelete.Checked = false
AccSingleRadio.Disabled = false
AccMultiRadio.Disabled = false
RadioDisable.Disabled = false
RadioDelete.Disabled = false
btnUserOption.disabled = false
btnReset.disabled = false
btnClose.disabled = false
End sub
'========================================================================================================================================================
'Sub to close the HTA Script Window
'========================================================================================================================================================
Sub window_close
window.parent.close
End Sub
'========================================================================================================================================================
'End of VBScript and Begining of Main HTML Code.
'========================================================================================================================================================
</SCRIPT>
<body bgColor="000000">
<P ALIGN = center>
<img src="wavylogo.bmp" alt="COMPANY"></P>
<BR>
<P ALIGN = center><h2 ALIGN = center>
Welcome to the Company Account Deletion Script</P></h2>
<BR>
<table width="100%" border="0">
<tr><th COLSPAN=2>Please Select the Action type<hr></th></tr>
</table>
<table width="100%" border="1">
<tr><td width="33%" valign="top" border= "black"><input type="radio"
name="RadioDisable" name="Disable_Accounts"
onclick="Disable_Click">Disable User Accounts<BR>
<td width="33%" valign="top" border= "black"><input type="radio"
name="RadioDelete" name="Delete_Accounts"
onclick="Delete_Click">Delete User Accounts<BR></td>
</tr>
<table width="100%" border="0">
<tr><th COLSPAN=2>Please Select the deletion type<hr></th></tr>
</table>
<table width="100%" border="1">
<tr><td width="33%" valign="top" border= "black"><input type="radio"
name="AccSingleRadio" name="Delete_Single_Account"
onclick="Delete_Single_Click">Single User Account<BR>
<td width="33%" valign="top" border= "black"><input type="radio"
name="AccMultiRadio" name="Delete_Multiple_Accounts"
onclick="Delete_Multiple_Click">Multiple User Accounts<BR></td>
</tr>
<table width="100%" border="0">
<tr><th COLSPAN=2>Please Enter the Users Information<hr></th></tr>
</table>
<table width="100%" border="0">
<tr><td>Please enter the AD account username:</td><td><input
type="text" id="txtusername" size="30"></td></tr>
</table><hr>
<table width="100%" border="0">
<tr><td><P ALIGN = center><input type="button" id="btnUserOption"
Value="Submit" onclick="btnRunScript_Click"><input type="button"
id="btnReset" Value="Reset" onclick="btnReset_Click"><input
type="button" id="btnClose" Value="Close" onclick="window_Close"></P></
th></tr>
</table>
</body>
</html>
.
- Prev by Date: Re: Help required to script a message
- Next by Date: Re: Win32_LogicalDisk and usb sticks without accessing floppy ?
- Previous by thread: Finding files / wildcards
- Next by thread: Re: Accuracy of LastLogon and LastLogonTimeStamp
- Index(es):