svn commit: samba r16031 - in branches/SOC/bnh: .

brad at samba.org brad at samba.org
Sat Jun 3 19:05:51 GMT 2006


Author: brad
Date: 2006-06-03 19:05:51 +0000 (Sat, 03 Jun 2006)
New Revision: 16031

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=16031

Log:
Removed ads_adduser.vbs and replaced with ads_adduser.wsf.
Windows script files (.wsf) have xml tags that let us include other source files.

Functionality has also changed between these ads_adduser.vbs and ads_adduser.wsf.
ads_adduser.wsf checks to see if the user exists in the directory, and if it does it removes the user from the directory.
This script also handles its own runtime error handling, rather than letting the interpreter halt on error.


Added:
   branches/SOC/bnh/ads_adduser.wsf
Removed:
   branches/SOC/bnh/ads_adduser.vbs


Changeset:
Deleted: branches/SOC/bnh/ads_adduser.vbs
===================================================================
--- branches/SOC/bnh/ads_adduser.vbs	2006-06-03 18:58:35 UTC (rev 16030)
+++ branches/SOC/bnh/ads_adduser.vbs	2006-06-03 19:05:51 UTC (rev 16031)
@@ -1,58 +0,0 @@
-const READ_ONLY = 1
-const USAGE_STATEMENT = "Usage: cscript ads_adduser.vbs /username:<username> /password:<password>"
-const ADS_UF_ACCOUNTDISABLE = 2
-
-' This function returns the contents of a file.
-' When passed the name of a .vbs script and passed to execute, the contents
-' of the script are visible within the relevant scope of this script.
-function include(library_filename)
-	dim filesystem_object, file
-	set filesystem_object = createobject("scripting.filesystemobject")
-	set file = filesystem_object.opentextfile(library_filename, READ_ONLY)
-
-	include = file.readall
-	set file = nothing
-	set filesystem_object = nothing
-end function
-
-execute include("common.vbs")
-
-' Required command line options
-dim required_options, provided_options 
-
-required_options = array("username", "password")
-set provided_options = wscript.arguments.named
-
-set setup_options = new setup_object
-setup_options.check_options provided_options, required_options
-
-if setup_options.error_code = RTN_ERR then
-	setup_options.list_missing_options
-	stdout.writeline USAGE_STATEMENT
-	wscript.quit(setup_options.error_code)
-end if
-
-username = provided_options.item("username")
-password = provided_options.item("password")
-
-' Bind to the DC.
-set rootDSE = getobject("LDAP://rootDSE")
-set container = getobject("LDAP://CN=Users," & _
-	rootDSE.get("defaultNamingContext"))
-
-' Create the user account.
-set user_account = container.create("User", "CN=" & username)
-user_account.put "sAMAccountName", username
-user_account.setinfo
-
-' Get user account info.
-set user_account = getobject _
-	("LDAP://CN=" & username & ",CN=Users," & _
-	rootDSE.get("defaultNamingContext"))
-
-' Set the password and enable the account.
-user_account.setpassword password
-useraccountcontrol = user_account.get("userAccountControl")
-user_account.put "userAccountControl", _
-	useraccountcontrol XOR ADS_UF_ACCOUNTDISABLE
-user_account.setinfo

Added: branches/SOC/bnh/ads_adduser.wsf
===================================================================
--- branches/SOC/bnh/ads_adduser.wsf	2006-06-03 18:58:35 UTC (rev 16030)
+++ branches/SOC/bnh/ads_adduser.wsf	2006-06-03 19:05:51 UTC (rev 16031)
@@ -0,0 +1,131 @@
+' A windows script (.wsf) to add an AD user, written in VBScript.
+' Copyright Brad Henry <brad at samba.org> 2006
+' Released under the GNU GPL v2 or later.
+
+<package>
+<job id=ads_adduser>
+<script language="VBScript" src="common.vbs">
+
+const READ_ONLY = 1
+const USAGE_STATEMENT = "Usage: cscript ads_adduser.wsf /username:<username> /password:<password>"
+const ADS_UF_ACCOUNTDISABLE = 2
+
+' check to see if the user account exists.
+' if it does, delete and then create it.
+' if it does not, create it.
+
+function setup_user(username, password)
+
+	on error resume next
+
+	' Bind to the DC.
+	set rootDSE = getObject("LDAP://rootDSE")
+	if err.number <> 0 then
+		stdout.writeline "Unhandled error occurred while resolving " _
+			& "the LDAP rootDSE."
+		report_error
+		wscript.quit(err.number)
+	end if
+
+	binding_string = "LDAP://CN=Users," _
+		& rootDSE.get("defaultNamingContext")
+	set container = getObject(binding_string)
+	if err.number <> 0 then
+		stdout.writeline "Unhandled error occurred while binding to " _
+			& binding_string & "."
+		report_error
+		wscript.quit(err.number)
+	end if
+
+	' Get user account info.
+	set user_account = getObject ("LDAP://CN=" & username _
+		& ",CN=Users," & rootDSE.get("defaultNamingContext"))
+
+	if err.number = 0 then
+		' The account exists.
+		stdout.writeline "User account " & username & " exists."
+
+		' Delete the account.
+		container.delete "User", "CN=" & username
+		if err.number <> 0 then
+			stdout.writeline "Unhandled error while deleting " _
+				& username
+			report_error
+			wscript.quit(err.number)
+		end if
+
+		stdout.writeline username & " deleted."
+	else if hex(err.number) <> ADSI_ERROR_DS_NO_SUCH_OBJECT then
+		stdout.writeline "An unhandled error occurred while " _
+			& "retrieving user account information for " _
+			& username & "."
+		report_error
+		wscript.quit(err.number)
+	     end if
+	end if
+
+	' Clear the last error in case the account didn't already exist.
+	err.clear
+
+	' Create the account.
+	set user_account = container.create("User", "CN=" & username)
+	user_account.put "sAMAccountName", username
+	user_account.setInfo
+	if err.number <> 0 then
+		stdout.writeline "An unhandled error occurred while " _
+			& "retrieving user account information for " _
+			& username & "."
+		report_error
+		wscript.quit(err.number)
+	end if
+
+	stdout.writeline username & " created."
+
+	' Set the password and enable the account.
+	user_account.setPassword password
+	userAccountControl = user_account.get("userAccountControl")
+	user_account.put "userAccountControl", userAccountControl _
+		XOR ADS_UF_ACCOUNTDISABLE
+	user_account.setInfo
+
+	if err.number <> 0 then
+		if hex(err.number) = LM_NERR_PasswordTooShort then
+			stdout.writeline "The password does not meet " _
+				& "the password policy requirements."
+			report_error
+			wscript.quit(err.number)
+		end if
+		stdout.writeline "An unhandled error occured while " _
+			& "setting the password for user account " _
+			& username & "."
+		report_error
+		wscript.quit(err.number)
+	end if
+end function
+
+' Command line options
+dim required_options, provided_options
+
+required_options = array("username", "password")
+set provided_options = wscript.arguments.named
+
+set setup_options = new setup_object
+setup_options.check_options provided_options, required_options
+
+if setup_options.error_code = RTN_ERR then
+	setup_options.list_missing_options
+	stdout.writeline USAGE_STATEMENT
+	wscript.quit(setup_options.error_code)
+end if
+
+username = provided_options.item("username")
+password = provided_options.item("password")
+
+' check to see if the user account exists.
+' if it does, delete and then recreate it.
+' if it does not, create it.
+setup_user username, password
+
+</script>
+</job>
+</package>


Property changes on: branches/SOC/bnh/ads_adduser.wsf
___________________________________________________________________
Name: svn:executable
   + *



More information about the samba-cvs mailing list