Install-Dynamics365Server.psm1

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
function Install-Dynamics365Server {
    [CmdletBinding(DefaultParameterSetName = 'Groups')]
    param (
        [Parameter(ParameterSetName = 'OU', Mandatory=$true)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$true)]
        [string]
        $MediaDir,
        [Parameter(ParameterSetName = 'OU', Mandatory=$true)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$true)]
        [string]
        $SqlServer,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $Patch,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $LicenseKey,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [switch]
        $CreateDatabase,
        [ValidateSet(
            "FrontEnd",
            "BackEnd",
            "DeploymentAdministration",
            "WebApplicationServer",
            "OrganizationWebService",
            "DiscoveryWebService",
            "HelpServer",
            "AsynchronousProcessingService",
            "EmailConnector",
            "SandboxProcessingService",
            "DeploymentTools",
            "DeploymentWebService",
            "VSSWriter"
        )]
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string[]]
        $ServerRoles,
        [Parameter(ParameterSetName = 'OU', Mandatory=$true)]
        [string]
        $OU,
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $PrivUserGroup,
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $SQLAccessGroup,
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $UserGroup,
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $ReportingGroup,
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $PrivReportingGroup,
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [switch]
        $AutoGroupManagementOff,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [pscredential]
        $CrmServiceAccount,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [pscredential]
        $DeploymentServiceAccount,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [pscredential]
        $SandboxServiceAccount,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [pscredential]
        $VSSWriterServiceAccount,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [pscredential]
        $AsyncServiceAccount,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [pscredential]
        $MonitoringServiceAccount,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [switch]
        $CreateWebSite,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [int]
        $WebSitePort,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $WebSiteUrl,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $Organization,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $OrganizationUniqueName,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $ReportingUrl,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $InstallDir,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $IncomingExchangeServer,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $BaseISOCurrencyCode,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $BaseCurrencyName,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $BaseCurrencySymbol,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [int]
        $BaseCurrencyPrecision,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $OrganizationCollation,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [switch]
        $SQM = $false,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [switch]
        $MUOptin = $false,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [switch]
        $Reboot = $false,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [string]
        $LogFilePath = $null,
        [ValidateRange(1,3600)]
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [int]
        $LogFilePullIntervalInSeconds = 30,
        [Parameter(ParameterSetName = 'OU', Mandatory=$false)]
        [Parameter(ParameterSetName = 'Groups', Mandatory=$false)]
        [switch]
        $LogFilePullToOutput = $false
    )
    if ( $ServerRoles ) {
        $serverRoleModel = @{
            FrontEnd = (
                "WebApplicationServer",
                "OrganizationWebService",
                "DiscoveryWebService",
                "HelpServer"
            )
            BackEnd = @(
                "AsynchronousProcessingService",
                "EmailConnector",
                "SandboxProcessingService"
            )
            DeploymentAdministration = @(
                "DeploymentTools",
                "DeploymentWebService",
                "VSSWriter"
            )
        }
        $refinedRoles = $ServerRoles | ForEach-Object {
            $roleOrGroupName = $_;
            switch ( $roleOrGroupName ) {
                "FrontEnd" {
                    Write-Output "WebApplicationServer";
                    Write-Output "OrganizationWebService";
                    Write-Output "DiscoveryWebService";
                    Write-Output "HelpServer";
                    break;
                }
                "BackEnd" {
                    Write-Output "AsynchronousProcessingService";
                    Write-Output "EmailConnector";
                    Write-Output "SandboxProcessingService";
                    break;
                }
                "DeploymentAdministration" {
                    Write-Output "DeploymentTools";
                    Write-Output "DeploymentWebService";
                    Write-Output "VSSWriter";
                    break;
                }
                default {
                    Write-Output $roleOrGroupName;
                    break;
                }
            }
        } | Select-Object -Unique;
        $ServerRoles = $refinedRoles;
        Write-Output "Refined server roles:";
        Write-Output $ServerRoles;
    } else {
        Write-Output "Specific roles are not requested, so full server will be installed";
    }
    $setupFilePath = "$mediaDir\SetupServer.exe";
    if ( Test-Path $setupFilePath ) {
        $fileVersion = [version]( Get-Command $setupFilePath ).FileVersionInfo.FileVersion;
        Write-Output "Version of software to be installed: $($fileVersion.ToString())";
        if ( !$LicenseKey ) {
            if ( $fileVersion.Major -eq 8 ) {
                $LicenseKey = "WCPQN-33442-VH2RQ-M4RKF-GXYH4";
            }
            if ( $fileVersion.Major -eq 9 ) {
                $LicenseKey = "KKNV2-4YYK8-D8HWD-GDRMW-29YTW";
            }
            if ( !$LicenseKey ) {
                $errorMessage = "License key is not specified and cannot be calculated";
                Write-Output $errorMessage;
                Throw $errorMessage;
            }
        }
        if ( Test-Path "$mediaDir\LangPacks" ) {
            $languagePacks = Get-ChildItem "$mediaDir\LangPacks";
            if ( $languagePacks -and $languagePacks.Count -eq 1 ) {
                $fileLanguageCode = [int]$languagePacks.Name;
                Write-Output "Language code of software to be installed: $fileLanguageCode";
                $msCRMRegistryValues = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\MSCRM -ErrorAction Ignore;
                If ( $msCRMRegistryValues ) {
                    $isInstalled = $true;
                    Write-Output "Some version of Dynamics is already installed on this machine"
                    $installedVersion = Get-Dynamics365ServerVersion;
                    if ( $installedVersion.Major -ne $fileVersion.Major ) {
                        $errorMessage = "Another version is already installed: $($installedVersion.ToString())";
                        Write-Output $errorMessage;
                        Throw $errorMessage;
                    }
                    $installedLanguage = Get-Dynamics365ServerLanguage;
                    if ( $installedLanguage -ne $fileLanguageCode ) {
                        $errorMessage = "Another language is already installed: $installedLanguage";
                        Write-Output $errorMessage;
                        Throw $errorMessage;
                    }
                    $installedRoles = Get-Dynamics365ServerRole;
                    Write-Output "Found Roles: ";
                    $installedRoles | Write-Output;
                    if ( $ServerRoles ) {
                        $ServerRoles | ForEach-Object {
                            if ( !( $installedRoles -contains $_ -or ( $_ -eq "VssWriter" -and $installedRoles -contains "VssWriterService" ) ) ) {
                                Write-Output "The $_ role is missing, installation will be done"
                                $isInstalled = $false;
                                $installType = "Configure";
                            }
                        }
                    }
                } else {
                    $isInstalled = $false;
                }
                if ( !$isInstalled ) {
                    if ( $Patch ) {
                        $resourcePathItem = Get-Item $Patch;
                        if ( !$resourcePathItem ) { throw "Specified file or directory '$Patch' could not be found" }
                        if ( $resourcePathItem.PSIsContainer ) {
                            $patchFiles = Get-Item $Patch\Server_KB*_amd64_$fileLanguageCode.msp
                            if ( $patchFiles ) {
                                $patchFilePath = $patchFiles[0].FullName;
                                Write-Output "Found patch file $patchFilePath"
                            } else {
                                throw "File could not be found: $Patch\Server_KB*_amd64_$fileLanguageCode.msp"
                            }
                        } else {
                            $patchFilePath = $Patch;
                        }
                    }
                    $xml = [xml]"";
                    $crmSetupElement = $xml.CreateElement( "CRMSetup" );
                        $serverElement = $xml.CreateElement( "Server" );
                            if ( $installType ) {
                                $installTypeElement = $xml.CreateElement( "InstallType" );
                                    $installTypeElement.InnerText = $installType;
                                $serverElement.AppendChild( $installTypeElement ) | Out-Null;
                            }
                            #Patch is a required element for setup
                            $patchElement = $xml.CreateElement( "Patch" );
                                if ( $Patch -ne $null ) {
                                    $patchElement.SetAttribute( "Update", $true ) | Out-Null;
                                    $patchElement.InnerText = $patchFilePath;
                                } else {
                                    $patchElement.SetAttribute( "Update", $false ) | Out-Null;
                                }
                            $serverElement.AppendChild( $patchElement ) | Out-Null;
                            $licenseKeyElement = $xml.CreateElement( "LicenseKey" );
                                $licenseKeyElement.InnerText = $licenseKey;
                            $serverElement.AppendChild( $licenseKeyElement ) | Out-Null;
                            if ( $installDir ) {
                                $installDirElement = $xml.CreateElement( "InstallDir" );
                                    $installDirElement.InnerText = $installDir;
                                $serverElement.AppendChild( $installDirElement ) | Out-Null;
                            }
                            $databaseElement = $xml.CreateElement( "Database" );
                                $databaseElement.SetAttribute( "create", $CreateDatabase ) | Out-Null;
                            $serverElement.AppendChild( $databaseElement ) | Out-Null;
                            $SQLServerElement = $xml.CreateElement( "SqlServer" );
                                $SQLServerElement.InnerText = $SQLServer;
                            $serverElement.AppendChild( $SQLServerElement ) | Out-Null;
                            if ( $OU) {
                                $OUElement = $xml.CreateElement( "OU" );
                                    $OUElement.InnerText = $OU;
                                $serverElement.AppendChild( $OUElement ) | Out-Null;
                            }
                            if ( $privUserGroup -or $SQLAccessGroup -or $userGroup -or $reportingGroup -or $privReportingGroup -or $AutoGroupManagementOff ) {
                                $groupsElement = $xml.CreateElement( "Groups" );
                                    $groupsElement.SetAttribute( "autogroupmanagementoff", $AutoGroupManagementOff ) | Out-Null;
                                    if ( $privUserGroup ) {
                                        $privUserGroupElement = $xml.CreateElement( "PrivUserGroup" );
                                            $privUserGroupElement.InnerText = $privUserGroup;
                                        $groupsElement.AppendChild( $privUserGroupElement ) | Out-Null;
                                    }
                                    if ( $SQLAccessGroup ) {
                                        $SQLAccessGroupElement = $xml.CreateElement( "SQLAccessGroup" );
                                            $SQLAccessGroupElement.InnerText = $SQLAccessGroup;
                                        $groupsElement.AppendChild( $SQLAccessGroupElement ) | Out-Null;
                                    }
                                    if ( $userGroup ) {
                                        $userGroupElement = $xml.CreateElement( "UserGroup" );
                                            $userGroupElement.InnerText = $userGroup;
                                        $groupsElement.AppendChild( $userGroupElement ) | Out-Null;
                                    }
                                    if ( $reportingGroup ) {
                                        $reportingGroupElement = $xml.CreateElement( "ReportingGroup" );
                                            $reportingGroupElement.InnerText = $reportingGroup;
                                        $groupsElement.AppendChild( $reportingGroupElement ) | Out-Null;
                                    }
                                    if ( $privReportingGroup ) {
                                        $privReportingGroupElement = $xml.CreateElement( "PrivReportingGroup" );
                                            $privReportingGroupElement.InnerText = $privReportingGroup;
                                        $groupsElement.AppendChild( $privReportingGroupElement ) | Out-Null;
                                    }
                                $serverElement.AppendChild( $groupsElement ) | Out-Null;
                            }
                            if ( $CRMServiceAccount ) {
                                $CRMServiceAccountElement = $xml.CreateElement( "CrmServiceAccount" );
                                    $CRMServiceAccountElement.SetAttribute( "type", "DomainUser" ) | Out-Null;
                                    $serviceAccountLoginElement = $xml.CreateElement( "ServiceAccountLogin" );
                                        $serviceAccountLoginElement.InnerText = $CRMServiceAccount.UserName;
                                    $CRMServiceAccountElement.AppendChild( $serviceAccountLoginElement ) | Out-Null;
                                    $serviceAccountPasswordElement = $xml.CreateElement( "ServiceAccountPassword" );
                                        $serviceAccountPasswordElement.InnerText = $CRMServiceAccount.GetNetworkCredential().Password;
                                    $CRMServiceAccountElement.AppendChild( $serviceAccountPasswordElement ) | Out-Null;
                                $serverElement.AppendChild( $CRMServiceAccountElement ) | Out-Null;
                            }
                            if ( $deploymentServiceAccount ) {
                                $deploymentServiceAccountElement = $xml.CreateElement( "DeploymentServiceAccount" );
                                    $deploymentServiceAccountElement.SetAttribute( "type", "DomainUser" ) | Out-Null;
                                    $serviceAccountLoginElement = $xml.CreateElement( "ServiceAccountLogin" );
                                        $serviceAccountLoginElement.InnerText = $deploymentServiceAccount.UserName;
                                    $deploymentServiceAccountElement.AppendChild( $serviceAccountLoginElement ) | Out-Null;
                                    $serviceAccountPasswordElement = $xml.CreateElement( "ServiceAccountPassword" );
                                        $serviceAccountPasswordElement.InnerText = $deploymentServiceAccount.GetNetworkCredential().Password;
                                    $deploymentServiceAccountElement.AppendChild( $serviceAccountPasswordElement ) | Out-Null;
                                $serverElement.AppendChild( $deploymentServiceAccountElement ) | Out-Null;
                            }
                            if ( $sandboxServiceAccount ) {
                                $sandboxServiceAccountElement = $xml.CreateElement( "SandboxServiceAccount" );
                                    $sandboxServiceAccountElement.SetAttribute( "type", "DomainUser" ) | Out-Null;
                                    $serviceAccountLoginElement = $xml.CreateElement( "ServiceAccountLogin" );
                                        $serviceAccountLoginElement.InnerText = $sandboxServiceAccount.UserName;
                                    $sandboxServiceAccountElement.AppendChild( $serviceAccountLoginElement ) | Out-Null;
                                    $serviceAccountPasswordElement = $xml.CreateElement( "ServiceAccountPassword" );
                                        $serviceAccountPasswordElement.InnerText = $sandboxServiceAccount.GetNetworkCredential().Password;
                                    $sandboxServiceAccountElement.AppendChild( $serviceAccountPasswordElement ) | Out-Null;
                                $serverElement.AppendChild( $sandboxServiceAccountElement ) | Out-Null;
                            }
                            if ( $VSSWriterServiceAccount ) {
                                $VSSWriterServiceAccountElement = $xml.CreateElement( "VSSWriterServiceAccount" );
                                    $VSSWriterServiceAccountElement.SetAttribute( "type", "DomainUser" ) | Out-Null;
                                    $serviceAccountLoginElement = $xml.CreateElement( "ServiceAccountLogin" );
                                        $serviceAccountLoginElement.InnerText = $VSSWriterServiceAccount.UserName;
                                    $VSSWriterServiceAccountElement.AppendChild( $serviceAccountLoginElement ) | Out-Null;
                                    $serviceAccountPasswordElement = $xml.CreateElement( "ServiceAccountPassword" );
                                        $serviceAccountPasswordElement.InnerText = $VSSWriterServiceAccount.GetNetworkCredential().Password;
                                    $VSSWriterServiceAccountElement.AppendChild( $serviceAccountPasswordElement ) | Out-Null;
                                $serverElement.AppendChild( $VSSWriterServiceAccountElement ) | Out-Null;
                            }
                            if ( $asyncServiceAccount ) {
                                $asyncServiceAccountElement = $xml.CreateElement( "AsyncServiceAccount" );
                                    $asyncServiceAccountElement.SetAttribute( "type", "DomainUser" ) | Out-Null;
                                    $serviceAccountLoginElement = $xml.CreateElement( "ServiceAccountLogin" );
                                        $serviceAccountLoginElement.InnerText = $asyncServiceAccount.UserName;
                                    $asyncServiceAccountElement.AppendChild( $serviceAccountLoginElement ) | Out-Null;
                                    $serviceAccountPasswordElement = $xml.CreateElement( "ServiceAccountPassword" );
                                        $serviceAccountPasswordElement.InnerText = $asyncServiceAccount.GetNetworkCredential().Password;
                                    $asyncServiceAccountElement.AppendChild( $serviceAccountPasswordElement ) | Out-Null;
                                $serverElement.AppendChild( $asyncServiceAccountElement ) | Out-Null;
                            }
                            if ( $monitoringServiceAccount ) {
                                $monitoringServiceAccountElement = $xml.CreateElement( "MonitoringServiceAccount" );
                                    $monitoringServiceAccountElement.SetAttribute( "type", "DomainUser" ) | Out-Null;
                                    $serviceAccountLoginElement = $xml.CreateElement( "ServiceAccountLogin" );
                                        $serviceAccountLoginElement.InnerText = $monitoringServiceAccount.UserName;
                                    $monitoringServiceAccountElement.AppendChild( $serviceAccountLoginElement ) | Out-Null;
                                    $serviceAccountPasswordElement = $xml.CreateElement( "ServiceAccountPassword" );
                                        $serviceAccountPasswordElement.InnerText = $monitoringServiceAccount.GetNetworkCredential().Password;
                                    $monitoringServiceAccountElement.AppendChild( $serviceAccountPasswordElement ) | Out-Null;
                                $serverElement.AppendChild( $monitoringServiceAccountElement ) | Out-Null;
                            }
                            #WebsiteUrl is required
                            $webSiteUrlElement = $xml.CreateElement( "WebsiteUrl" );
                                $webSiteUrlElement.SetAttribute( "create", $createWebSite ) | Out-Null;
                                $webSiteUrlElement.SetAttribute( "port", $webSitePort ) | Out-Null;
                                $webSiteUrlElement.InnerText = $webSiteUrl;
                            $serverElement.AppendChild( $webSiteUrlElement ) | Out-Null;
                            if ( $ServerRoles ) {
                                $rolesElement = $xml.CreateElement( "Roles" );
                                    $ServerRoles | ForEach-Object {
                                        $roleElement = $xml.CreateElement( "Role" );
                                            $roleElement.SetAttribute( "Name", $_ ) | Out-Null;
                                            $roleElement.SetAttribute( "Action", "Add" ) | Out-Null;
                                        $rolesElement.AppendChild( $roleElement ) | Out-Null;
                                    }
                                $serverElement.AppendChild( $rolesElement ) | Out-Null;
                            }
                            if ( $incomingExchangeServer )
                            {
                                $emailElement = $xml.CreateElement( "Email" );
                                    $incomingExchangeServerElement = $xml.CreateElement( "IncomingExchangeServer" );
                                        $incomingExchangeServerElement.SetAttribute( "name", $incomingExchangeServer ) | Out-Null;
                                    $emailElement.AppendChild( $incomingExchangeServerElement ) | Out-Null;
                                $serverElement.AppendChild( $emailElement ) | Out-Null;
                            }
                            if ( $organizationUniqueName ) {
                                $organizationUniqueNameElement = $xml.CreateElement( "OrganizationUniqueName" );
                                    $organizationUniqueNameElement.InnerText = $organizationUniqueName;
                                $serverElement.AppendChild( $organizationUniqueNameElement ) | Out-Null;
                            }
                            if ( $organization ) {
                                $organizationElement = $xml.CreateElement( "Organization" );
                                    $organizationElement.InnerText = $organization;
                                $serverElement.AppendChild( $organizationElement ) | Out-Null;
                            }
                            if ( $baseISOCurrencyCode -or $baseCurrencyName -or $baseCurrencySymbol -or $baseCurrencyPrecision )
                            {
                                $baseCurrencyElement = $xml.CreateElement( "basecurrency" );
                                if ( $baseISOCurrencyCode )
                                {
                                    $baseCurrencyElement.SetAttribute( "isocurrencycode", $baseISOCurrencyCode ) | Out-Null;
                                }
                                if ( $baseCurrencyName )
                                {
                                    $baseCurrencyElement.SetAttribute( "currencyname", $baseCurrencyName ) | Out-Null;
                                }
                                if ( $baseCurrencySymbol )
                                {
                                    $baseCurrencyElement.SetAttribute( "currencysymbol", $baseCurrencySymbol ) | Out-Null;
                                }
                                if ( $baseCurrencyPrecision )
                                {
                                    $baseCurrencyElement.SetAttribute( "currencyprecision", $baseCurrencyPrecision ) | Out-Null;
                                }
                                $serverElement.AppendChild( $baseCurrencyElement ) | Out-Null;
                            }
                            if ( $organizationCollation )
                            {
                                $organizationCollationElement = $xml.CreateElement( "OrganizationCollation" );
                                    $organizationCollationElement.InnerText = $organizationCollation;
                                $serverElement.AppendChild( $organizationCollationElement ) | Out-Null;
                            }
                            if ( $ReportingUrl ) {
                                $reportingElement = $xml.CreateElement( "Reporting" );
                                    $reportingElement.SetAttribute( "URL", $ReportingUrl ) | Out-Null;
                                $serverElement.AppendChild( $reportingElement ) | Out-Null;
                            }
                            $SQMElement = $xml.CreateElement( "SQM" );
                                $SQMElement.SetAttribute( "optin", $SQM ) | Out-Null;
                            $serverElement.AppendChild( $SQMElement ) | Out-Null;
                            $MUOptinElement = $xml.CreateElement( "muoptin" );
                                $MUOptinElement.SetAttribute( "optin", $MUOptin ) | Out-Null;
                            $serverElement.AppendChild( $MUOptinElement ) | Out-Null;
                            $remoteInstallElement = $xml.CreateElement( "remoteinstall" );
                                $remoteInstallElement.InnerText = $true;
                            $serverElement.AppendChild( $remoteInstallElement ) | Out-Null;
                            $rebootElement = $xml.CreateElement( "Reboot" );
                                $rebootElement.InnerText = $reboot;
                            $serverElement.AppendChild( $rebootElement ) | Out-Null;
                        $crmSetupElement.AppendChild( $serverElement ) | Out-Null;
                    $xml.AppendChild( $crmSetupElement ) | Out-Null;

                    $stringWriter = New-Object System.IO.StringWriter;
                    $xmlWriter = New-Object System.Xml.XmlTextWriter $stringWriter;
                    $xmlWriter.Formatting = "indented";
                    $xml.WriteTo( $xmlWriter );
                    $xmlWriter.Flush();
                    $stringWriter.Flush();
                    $xmlConfig = $stringWriter.ToString();

                    if([String]::IsNullOrEmpty($logFilePath) -eq $True) {
                        $timeStamp = ( Get-Date -Format u ).Replace(" ","-").Replace(":","-");
                        $logFilePath = "$env:APPDATA\Microsoft\MSCRM\Logs\CRMInstallationLog_$timeStamp.txt";
                    }
                    $tempFileName = [guid]::NewGuid().Guid;
                    $tempFilePath = "$env:Temp\$tempFileName.xml";

                    Write-Output "$(Get-Date) Saving configuration temporary to $tempFilePath";
                    Set-Content -Path $tempFilePath -Value $xmlConfig -Encoding utf8;
                    Get-Content $tempFilePath | Write-Debug;

                    Write-Output "$(Get-Date) Starting $setupFilePath";

                    $installCrmScript = {
                        param( $setupFilePath, $tempFilePath, $logFilePath );

                        Write-Output "Start-Process '$setupFilePath' -ArgumentList '/Q /config $tempFilePath /L $logFilePath' -Wait;";
                        Start-Process "$setupFilePath" -ArgumentList "/Q /config $tempFilePath /L $logFilePath" -Wait;
                    }

                    $job = Start-Job -ScriptBlock $installCrmScript -ArgumentList $setupFilePath, $tempFilePath, $logFilePath;
                    Write-Output "$(Get-Date) Started installation job, log will be saved in $logFilePath";
                    $lastLinesCount = 0;
                    $startTime = Get-Date;
                    do {
                        $elapsedTime = $( Get-Date ) - $startTime;
                        $elapsedString = "{0:HH:mm:ss}" -f ( [datetime]$elapsedTime.Ticks );
                        Write-Output "$(Get-Date) Elapsed $elapsedString. Waiting until CRM installation job is done, sleeping $logFilePullIntervalInSeconds sec";
                        Start-Sleep $logFilePullIntervalInSeconds;
                        $jobState = $job.State;
                        if ( $logFilePullToOutput ) {
                            if ( Test-Path $logFilePath ) {
                                $logFileContents = Get-Content $logFilePath -ReadCount 0;
                                $linesCount = $logFileContents.Length;
                                $newLinesCount = $linesCount - $lastLinesCount;
                                if($newLinesCount -gt 0) {
                                    Write-Output "$(Get-Date) - new logs: $newLinesCount lines";
                                    $logFileContents | Select-Object -First $newLinesCount -Skip $lastLinesCount | Write-Output;
                                } else {
                                    Write-Output "$(Get-Date) - no new logs";
                                }
                                $lastLinesCount = $linesCount;
                            }
                        }
                    } until ( $jobState -eq "Completed" )
                    $elapsedTime = $( Get-Date ) - $startTime;
                    $elapsedString = "{0:HH:mm:ss}" -f ( [datetime]$elapsedTime.Ticks );
                    Write-Output "$(Get-Date) Elapsed $elapsedString. Job is complete, output:";
                    Write-Output ( Receive-Job $job );

                    Remove-Job $job;
                    Start-Sleep 10;
                    Remove-Item $tempFilePath;
                    if( (Test-Path $logFilePath) -eq $True) {
                        $errorLines = Get-Content $logFilePath | Select-String -Pattern "Error|" -SimpleMatch;
                        if ( $errorLines ) {
                            Write-Output "Errors from install log:";
                            $errorLines | Foreach-Object {
                                Write-Output $_.Line;
                            }
                        }
                    }
                    $msCRMRegistryValues = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\MSCRM -ErrorAction Ignore;
                    if ( $msCRMRegistryValues ) {
                        $isInstalled = $true;
                        $installedLanguage = Get-Dynamics365ServerLanguage;
                        if ( $installedLanguage -ne $fileLanguageCode ) {
                            $errorMessage = "Another language is installed: $installedLanguage";
                            Write-Output $errorMessage;
                            Throw $errorMessage;
                        }
                    } else {
                        $isInstalled = $false;
                    }
                    if ( $isInstalled ) {
                        $installedRoles = Get-Dynamics365ServerRole;
                        Write-Output "Found Roles: ";
                        $installedRoles | Write-Output;
                        if ( $ServerRoles ) {
                            $ServerRoles | ForEach-Object {
                                if ( !( $installedRoles -contains $_ -or ( $_ -eq "VssWriter" -and $installedRoles -contains "VssWriterService" ) ) ) {
                                    $errorMessage = "Installation job finished but $_ role is not installed";
                                    Write-Output $errorMessage;
                                    Throw $errorMessage;
                                }
                            }
                        }
                        Write-Output "Installation is finished and verified successfully";
                    } else {
                        $errorMessage = "Installation job finished but the product is still not installed";
                        Write-Output $errorMessage;
                        Throw $errorMessage;
                    }
                } else {
                    Write-Output "This product is already installed, skipping";
                }
            } else {
                $errorMessage = "Single language is not found in \LangPacks directory";
                Write-Output $errorMessage;
                Throw $errorMessage;
            }
        } else {
            $errorMessage = "\LangPacks directory is not found";
            Write-Output $errorMessage;
            Throw $errorMessage;
        }
    } else {
        $errorMessage = "\SetupServer.exe file is not found";
        Write-Output $errorMessage;
        Throw $errorMessage;
    }
}