-
Notifications
You must be signed in to change notification settings - Fork 17
/
Create-Account.ps1
83 lines (71 loc) · 2.86 KB
/
Create-Account.ps1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Define the URL for the SOAP endpoint
$zimbraServerUrl = "https://ZimbraServer:7071/service/admin/soap/"
#Define admin Creds
$adminUsername = Read-Host "Enter your admin email account"
$securePassword = (Read-Host "Enter your admin Password" -AsSecureString)
$adminPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword))
# Define the SOAP envelope for the first step of authentication
$authRequestStep1 = @"
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<context xmlns="urn:zimbra"/>
</soap:Header>
<soap:Body>
<AuthRequest xmlns="urn:zimbraAdmin">
<account by="adminName">$adminUsername</account>
<password>$adminPassword</password>
</AuthRequest>
</soap:Body>
</soap:Envelope>
"@
# Send the first step of the authentication request
$authResponseStep1 = Invoke-RestMethod -Uri $zimbraServerUrl -Method Post -Body $authRequestStep1 -ContentType "application/soap+xml"
# Extract the temporary auth token from the response
$tempAuthToken = $authResponseStep1.Envelope.Body.AuthResponse.authToken
# Define your two-factor authentication code
$twoFactorCode = Read-Host "Please enter 2FA code"
# Define the SOAP envelope for the second step of authentication
$authRequestStep2 = @"
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<context xmlns="urn:zimbra">
<authToken>$tempAuthToken</authToken>
</context>
</soap:Header>
<soap:Body>
<AuthRequest xmlns="urn:zimbraAdmin">
<twoFactorCode>$twoFactorCode</twoFactorCode>
</AuthRequest>
</soap:Body>
</soap:Envelope>
"@
# Send the second step of the authentication request
$authResponseStep2 = Invoke-RestMethod -Uri $zimbraServerUrl -Method Post -Body $authRequestStep2 -ContentType "application/soap+xml"
# Extract the final auth token from the response
$authToken = $authResponseStep2.Envelope.Body.AuthResponse.authToken
# Define the SOAP request body
$createAccountRequest = @"
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<context xmlns="urn:zimbra">
<authToken>$authToken</authToken>
</context>
</soap:Header>
<soap:Body>
<CreateAccountRequest xmlns="urn:zimbraAdmin">
<name>$email</name>
<a n="displayName">$fullname</a>
<a n="givenName">$firstname</a>
<a n="sn">$surname</a>
<a n="description">$jobtitle</a>
<a n="zimbraNotes">$group</a>
</CreateAccountRequest>
</soap:Body>
</soap:Envelope>
"@
# Send the HTTP request
$createAccountResponse = Invoke-RestMethod -Uri $zimbraServerUrl -Method Post -Body $createAccountRequest -Headers $headers
# Output the response
$createAccountResponse
# Clear Admin password variable
Remove-Variable -Name "adminPassword"