forked from shaniacht1/content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomation-ADGetEmailForUser.yml
61 lines (61 loc) · 2.55 KB
/
automation-ADGetEmailForUser.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
args:
- description: Active Directory Distinguished Name of the desired user
name: dn
- description: Name of the desired user
name: name
- description: Include these AD attributes of the resulting objects in addition to
the default ones
name: attributes
comment: Use Active Directory to retrieve the email address associated with the specified
user. The user can be specified by name, email or as an Active Directory Distinguished
Name (DN).
commonfields:
id: ADGetEmailForUser
version: -1
dependson:
must:
- ad-search
deprecated: true
name: ADGetEmailForUser
runonce: false
script: |-
# Optional arguments and default values
attrs = 'name,displayname,mail'
if demisto.get(demisto.args(), 'attributes'):
attrs += "," + demisto.args()['attributes']
memberDN = ''
if demisto.get(demisto.args(), 'dn'):
memberDN = demisto.args()['dn']
elif demisto.get(demisto.args(), 'name'):
resp = demisto.executeCommand( 'AdSearch', { 'filter' : "(&(objectClass=User)(name=" + demisto.args()['name'] + "))" } )
if type(resp)==list and len(resp)==1 and type(resp[0])==dict and 'Contents' in resp[0] and type(resp[0]['Contents'])==list and len(resp[0]['Contents'])==1 and type(resp[0]['Contents'][0])==dict and 'dn' in resp[0]['Contents'][0]:
memberDN = resp[0]['Contents'][0]['dn']
else:
demisto.results( resp )
sys.exit(0)
else:
demisto.results( { 'Type' : entryTypes['error'], 'ContentsFormat' : formats['text'], 'Contents' : 'You must provide either dn or name as argument!' } )
sys.exit(0)
if memberDN:
filterstr = r"(&(objectClass=User)(distinguishedName=" + memberDN + "))"
resp = demisto.executeCommand( 'AdSearch' , { 'filter' : filterstr, 'attributes' : attrs } )
if not isError(resp[0]) and not 'No results' == resp[0]['Contents']:
if isinstance(resp[0]['Contents'], list) and resp[0]['Contents']:
person = resp[0]['Contents'][0]
dispname = demisto.get(person, 'displayname')
if dispname:
demisto.setContext('ADDisplayName', dispname)
name = demisto.get(person, 'name')
if name:
demisto.setContext('ADPersonName', name)
dn = demisto.get(person, 'dn')
if dn:
demisto.setContext('ADDistinguishedName', dn)
demisto.results(resp)
else:
demisto.results( { 'Type' : entryTypes['error'], 'ContentsFormat' : formats['text'], 'Contents' : 'User not found.' } )
scripttarget: 0
system: true
tags:
- active directory
type: python