Ravish Mody

Here is a simple example of WLST Script which allows us to delete Users and Groups. This  a similar WLST script which Jay had created for Creating Users And Groups Using WLST NonStop , however here we are just deleting the same users and groups. Doing the same thing from admin console is very time consuming. The following WLST Script is just an example for the same but it does the same work in one go.

The best thing here is the Administrator need to just edit the properties file with the users and group details, rest of the things will be taken care by the WLST Script. Administrator need to just change the  Iteration of “for” Loop in the WLST Script sccording to the number of WebLogic Users and WebLogic Groups.

Steps to Deleting Users And Groups Using WLST

Step1). Create a Directory somewhere in your file system like :  “C:WLST_MultiDomain_DS”

Step2). Write a Properties file “details.properties” inside “C:WLST_MultiDomain_DS” like following:

domain.name=Domain_8001
admin.url=t3://localhost:8001
admin.userName=weblogic
admin.password=weblogic
security.realmName=myrealm

total.groups=2
total.username=3

delete.group.name.1=GroupOne
delete.group.name.2=GroupTwo

delete.user.name.1=TestUserOne
delete.user.name.2=TestUserTwo
delete.user.name.3=TestUserThree

NOTE: The “delete.group.name.1.members” Entries must end with a COMMA (,)

Step3). Write the  WLST Script “delete_users_groups.py” inside “C:WLST_MultiDomain_DS” directory.

#############################################################################
#
# @author Copyright (c) 2010 - 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################

from java.io import FileInputStream

propInputStream = FileInputStream("details.properties")
configProps = Properties()
configProps.load(propInputStream)

domainName=configProps.get("domain.name")
adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")
realmName=configProps.get("security.realmName")

totalGroups_to_Create=configProps.get("total.groups")
totalUsers_to_Create=configProps.get("total.username")

connect(adminUserName, adminPassword, adminURL)
serverConfig()
authenticatorPath= '/SecurityConfiguration/' + domainName + '/Realms/' + realmName + '/AuthenticationProviders/DefaultAuthenticator'
print authenticatorPath
cd(authenticatorPath)

print 'Deleting Groups . . .'

i=1
while (i <= int(totalGroups_to_Create)) :
	groupName = configProps.get("delete.group.name."+ str(i))
	try:
		cmo.removeGroup(groupName)
		print '-----------Group Deleted With Name : ' , groupName
	except:
		print '*************** CANNOT DELETE !!! Check If The Group With the Name : ' , groupName ,' Exists or NOT...'
	i = i + 1
print ' '
print ' '

print 'Deleting Users . . .'
x=1
while (x <= int(totalUsers_to_Create)):
	userName = configProps.get("delete.user.name."+ str(x))
	try:
		cmo.removeUser(userName)
		print '-----------User Deleted With Name : ' , userName
	except:
		print '*************** CANNOT DELETE !!! Check If the User With the Name : ' , userName ,' Exists or NOT...'
	x = x + 1
print ' '
print ' '

Step4). Run the “. ./setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
.  ./setWLSEnv.sh

Note: the first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step5). Now run the WLS Script like following:

java        weblogic.WLST        delete_users_groups.py

Following would be the output:

C:WLST_MultiDomain_DS>java weblogic.WLST delete_users_groups.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:8001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'Domain_8001'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Already in Config Runtime

/SecurityConfiguration/Domain_8001/Realms/myrealm/AuthenticationProviders/DefaultAuthenticator
Deleting Groups . . .
-----------Group Deleted With Name :  GroupOne
-----------Group Deleted With Name :  GroupTwo

Deleting Users . . .
-----------User Deleted With Name :  TestUserOne
-----------User Deleted With Name :  TestUserTwo
-----------User Deleted With Name :  TestUserThree

If the Groups or Users are does not exists then below output would be seen

java weblogic.WLST delete_users_groups.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:8001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'Domain_8001'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Already in Config Runtime

/SecurityConfiguration/Domain_8001/Realms/myrealm/AuthenticationProviders/DefaultAuthenticator
Deleting Groups . . .
*************** CANNOT DELETE !!! Check If The Group With the Name :  GroupOne  Exists or NOT...
*************** CANNOT DELETE !!! Check If The Group With the Name :  GroupTwo  Exists or NOT...

Deleting Users . . .
*************** CANNOT DELETE !!! Check If the User With the Name :  TestUserOne  Exists or NOT...
*************** CANNOT DELETE !!! Check If the User With the Name :  TestUserTwo  Exists or NOT...
*************** CANNOT DELETE !!! Check If the User With the Name :  TestUserThree  Exists or NOT...

.

.

Regards,

Ravish Mody

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.