From: Rowland Penny Date: Wed, 28 Sep 2016 14:39:52 +0000 (+0100) Subject: bug 12292: stop user.py throwing errors if user is unknown X-Git-Tag: tevent-0.9.31~61 X-Git-Url: http://git.samba.org/samba.git/?a=commitdiff_plain;h=22da0887b26f090dd39e864c139908ea33738a7b;p=metze%2Fsamba-autobuild%2F.git bug 12292: stop user.py throwing errors if user is unknown BUG: https://bugzilla.samba.org/show_bug.cgi?id=12292 Signed-off-by: Rowland Penny Reviewed-by: Alexander Bokovoy --- diff --git a/python/samba/netcmd/user.py b/python/samba/netcmd/user.py index 5adc2879bcc..43581703bfd 100644 --- a/python/samba/netcmd/user.py +++ b/python/samba/netcmd/user.py @@ -406,10 +406,23 @@ Example2 shows how to delete a user in the domain against the local server. su lp = sambaopts.get_loadparm() creds = credopts.get_credentials(lp, fallback_machine=True) + samdb = SamDB(url=H, session_info=system_session(), + credentials=creds, lp=lp) + + filter = ("(&(sAMAccountName=%s)(sAMAccountType=805306368))" % + username) + try: - samdb = SamDB(url=H, session_info=system_session(), - credentials=creds, lp=lp) - samdb.deleteuser(username) + res = samdb.search(base=samdb.domain_dn(), + scope=ldb.SCOPE_SUBTREE, + expression=filter, + attrs=["dn"]) + user_dn = res[0].dn + except IndexError: + raise CommandError('Unable to find user "%s"' % (username)) + + try: + samdb.delete(user_dn) except Exception, e: raise CommandError('Failed to remove user "%s"' % username, e) self.outf.write("Deleted user %s\n" % username)