/
ProvisionOnlineInstance.ps1
177 lines (148 loc) · 4.46 KB
/
ProvisionOnlineInstance.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# ProvisionOnlineInstance.ps1
#
[CmdletBinding()]
param(
[string]$ApiUrl,
[string]$Username,
[string]$Password,
[string]$DomainName,
[string]$FriendlyName,
[string]$Purpose,
[string]$InitialUserEmail,
[string]$InstanceType,
[string]$ReleaseId,
[string[]]$TemplateNames,
[string]$LanguageId,
[string]$CurrencyCode,
[string]$CurrencyName,
[int]$CurrencyPrecision,
[string]$CurrencySymbol,
[string]$SecurityGroupId,
[string]$SecurityGroupName,
[bool]$WaitForCompletion = $false,
[int]$SleepDuration = 3,
[string]$PSModulePath
)
$ErrorActionPreference = "Stop"
Write-Verbose 'Entering BackupCRMOnlineInstance.ps1'
#Parameters
Write-Verbose "ApiUrl = $ApiUrl"
Write-Verbose "Username = $Username"
Write-Verbose "DomainName = $DomainName"
Write-Verbose "FriendlyName = $FriendlyName"
Write-Verbose "Purpose = $Purpose"
Write-Verbose "InitialUserEmail = $InitialUserEmail"
Write-Verbose "InstanceType = $InstanceType"
Write-Verbose "ReleaseId = $ReleaseId"
Write-Verbose "TemplateNames = $TemplateNames"
Write-Verbose "LanguageId = $LanguageId"
Write-Verbose "CurrencyCode = $CurrencyCode"
Write-Verbose "CurrencyName = $CurrencyName"
Write-Verbose "CurrencyPrecision = $CurrencyPrecision"
Write-Verbose "CurrencySymbol = $CurrencySymbol"
Write-Verbose "SecurityGroupId = $SecurityGroupId"
Write-Verbose "SecurityGroupName = $SecurityGroupName"
Write-Verbose "WaitForCompletion = $WaitForCompletion"
Write-Verbose "SleepDuration = $SleepDuration"
Write-Verbose "PSModulePath = $PSModulePath"
#Script Location
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Write-Verbose "Script Path: $scriptPath"
#Load Online Management Module
$xrmOnlineModule = $scriptPath + "\Microsoft.Xrm.OnlineManagementAPI.dll"
if ($PSModulePath)
{
$xrmOnlineModule = $PSModulePath + "\Microsoft.Xrm.OnlineManagementAPI.dll"
}
Write-Verbose "Importing Online Management Module: $xrmOnlineModule"
Import-Module $xrmOnlineModule
Write-Verbose "Imported Online Management Module"
#Create Credentials
$SecPassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($Username, $SecPassword)
."$scriptPath\OnlineInstanceFunctions.ps1"
$InstanceInfoParams = @{
BaseLanguage = $LanguageId
DomainName = $DomainName
FriendlyName = $FriendlyName
InitialUserEmail = $InitialUserEmail
InstanceType = $InstanceType
ServiceVersionId = $ReleaseId
Purpose = $Purpose
TemplateList = $TemplateNames
}
if ($CurrencyCode -ne '')
{
$InstanceInfoParams.CurrencyCode = $CurrencyCode
}
if ($CurrencyName -ne '')
{
$InstanceInfoParams.CurrencyName = $CurrencyName
}
if ($CurrencyPrecision -ne 0)
{
$InstanceInfoParams.CurrencyPrecision = $CurrencyPrecision
}
if ($CurrencySymbol -ne '')
{
$InstanceInfoParams.CurrencySymbol = $CurrencySymbol
}
if ($SecurityGroupName -ne '')
{
Write-Verbose "Importing Module AzureAD"
Import-Module AzureAD
Write-Verbose "Imported Module AzureAD"
$group = Get-AzureADGroup -Filter "DisplayName eq '$SecurityGroupName'"
if ($group -ne $null)
{
$SecurityGroupId = $group.ObjectId
}
if ($group -eq $null)
{
throw "$SecurityGroupName not found"
}
}
if ($SecurityGroupId -ne '')
{
$InstanceInfoParams.SecurityGroupId = $SecurityGroupId
}
$instanceInfo = New-CrmInstanceInfo @InstanceInfoParams
$operation = New-CrmInstance -ApiUrl $ApiUrl -NewInstanceInfo $instanceInfo -Credential $Cred
$OperationId = $operation.OperationId
$OperationStatus = $operation.Status
Write-Output "OperationId = $OperationId"
Write-Host "Status = $OperationStatus"
if ($operation.Errors.Count -gt 0)
{
$errorMessage = $operation.Errors[0].Description
throw "Errors encountered : $errorMessage"
}
if ($WaitForCompletion -and ($OperationStatus -ne "Succeeded"))
{
$status = Wait-XrmOperation -ApiUrl $ApiUrl -Cred $Cred -operationId $operation.OperationId
$status
if ($status.Status -ne "Succeeded")
{
throw "Operation status: $status.Status"
}
}
if ($WaitForCompletion)
{
#Sometimes instance is created but the API still returns NOT FOUND (no other instances available).
#Added this delay to give chance for operation to progress.
Start-Sleep -Seconds 30
$provisioning = $true
while ($provisioning)
{
Start-Sleep -Seconds $SleepDuration
$instance = Get-XrmInstanceByName -ApiUrl $ApiUrl -Cred $Cred -InstanceName $DomainName
$State = $instance.State
Write-Verbose "Instance State: $State"
if (($instance -ne $null) -and ($State -eq "Ready"))
{
$provisioning = $false
}
}
}
Write-Verbose 'Leaving ProvisionOnlineInstance.ps1'