Author: brad Date: 2006-06-01 03:21:22 +0000 (Thu, 01 Jun 2006) New Revision: 15987
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=15987 Log: common.vbs is the start of a library. Right now it sets up stdin and stdout, and has a class to handle command line options. Added library handling to the user and share scripts. They also use the library option handling class. Added: branches/SOC/bnh/common.vbs Modified: branches/SOC/bnh/ads_adduser.vbs branches/SOC/bnh/ads_deluser.vbs branches/SOC/bnh/smb_addshare.vbs branches/SOC/bnh/smb_delshare.vbs Changeset: Modified: branches/SOC/bnh/ads_adduser.vbs =================================================================== --- branches/SOC/bnh/ads_adduser.vbs 2006-05-31 17:34:04 UTC (rev 15986) +++ branches/SOC/bnh/ads_adduser.vbs 2006-06-01 03:21:22 UTC (rev 15987) @@ -1,46 +1,58 @@ -Const ADS_UF_ACCOUNTDISABLE=2 +const READ_ONLY = 1 +const USAGE_STATEMENT = "Usage: cscript ads_adduser.vbs /username:<username> /password:<password>" +const ADS_UF_ACCOUNTDISABLE = 2 -Set stdout = WScript.StdOut -Set stdin = WScript.StdIn +' 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) -'Check passed in parameters. -Set argv = WScript.Arguments.Named + include = file.readall + set file = nothing + set filesystem_object = nothing +end function -if WScript.Arguments.Count = 2 Then - username = argv.Item("username") - password = argv.Item("password") - - If Not argv.Exists("username") Then - stdout.Write "You must specify a username (/username:<username>)" - WScript.Quit - ElseIf Not argv.Exists("password") Then - stdout.Write "You must specify a password (/password:<password>)" - WScript.Quit - End If -Else - stdout.Write "Usage: cscript ads_adduser.vbs /username:<username> /password:<password>" - WScript.Quit -End If +execute include("common.vbs") -'Bind to the DC. -Set rootDSE = GetObject("LDAP://rootDSE") -Set container = GetObject("LDAP://CN=Users," & _ - rootDSE.Get("defaultNamingContext")) +' Required command line options +dim required_options, provided_options -'Create the user account. -Set userAccount = container.Create("User", "CN=" & username) -userAccount.Put "sAMAccountName", username -userAccount.SetInfo +required_options = array("username", "password") +set provided_options = wscript.arguments.named -'Get user account info. -Set userAccount = GetObject _ - ("LDAP://CN=" & username & ",CN=Users," & _ - rootDSE.Get("defaultNamingContext")) +set setup_options = new setup_object +setup_options.check_options provided_options, required_options -'Set the password and enable the account. -userAccount.SetPassword password -userAccountControl = userAccount.Get("userAccountControl") -userAccount.Put "userAccountControl", _ - userAccountControl XOR ADS_UF_ACCOUNTDISABLE -userAccount.SetInfo +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 Modified: branches/SOC/bnh/ads_deluser.vbs =================================================================== --- branches/SOC/bnh/ads_deluser.vbs 2006-05-31 17:34:04 UTC (rev 15986) +++ branches/SOC/bnh/ads_deluser.vbs 2006-06-01 03:21:22 UTC (rev 15987) @@ -1,20 +1,42 @@ -Set stdout = WScript.StdOut -Set stdin = WScript.StdIn +const READ_ONLY = 1 +const USAGE_STATEMENT = "Usage: cscript ads_deluser.vbs /username:<username>" -' Check passed in parameters. -Set argv = WScript.Arguments.Named +' 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) -If Not argv.Exists("username") Then - stdout.Write "You must specify a username (/username:<username>)" - WScript.Quit -Else - username = argv.Item("username") -End If + 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") +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") + ' Bind to the DC. -Set rootDSE = GetObject("LDAP://rootDSE") -Set container = GetObject("LDAP://CN=Users," & _ - rootDSE.Get("defaultNamingContext")) +set rootDSE = getobject("LDAP://rootDSE") +Set container = getobject("LDAP://CN=Users," & _ + rootDSE.get("defaultNamingContext")) ' Delete the user account. -container.Delete "User", "CN=" & username +container.delete "User", "CN=" & username Added: branches/SOC/bnh/common.vbs =================================================================== --- branches/SOC/bnh/common.vbs 2006-05-31 17:34:04 UTC (rev 15986) +++ branches/SOC/bnh/common.vbs 2006-06-01 03:21:22 UTC (rev 15987) @@ -0,0 +1,40 @@ +const RTN_OK = 0 +const RTN_ERR = 1 + +set stdout = wscript.stdout +set stdin = wscript.stdin + +class setup_object + dim error_code, missing_options + + public function check_options(provided, required) + dim opt, index + index = 0 + + ' Make sure all required options are available in + ' the provided list. The redim call resizes the array. + for each opt in required + if not provided.exists(opt) then + error_code = RTN_ERR + redim preserve missing_options(index) + missing_options(index) = opt + index = index + 1 + end if + next + end function + + public function list_missing_options + dim opt + stdout.writeline "The following required options are missing:" + for each opt in missing_options + stdout.writeline "/" & opt & ":" + next + end function + + ' A subroutine named class_initialize is used to set default values + ' on object instantiation. + private sub class_initialize + error_code = RTN_OK + missing_options = array(25) + end sub +end class Modified: branches/SOC/bnh/smb_addshare.vbs =================================================================== --- branches/SOC/bnh/smb_addshare.vbs 2006-05-31 17:34:04 UTC (rev 15986) +++ branches/SOC/bnh/smb_addshare.vbs 2006-06-01 03:21:22 UTC (rev 15987) @@ -1,30 +1,41 @@ +const READ_ONLY = 1 +const USAGE_STATEMENT = "Usage: cscript smb_addshare.vbs /username:<username> /sharename:<share name> /sharepath:<share path>" -Set stdout = WScript.StdOut -Set stdin = WScript.StdIn +' 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 -' Check passed in parameters. -Set argv = WScript.Arguments.Named + set filesystem_object = CreateObject("Scripting.FileSystemObject") + set file = filesystem_object.OpenTextFile(library_filename, READ_ONLY) -if WScript.Arguments.Count = 3 Then - username = argv.Item("username") - sharename = argv.Item("sharename") - pathname = argv.Item("sharepath") - - If Not argv.Exists("username") Then - stdout.Write "You must specify a username (/username:<username>)" - WScript.Quit - ElseIf Not argv.Exists("sharename") Then - stdout.Write "You must specify a share name (/sharename:<share name>)" - WScript.Quit - ElseIf Not argv.Exists("sharepath") Then - stdout.Write "You must specify a share path (/sharepath:<share path>)" - WScript.Quit - End If -Else - stdout.Write "Usage: cscript smb_addshare.vbs /username:<username> /sharename:<share name> /sharepath:<share path>" - WScript.Quit -End If + 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", "sharename", "sharepath") +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") +sharename = provided_options.item("sharename") +pathname = provided_options.item("sharepath") + ' Check if the directory exists, and exit if it does. Set fileSystemObject = CreateObject("Scripting.FileSystemObject") if fileSystemObject.FolderExists(pathname) Then @@ -45,4 +56,3 @@ ' Remove share. ' Delete shared directory. 'fileSystemObject.DeleteFolder(pathname) - Modified: branches/SOC/bnh/smb_delshare.vbs =================================================================== --- branches/SOC/bnh/smb_delshare.vbs 2006-05-31 17:34:04 UTC (rev 15986) +++ branches/SOC/bnh/smb_delshare.vbs 2006-06-01 03:21:22 UTC (rev 15987) @@ -1,36 +1,51 @@ -Set stdout = WScript.StdOut -Set stdin = WScript.StdIn +const READ_ONLY = 1 +const USAGE_STATEMENT = "Usage: cscript smb_delshare.vbs /sharename:<share name> /sharepath:<share path>" +const NEW_WINDOW_MINIMIZED = 7 -' Check passed in parameters. -Set argv = WScript.Arguments.Named +' 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) -if WScript.Arguments.Count = 2 Then - sharename = argv.Item("sharename") - pathname = argv.Item("sharepath") + include = file.readall + set file = Nothing + set filesystem_object = nothing +end function - If Not argv.Exists("sharename") Then - stdout.Write "You must specify a share name (/sharename:<share name>)" - WScript.Quit - ElseIf Not argv.Exists("sharepath") Then - stdout.Write "You must specify a share path (/sharepath:<share path>)" - WScript.Quit - End If -Else - stdout.Write "Usage: cscript smb_addshare.vbs /sharename:<share name> /sharepath:<share path>" - WScript.Quit -End If +execute include("common.vbs") +' Required command line options +dim required_options, provided_options + +required_options = array("sharename", "sharepath") +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 + +sharename = provided_options.item("sharename") +pathname = provided_options.item("sharepath") + ' Check if the directory exists, and exit if it does not. -Set fileSystemObject = CreateObject("Scripting.FileSystemObject") -if Not fileSystemObject.FolderExists(pathname) Then - stdout.Write "Error: Directory " & pathname & " does not exist. Exiting." - WScript.Quit -End If +set filesystemobject = createobject("scripting.filesystemobject") +if not filesystemobject.folderexists(pathname) then + stdout.write "Error: Directory " & pathname & " does not exist. Exiting." + wscript.quit +end if ' Delete the share. -Set shell = WScript.CreateObject("WScript.Shell") +set shell = wscript.createobject("wscript.shell") netsharecmd = "net share " & sharename & " /DELETE" -shell.Run netsharecmd, 7, True +shell.run netsharecmd, NEW_WINDOW_MINIMIZED, True ' Delete the directory. -fileSystemObject.DeleteFolder(pathname) +filesystemobject.deletefolder(pathname)