Dim objSQL, objDatabase, objLogins, objLogin, objUsers, objUser
Dim action, userName, passWord, newPassWord, oldPassWord
Dim sLoginName, sUserName

on Error Resume Next
Set loginArgs = WScript.Arguments
If loginArgs.Count > 0 Then
	
	action = UCase ( loginArgs(0) )
	
	if ( action = "REMOVE" ) then
		if loginArgs.Count <> 2 then

			WScript.Echo " You must specify the user account to be removed." & vbcrlf & vbcrlf & " Example: SQLUser remove smartmax"
			Wscript.Quit(1)
		else
		
			'Code to remove the user
			loginName = loginArgs(1)

		end if
	elseif ( action = "ADD" ) then
		
		if loginArgs.Count <> 3 then

			WScript.Echo " You must specify the user account and password to be added." & vbcrlf & vbcrlf & " Example: SQLUser add smartmax password"
			Wscript.Quit(1)
		else
		
			'Code to create the user
			loginName = loginArgs(1)
			password = loginArgs(2)

		end if
	elseif ( action = "CHANGEPASS" ) then

		if loginArgs.Count <> 4 then

			WScript.Echo " You must specify the user account, the old password, and the new password." & vbcrlf & vbcrlf & " Example: SQLUser changepass smartmax oldpassword newpassword"
			Wscript.Quit(1)
		else
		
			'Code to change the Login password
			loginName = loginArgs(1)
			oldPassWord = loginArgs(2)
			newPassWord = loginArgs(3)

		end if
	else

		WScript.Echo " Please enter valid arguments." & vbcrlf & vbcrlf & " Parameter Options:  1) add  2) remove  3) changepass" & vbcrlf & vbcrlf & " Example: SQLUser remove"
		Wscript.Quit(1)

	end if
	
else

	WScript.Echo " This file must be run from a command prompt!" & vbcrlf & vbcrlf & " Parameter Options:  1) add  2) remove  3) changepass" & vbcrlf & vbcrlf & " Example: SQLUser remove"
	Wscript.Quit(1)
	

End If

'If it is a system administrator user (sa) then don't do anything, just display a message saying that it cannot be used
'else connect to the server and do the necessary tasks based on the arguments passed
if ( UCase ( loginName ) <> "SA" ) then

	Set objSQL = WScript.CreateObject("SQLDMO.SQLServer")
	objSQL.Name = "127.0.0.1\MAILMAX5"
	objSQL.LoginSecure = True
	err.Clear
	objSQL.Connect 'Connect to the database
	'if the connection fails then display the message that unable to connect to the database.
	'else start doing the specified tasks.
	
	if err then
		WScript.Echo "Failed to connect to the MailMax5 database."
		WScript.Quit(1)
	else
		'ok...now we are successfully connected to the SQL Server..so point to the MAILMAX5 database
		set objDatabase = objSQL.Databases("MAILMAX5")
		
		'do u want to create new user login? .... ok.. then execute the following to create the new user/login
		if ( action = "ADD" ) then
				
			Set objLogins = objSQL.Logins
			Err.Clear
			sloginName = objLogins( loginName ).Name 'is Login already exists?
			'if so then display a message saying that login already exists else create the user
			if Err then
				set objLogin = WScript.CreateObject("SQLDMO.Login")
				objLogin.Database = "MAILMAX5"
				objLogin.Name = loginName
				objLogin.SetPassword  "", password 
				
				objLogins.Add objLogin
				
				Set objUsers = objDatabase.Users
				Err.Clear
				sUserName = objUsers( loginName ).Name 'is user already exists?
				'if so then display a message saying that user already exists else create the user
				if Err then
					set objUser = WScript.CreateObject("SQLDMO.User")
					objUser.Login = loginName
					objUser.Name = loginName
					objUser.Role = "db_owner" 'Set the user role as db_owner
					objUsers.Add objUser 'Add the user to the user groups
					WSCript.Echo "Successfully added user '" & loginName & "' with password of '" & password & "' to SQL Server."
				else
					WScript.Echo "Failed to add user.  " & "User '" & loginName & "' already exists in the SQL Server."
				end if
			
			else
				WScript.Echo "Failed to add user.  " & "User '" & loginName & "' already exists in the SQL Server."
			end if
		'do you want to remove the existing user?..ok then execute the following to remove the user/login
		elseif ( action = "REMOVE" ) then
			'Code to remove the user first
			Set objUsers = objDatabase.Users 
			'code to remove the login 
			Set objLogins = objSQL.Logins
			Err.Clear
			sLoginName = objLogins( loginName ).Name 'is login already exists?
			
			'if so then remove else display a message
			if Err then
				WScript.Echo "Failed to remove user.  " & "User '" & loginName & "' does not exist in the SQL Server."
			else
				err.Clear
				sUserName = objUsers( loginName ).Name 'is User already exists?
				if err then
					WScript.Echo "Failed to remove user.  " & "User '" & loginName & "' does not exist in the SQL Server."
				else
					objUsers.Remove ( loginName )
				end if
				objLogins.Remove ( loginName )
				WSCript.Echo "Successfully removed user '" & loginName & "' from the SQL Server."
			end if
		'Do you want to change the password?....ok then execute the following task
		elseif ( action = "CHANGEPASS" ) then

			Set objLogins = objSQL.Logins
			Err.Clear
			sloginName = objLogins( loginName ).Name 'is this login already exists?
			'If so then set the password for it with the new value else display a message
			if Err then
				
				WScript.Echo "Failed to change password.  " & "User '" & loginName & "' does not exist in the SQL Server."

			else
				set objLogin = objLogins( loginName )
				err.Clear
				objLogin.SetPassword oldPassWord, newPassWord 'Set the pass word with the new value
				'is the old password entered correctly..if so the change to the new value else display a message
				if err then
				WSCript.Echo " Failed to change the password!"  & vbcrlf & vbcrlf &  " The password '" & oldpassword & "' is not the current password for user '" & loginName & "'." & vbcrlf & vbcrlf & " If you do not know the old password for user '" & loginName & "' you can remove the account and re-add it."
				else
				WSCript.Echo " Successfully changed password for user '" & loginName & "' from '" & oldpassword & "' to '" & newpassword & "' in the SQL Server."
				end if
			end if

		end if
		'Clean up all the objects you have created from the memory
		objSQL.DisConnect
		set objUser = nothing
		set objUsers = nothing
		set objLogin = nothing
		Set objLogins = nothing
		Set objDatabase = nothing
	
	end if
	
	Set objSQL =nothing
'you are not allowed to use the "sa" user
else
	WScript.Echo " You cannot add, remove, or modify the 'sa' user account.  Please select another account name."
	Wscript.Quit(1)
end if


