@@ -10,24 +10,29 @@ function Invoke-AddTenant {
1010
1111 $APIName = $Request.Params.CIPPEndpoint
1212 Write-LogMessage - headers $Request.Headers - API $APINAME - message ' Accessed this API' - Sev ' Debug'
13+ $Action = $Request.Body.Action ?? $Request.Query.Action
14+ $TenantName = $Request.Body.TenantName ?? $Request.Query.TenantName
15+ $StatusCode = [HttpStatusCode ]::OK
1316
14- switch ($Request .Body. Action ) {
17+ switch ($Action ) {
1518 ' ValidateDomain' {
1619 # Validate the onmicrosoft.com domain
17- $Domain = " $ ( $Request .Body. TenantName ) .onmicrosoft.com"
20+ $Domain = " $ ( $TenantName ) .onmicrosoft.com"
1821 $DomainCheckUri = " https://api.partnercenter.microsoft.com/v1/domains/$Domain "
22+ Write-Information " Checking $Domain "
1923 try {
20- $DomainCheckResponse = New-GraphPOSTRequest - type HEAD - uri $DomainCheckUri - scope ' https://api.partnercenter.microsoft.com/.default' - NoAuthCheck $true
21- } catch {
22- return @ {
23- Status = ' Error '
24+ $null = New-GraphPOSTRequest - type HEAD - uri $DomainCheckUri - scope ' https://api.partnercenter.microsoft.com/.default' - NoAuthCheck $true
25+
26+ $Body = @ {
27+ Success = $false
2428 Message = " The domain '$Domain ' is already in use."
2529 }
30+ } catch {
31+ $Body = @ {
32+ Success = $true
33+ }
2634 }
27- return @ {
28- Status = ' Success'
29- Message = " The domain '$Domain ' is available."
30- }
35+
3136 }
3237 ' AddTenant' {
3338 # Fetch the organization id for Tier 2 CSPs
@@ -41,6 +46,7 @@ function Invoke-AddTenant {
4146 state = ' Error'
4247 resultText = " Failed to retrieve organization profile: $ ( $_.Exception.Message ) "
4348 }
49+ $StatusCode = [HttpStatusCode ]::BadRequest
4450 break
4551 }
4652 }
@@ -51,7 +57,7 @@ function Invoke-AddTenant {
5157 CommerceId = $null
5258 CompanyProfile = @ {
5359 TenantId = $null
54- Domain = ' {0}.onmicrosoft.com' -f $Request .Body. TenantName
60+ Domain = ' {0}.onmicrosoft.com' -f $TenantName
5561 CompanyName = $Request.Body.CompanyName
5662 Attributes = @ { ObjectType = ' CustomerCompanyProfile' }
5763 }
@@ -94,14 +100,15 @@ function Invoke-AddTenant {
94100
95101 $Body = @ {
96102 state = ' Success'
97- resultText = " Tenant created successfully. 'Username is $ ( $Response.userCredentials.userName ) @{0}.onmicrosoft.com'. Click copy to retrieve the password." -f $Request .Body. TenantName
103+ resultText = " Tenant created successfully. 'Username is $ ( $Response.userCredentials.userName ) @{0}.onmicrosoft.com'. Click copy to retrieve the password." -f $TenantName
98104 copyField = $Response.userCredentials.password
99105 }
100106 } catch {
101107 $Body = @ {
102108 state = ' Error'
103109 resultText = " Failed to create tenant: $ ( $_.Exception.Message ) "
104110 }
111+ $StatusCode = [HttpStatusCode ]::BadRequest
105112 }
106113 }
107114 ' ValidateAddress' {
@@ -126,21 +133,21 @@ function Invoke-AddTenant {
126133 }
127134 } catch {
128135 return @ {
129- state = ' Error'
136+ state = ' Error'
130137 resultText = " Address validation failed: $ ( $_.Exception.Message ) "
131138 }
132139 }
133140 }
134141 default {
135142 return @ {
136- state = ' Error'
143+ state = ' Error'
137144 resultText = " Invalid action specified: $ ( $Request.Body.Action ) "
138145 }
139146 }
140147 }
141148
142149 Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
143- StatusCode = [ HttpStatusCode ]::InternalServerError
144- Body = $Body
145- })
150+ StatusCode = $StatusCode
151+ Body = $Body
152+ })
146153}
0 commit comments