/
M365DSCReverse.psm1
849 lines (760 loc) · 32.1 KB
/
M365DSCReverse.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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
<#
.Description
This function orchestrate the export process between Export-M365DSCConfiguration
and the ReverseDSC module.
.Functionality
Internal
#>
function Start-M365DSCConfigurationExtract
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param(
[Parameter()]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter()]
[System.String[]]
$Components,
[Parameter()]
[Switch]
$AllComponents,
[Parameter()]
[System.String]
$Path,
[Parameter()]
[System.String]
$FileName,
[Parameter()]
[System.String]
$ConfigurationName = 'M365TenantConfig',
[Parameter()]
[ValidateRange(1, 100)]
$MaxProcesses = 16,
[Parameter()]
[ValidateSet('AAD', 'SPO', 'EXO', 'INTUNE', 'SC', 'OD', 'O365', 'TEAMS', 'PP', 'PLANNER')]
[System.String[]]
$Workloads,
[Parameter()]
[ValidateSet('Lite', 'Default', 'Full')]
[System.String]
$Mode = 'Default',
[Parameter()]
[System.Boolean]
$GenerateInfo = $false,
[Parameter()]
[System.Collections.Hashtable]
$Filters,
[Parameter()]
[System.String]
$ApplicationId,
[Parameter()]
[System.String]
$ApplicationSecret,
[Parameter()]
[System.String]
$TenantId,
[Parameter()]
[System.String]
$CertificateThumbprint,
[Parameter()]
[System.String]
$CertificatePath,
[Parameter()]
[System.Management.Automation.PSCredential]
$CertificatePassword,
[Parameter()]
[Switch]
$ManagedIdentity
)
# Start by checking to see if a new Version of the tool is available in the
# PowerShell Gallery
try
{
Test-M365DSCModuleValidity
}
catch
{
Add-M365DSCEvent -Message $_ -Source 'M365DSCReverse::Test-M365DSCModuleValidity'
}
try
{
$Global:PartialExportFileName = "$(New-Guid).partial.ps1"
$M365DSCExportStartTime = [System.DateTime]::Now
$InformationPreference = 'Continue'
$VerbosePreference = 'SilentlyContinue'
$WarningPreference = 'SilentlyContinue'
if ($null -ne $Workloads)
{
$Components = Get-M365DSCResourcesByWorkloads -Workloads $Workloads `
-Mode $Mode
}
if ($null -eq $Components -or $Components.Length -eq 0)
{
$ComponentsSpecified = $false
}
else
{
$ComponentsSpecified = $true
}
$ComponentsToSkip = @()
if ($Mode -eq 'Default' -and $null -eq $Components)
{
$ComponentsToSkip = $Global:FullComponents
}
elseif ($Mode -eq 'Lite' -and $null -eq $Components)
{
$ComponentsToSkip = $Global:DefaultComponents + $Global:FullComponents
}
# Check to validate that based on the received authentication parameters
# we are allowed to export the selected components.
$AuthMethods = @()
Write-Host -Object ' '
Write-Host -Object 'Authentication methods specified:'
if ($null -ne $Credential)
{
Write-Host -Object '- Credentials'
$AuthMethods += 'Credentials'
}
if (-not [System.String]::IsNullOrEmpty($CertificateThumbprint))
{
Write-Host -Object '- Service Principal with Certificate Thumbprint'
$AuthMethods += 'CertificateThumbprint'
}
if (-not [System.String]::IsNullOrEmpty($CertificatePath))
{
Write-Host -Object '- Service Principal with Certificate Path'
$AuthMethods += 'CertificatePath'
}
if (-not [System.String]::IsNullOrEmpty($ApplicationSecret))
{
Write-Host -Object '- Service Principal with Application Secret'
$AuthMethods += 'ApplicationWithSecret'
}
if ($ManagedIdentity.IsPresent)
{
Write-Host -Object '- Managed Identity'
$AuthMethods += 'ManagedIdentity'
}
Write-Host -Object ' '
$allSupportedResourcesWithMostSecureAuthMethod = Get-M365DSCComponentsWithMostSecureAuthenticationType -AuthenticationMethod $AuthMethods
# If some resources are not supported based on the Authentication parameters
# received, write a warning.
if ($Components.Length -eq 0)
{
$allResourcesInModule = Get-M365DSCAllResources
$selectedItems = Compare-Object -ReferenceObject $allResourcesInModule `
-DifferenceObject $ComponentsToSkip | Where-Object -FilterScript { $_.SideIndicator -eq '<=' }
$selectedResources = @()
foreach ($item in $selectedItems)
{
$selectedResources += $item.InputObject
}
}
else
{
$selectedResources = $Components
}
try
{
if ($allSupportedResources.Length -eq 0)
{
$allSupportedResources = @()
}
if ($selectedResources.Length -eq 0)
{
$selectedResources = @()
}
[Array]$compareResourcesResult = Compare-Object -ReferenceObject $allSupportedResourcesWithMostSecureAuthMethod.Resource `
-DifferenceObject $selectedResources | Where-Object -FilterScript { $_.SideIndicator -eq '=>' }
}
catch
{
Write-Verbose -Message $_
}
if ($null -ne $compareResourcesResult)
{
# The client is trying to extract act least one resource which is not supported
# using only the provided authentication parameters;
$resourcesNotSupported = @()
foreach ($resource in $compareResourcesResult)
{
$resourcesNotSupported += $resource.InputObject
# Skip resources that are not supported;
$ComponentsToSkip += $resource.InputObject
}
Write-Host '[WARNING]' -NoNewline -ForegroundColor Yellow
Write-Host ' Based on the provided Authentication parameters, the following resources cannot be extracted: ' -ForegroundColor Gray
Write-Host "$resourcesNotSupported" -ForegroundColor Gray
# If all selected resources are not valid based on the authentication method used, simply return.
if ($ComponentsToSkip.Length -eq $selectedResources.Length)
{
return
}
}
# Get Tenant Info
$organization = ''
if ($AuthMethods -contains 'CertificateThumbprint' -or `
$AuthMethods -contains 'CertificatePath' -or `
$AuthMethods -contains 'ApplicationWithSecret')
{
$organization = Get-M365DSCTenantDomain -ApplicationId $ApplicationId `
-TenantId $TenantId `
-CertificateThumbprint $CertificateThumbprint `
-ApplicationSecret $ApplicationSecret `
-CertificatePath $CertificatePath
}
elseif ($AuthMethods -Contains 'Credentials')
{
if ($null -ne $Credential -and $Credential.UserName.Contains('@'))
{
$organization = $Credential.UserName.Split('@')[1]
}
}
elseif ($AuthMethods -contains 'ManagedIdentity')
{
$ConnectionMode = New-M365DSCConnection -Workload 'MicrosoftGraph' -InboundParameters @{'ManagedIdentity' = $true; 'TenantId' = $TenantId }
$organization = Get-M365DSCTenantDomain -TenantId $TenantId -ManagedIdentity
}
$AzureAutomation = $false
if ($organization.IndexOf('.') -gt 0)
{
$organization = $organization.Split('.')[0]
}
[array] $version = Get-Module 'Microsoft365DSC'
$version = $version[0].Version
$DSCContent = [System.Text.StringBuilder]::New()
$DSCContent.Append("# Generated with Microsoft365DSC version $version`r`n") | Out-Null
$DSCContent.Append("# For additional information on how to use Microsoft365DSC, please visit https://aka.ms/M365DSC`r`n") | Out-Null
$DSCContent.Append("param (`r`n") | Out-Null
# Add script parameters, only add PSCredential parameters. All other information
# is placed in the Configuration Data file.
$newline = $false
switch ($AuthMethods)
{
'CertificatePath'
{
if ($newline)
{
$DSCContent.Append("`r`n") | Out-Null
}
$DSCContent.Append(" [parameter()]`r`n") | Out-Null
$DSCContent.Append(" [System.Management.Automation.PSCredential]`r`n") | Out-Null
$DSCContent.Append(" `$CertificatePassword`r`n") | Out-Null
$newline = $true
}
'Credentials'
{
if ($newline)
{
$DSCContent.Append("`r`n") | Out-Null
}
$DSCContent.Append(" [parameter()]`r`n") | Out-Null
$DSCContent.Append(" [System.Management.Automation.PSCredential]`r`n") | Out-Null
$DSCContent.Append(" `$Credential`r`n") | Out-Null
$newline = $true
}
}
$DSCContent.Append(")`r`n`r`n") | Out-Null
# Create Configuration section
if (-not [System.String]::IsNullOrEmpty($FileName))
{
$FileParts = $FileName.Split('.')
if ([System.String]::IsNullOrEmpty($ConfigurationName))
{
$ConfigurationName = $FileName.Replace('.' + $FileParts[$FileParts.Length - 1], '')
}
}
if ([System.String]::IsNullOrEmpty($ConfigurationName))
{
$ConfigurationName = 'M365TenantConfig'
}
$DSCContent.Append("Configuration $ConfigurationName`r`n{`r`n") | Out-Null
# Adding Parameter section
$DSCContent.Append(" param (`r`n") | Out-Null
$newline = $false
$postParamContent = [System.Text.StringBuilder]::New()
switch ($AuthMethods)
{
{ $_ -in 'CertificateThumbprint', 'CertificatePath', 'ApplicationWithSecret' }
{
$postParamContent.Append(" `$OrganizationName = `$ConfigurationData.NonNodeData.OrganizationName`r`n") | Out-Null
Add-ConfigurationDataEntry -Node 'NonNodeData' `
-Key 'OrganizationName' `
-Value $organization `
-Description "Tenant's default verified domain name"
Add-ConfigurationDataEntry -Node 'NonNodeData' `
-Key 'ApplicationId' `
-Value $ApplicationId `
-Description 'Azure AD Application Id for Authentication'
Add-ConfigurationDataEntry -Node 'NonNodeData' `
-Key 'TenantId' `
-Value $TenantId `
-Description 'The Id or Name of the tenant to authenticate against'
}
'CertificateThumbprint'
{
Add-ConfigurationDataEntry -Node 'NonNodeData' `
-Key 'CertificateThumbprint' `
-Value $CertificateThumbprint `
-Description 'Thumbprint of the certificate to use for authentication'
}
'CertificatePath'
{
if ($newline)
{
$DSCContent.Append("`r`n") | Out-Null
}
$DSCContent.Append(" [parameter()]`r`n") | Out-Null
$DSCContent.Append(" [System.Management.Automation.PSCredential]`r`n") | Out-Null
$DSCContent.Append(" `$CertificatePassword`r`n") | Out-Null
if ($newline)
{
$postParamContent.Append("`r`n") | Out-Null
}
$postParamContent.Append(" if (`$null -eq `$CertificatePassword)`r`n") | Out-Null
$postParamContent.Append(" {`r`n") | Out-Null
$postParamContent.Append(" <# Credentials #>`r`n") | Out-Null
$postParamContent.Append(" }`r`n") | Out-Null
$postParamContent.Append(" else`r`n") | Out-Null
$postParamContent.Append(" {`r`n") | Out-Null
$postParamContent.Append(" `$CredsCertificatePassword = `$CertificatePassword`r`n") | Out-Null
$postParamContent.Append(" }`r`n`r`n") | Out-Null
Add-ConfigurationDataEntry -Node 'NonNodeData' `
-Key 'CertificatePath' `
-Value $CertificatePath `
-Description 'Local path to the .pfx certificate to use for authentication'
$newline = $true
# Add the Certificate Password to the Credentials List
Save-Credentials -UserName 'certificatepassword'
}
'ApplicationWithSecret'
{
Add-ConfigurationDataEntry -Node 'NonNodeData' `
-Key 'ApplicationSecret' `
-Value $ApplicationSecret `
-Description 'Azure AD Application Secret for Authentication'
}
'Credentials'
{
if ($newline)
{
$DSCContent.Append("`r`n") | Out-Null
}
$DSCContent.Append(" [parameter()]`r`n") | Out-Null
$DSCContent.Append(" [System.Management.Automation.PSCredential]`r`n") | Out-Null
$DSCContent.Append(" `$Credential`r`n") | Out-Null
if ($newline)
{
$postParamContent.Append("`r`n") | Out-Null
}
$postParamContent.Append(" if (`$null -eq `$Credential)`r`n") | Out-Null
$postParamContent.Append(" {`r`n") | Out-Null
$postParamContent.Append(" <# Credentials #>`r`n") | Out-Null
$postParamContent.Append(" }`r`n") | Out-Null
$postParamContent.Append(" else`r`n") | Out-Null
$postParamContent.Append(" {`r`n") | Out-Null
$postParamContent.Append(" `$CredsCredential = `$Credential`r`n") | Out-Null
$postParamContent.Append(" }`r`n`r`n") | Out-Null
$postParamContent.Append(" `$OrganizationName = `$CredsCredential.UserName.Split('@')[1]`r`n") | Out-Null
$newline = $true
# Add the Credential to the Credentials List
Save-Credentials -UserName 'credential'
}
'ManagedIdentity'
{
Add-ConfigurationDataEntry -Node 'NonNodeData' `
-Key 'TenantId' `
-Value $TenantId `
-Description 'The Id or Name of the tenant to authenticate against'
}
}
$DSCContent.Append(" )`r`n`r`n") | Out-Null
$DSCContent.Append($postParamContent.ToString()) | Out-Null
$DSCContent.Append("`r`n") | Out-Null
# Create Node section
$DSCContent.Append(" Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '$version'`r`n`r`n") | Out-Null
$DSCContent.Append(" Node localhost`r`n") | Out-Null
$DSCContent.Append(" {`r`n") | Out-Null
Add-ConfigurationDataEntry -Node 'localhost' `
-Key 'ServerNumber' `
-Value '0' `
-Description 'Default Value Used to Ensure a Configuration Data File is Generated'
$ResourcesPath = Join-Path -Path $PSScriptRoot `
-ChildPath '..\DSCResources\' `
-Resolve
$AllResources = Get-ChildItem $ResourcesPath -Recurse | Where-Object { $_.Name -like 'MSFT_*.psm1' }
$i = 1
$ResourcesToExport = @()
$ResourcesPath = @()
foreach ($ResourceModule in $AllResources)
{
try
{
$resourceName = $ResourceModule.Name.Split('.')[0] -replace 'MSFT_', ''
if ((($Components -and ($Components -contains $resourceName)) -or $AllComponents -or `
(-not $Components -and $null -eq $Workloads)) -and `
($ComponentsSpecified -or ($ComponentsToSkip -notcontains $resourceName)) -and `
$resourcesNotSupported -notcontains $resourceName)
{
$ResourcesToExport += $ResourceName
$ResourcesPath += $ResourceModule
}
}
catch
{
New-M365DSCLogEntry -Error $_ -Message $ResourceModule.Name -Source "[M365DSCReverse]$($ResourceModule.Name)"
}
}
# Retrieve the list of Workloads represented by the resources to export and pre-authenticate to each one;
if ($ResourcesToExport.Length -gt 0)
{
$WorkloadsToConnectTo = Get-M365DSCWorkloadsListFromResourceNames -ResourceNames $ResourcesToExport
}
foreach ($Workload in $WorkloadsToConnectTo)
{
Write-Host "Connecting to {$Workload}..." -NoNewline
$ConnectionParams = @{
Workload = $Workload
ApplicationId = $ApplicationId
ApplicationSecret = $ApplicationSecret
TenantId = $TenantId
CertificateThumbprint = $CertificateThumbprint
CertificatePath = $CertificatePath
CertificatePassword = $CertificatePassword.Password
Credential = $Credential
Identity = $ManagedIdentity.IsPresent
}
try
{
Connect-M365Tenant @ConnectionParams | Out-Null
Write-Host $Global:M365DSCEmojiGreenCheckmark
}
catch
{
Write-Host $Global:M365DSCEmojiRedX
throw $_
}
}
foreach ($resource in $ResourcesPath)
{
$resourceName = $resource.Name.Split('.')[0] -replace 'MSFT_', ''
$mostSecureAuthMethod = ($allSupportedResourcesWithMostSecureAuthMethod | Where-Object { $_.Resource -eq $resourceName }).AuthMethod
Import-Module $resource.FullName | Out-Null
$MaxProcessesExists = (Get-Command 'Export-TargetResource').Parameters.Keys.Contains('MaxProcesses')
$FilterExists = (Get-Command 'Export-TargetResource').Parameters.Keys.Contains('Filter')
$parameters = @{}
switch ($mostSecureAuthMethod)
{
{ $_ -in 'CertificateThumbprint', 'CertificatePath', 'ApplicationSecret' }
{
$parameters.Add('ApplicationId', $ApplicationId)
$parameters.Add('TenantId', $TenantId)
}
'CertificateThumbprint'
{
$parameters.Add('CertificateThumbprint', $CertificateThumbprint)
}
'CertificatePath'
{
$parameters.Add('CertificatePath', $CertificatePath)
$parameters.Add('CertificatePassword', $CertificatePassword)
}
'ApplicationSecret'
{
$applicationSecretValue = New-Object System.Management.Automation.PSCredential ('ApplicationSecret', (ConvertTo-SecureString $ApplicationSecret -AsPlainText -Force));
$parameters.Add('ApplicationSecret', $applicationSecretValue)
}
'Credentials'
{
$parameters.Add('Credential', $Credential)
}
'ManagedIdentity'
{
$parameters.Add('ManagedIdentity', $ManagedIdentity)
$parameters.Add('TenantId', $TenantId)
}
}
if ($MaxProcessesExists -and -not [System.String]::IsNullOrEmpty($MaxProcesses))
{
$parameters.Add('MaxProcesses', $MaxProcesses)
}
if ($FilterExists -and -not [System.String]::IsNullOrEmpty($Filter))
{
$parameters.Add('Filter', $Filter)
}
if ($ComponentsToSkip -notcontains $resourceName)
{
Write-Host "[$i/$($ResourcesToExport.Length)] Extracting [" -NoNewline
Write-Host $resourceName -ForegroundColor Green -NoNewline
Write-Host '] using {' -NoNewline
Write-Host $mostSecureAuthMethod -ForegroundColor Cyan -NoNewline
Write-Host '}...' -NoNewline
$exportString = [System.Text.StringBuilder]::New()
if ($GenerateInfo)
{
$exportString.Append("`r`n # For information on how to use this resource, please refer to:`r`n") | Out-Null
$exportString.Append(" # https://github.com/microsoft/Microsoft365DSC/wiki/$($resource.Name.Split('.')[0] -replace 'MSFT_', '')`r`n") | Out-Null
}
# Check if filters for the current resource were specified.
$resourceFilter = $null
$resourceName = $resource.Name.Split('.')[0] -replace 'MSFT_', ''
if ($filters -ne $null -and $filters.Keys.Contains($resourceName))
{
$resourceFilter = $Filters.($resource.Name.Split('.')[0] -replace 'MSFT_', '')
if ($FilterExists)
{
$parameters.Add('Filter', $resourceFilter)
}
elseif ($null -ne $resourceFilter)
{
Write-Host " `r`n$($Global:M365DSCEmojiYellowCircle) You specified a filter for resource {$resourceName} but it doesn't support filters. Filter will be ignored and all instances of the resource will be captured."
}
}
$exportString.Append((Export-TargetResource @parameters)) | Out-Null
$i++
}
$DSCContent.Append($exportString.ToString()) | Out-Null
}
# Close the Node and Configuration declarations
$DSCContent.Append(" }`r`n") | Out-Null
$DSCContent.Append("}`r`n") | Out-Null
# Azure Automation Check
$AzureAutomation = $false
if ('AzureAutomation/' -eq $env:AZUREPS_HOST_ENVIRONMENT)
{
$AzureAutomation = $true
}
$launchCommand = "$ConfigurationName -ConfigurationData .\ConfigurationData.psd1"
switch ($AuthMethods)
{
'CertificatePath'
{
$certCreds = $Global:CredsRepo[0]
$credsContent = ''
$credsContent += ' ' + (Resolve-Credentials $certCreds) + " = Get-Credential -Message `"Certificate Password`""
$credsContent += "`r`n"
$startPosition = $DSCContent.IndexOf('<# Credentials #>') + 19
$DSCContent = $DSCContent.Insert($startPosition, $credsContent)
$launchCommand += " -CertificatePassword `$CertificatePassword"
}
'Credentials'
{
#region Add the Prompt for Required Credentials at the top of the Configuration
$credsContent = ''
foreach ($credEntry in $Global:CredsRepo)
{
if (!$credEntry.ToLower().StartsWith('builtin'))
{
if (!$AzureAutomation)
{
$credsContent += ' ' + (Resolve-Credentials $credEntry) + " = Get-Credential -Message `"Credentials`"`r`n"
}
else
{
$resolvedName = (Resolve-Credentials $credEntry)
$credsContent += ' ' + $resolvedName + ' = Get-AutomationPSCredential -Name ' + ($resolvedName.Replace('$', '')) + "`r`n"
}
}
}
$credsContent += "`r`n"
$startPosition = $DSCContent.ToString().IndexOf('<# Credentials #>') + 19
$DSCContent = $DSCContent.Insert($startPosition, $credsContent)
$launchCommand += " -Credential `$Credential"
#endregion
}
}
$DSCContent.Append("`r`n") | Out-Null
$DSCContent.Append($launchCommand) | Out-Null
#region Benchmarks
$M365DSCExportEndTime = [System.DateTime]::Now
$timeTaken = New-TimeSpan -Start ($M365DSCExportStartTime.ToString()) `
-End ($M365DSCExportEndTime.ToString())
Write-Host "$($Global:M365DSCEmojiHourglass) Export took {" -NoNewline
Write-Host "$($timeTaken.TotalSeconds) seconds" -NoNewline -ForegroundColor Cyan
Write-Host '}'
#endregion
$sessions = Get-PSSession | Where-Object -FilterScript { $_.Name -like 'SfBPowerShellSessionViaTeamsModule_*' -or `
$_.Name -like 'ExchangeOnlineInternalSession*' }
foreach ($session in $sessions)
{
try
{
Write-Verbose -Message "Closing PSSession {$($session.Name)}"
Remove-PSSession $session | Out-Null
}
catch
{
Write-Verbose -Message $_
}
}
$shouldOpenOutputDirectory = $false
#region Prompt the user for a location to save the extract and generate the files
if ([System.String]::IsNullOrEmpty($Path))
{
$shouldOpenOutputDirectory = $true
$OutputDSCPath = Read-Host "`r`nDestination Path"
}
else
{
$OutputDSCPath = $Path
}
if ([System.String]::IsNullOrEmpty($OutputDSCPath))
{
$OutputDSCPath = '.'
}
while ((Test-Path -Path $OutputDSCPath -PathType Container -ErrorAction SilentlyContinue) -eq $false)
{
try
{
Write-Host "Directory `"$OutputDSCPath`" doesn't exist; creating..."
New-Item -Path $OutputDSCPath -ItemType Directory | Out-Null
if ($?)
{
break
}
}
catch
{
Write-Warning "$($_.Exception.Message)"
Write-Warning "Could not create folder $OutputDSCPath!"
}
$OutputDSCPath = Read-Host 'Please Provide Output Folder for DSC Configuration (Will be Created as Necessary)'
}
<## Ensures the path we specify ends with a Slash, in order to make sure the resulting file path is properly structured. #>
if (!$OutputDSCPath.EndsWith('\') -and !$OutputDSCPath.EndsWith('/'))
{
$OutputDSCPath += '\'
}
#endregion
#region Copy Downloaded files back into output folder
if (($null -ne $Components -and
$Components.Contains('SPOApp')) -or
$AllComponents -or ($null -ne $Workloads -and $Workloads.Contains('SPO')))
{
if ($ConnectionMode -eq 'credential')
{
$filesToDownload = Get-AllSPOPackages -Credential $Credential
}
else
{
$filesToDownload = Get-AllSPOPackages -ApplicationId $ApplicationId -CertificateThumbprint $CertificateThumbprint `
-CertificatePassword $CertificatePassword -TenantId $TenantId -CertificatePath $CertificatePath -ManagedIdentity:$ManagedIdentity.IsPresent
}
if ($filesToDownload.Count -gt 0)
{
foreach ($fileToCopy in $filesToDownload)
{
$filePath = Join-Path $env:Temp $fileToCopy.Name -Resolve
$destPath = Join-Path $OutputDSCPath $fileToCopy.Name
Copy-Item -Path $filePath -Destination $destPath
}
}
}
#endregion
if (-not [System.String]::IsNullOrEmpty($FileName))
{
$outputDSCFile = $OutputDSCPath + $FileName
}
else
{
$outputDSCFile = $OutputDSCPath + 'M365TenantConfig.ps1'
}
$DSCContent.ToString() | Out-File $outputDSCFile
if (!$AzureAutomation)
{
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
$LCMConfig = Get-DscLocalConfigurationManager
if ($null -ne $LCMConfig.CertificateID)
{
try
{
# Export the certificate assigned to the LCM
$certPath = $OutputDSCPath + 'M365DSC.cer'
Export-Certificate -FilePath $certPath `
-Cert "cert:\LocalMachine\my\$($LCMConfig.CertificateID)" `
-Type CERT `
-NoClobber | Out-Null
}
catch
{
Write-Verbose -Message $_
Add-M365DSCEvent -Message $_ -EntryType 'Error' `
-EventID 1 -Source $($MyInvocation.MyCommand.Source)
}
Add-ConfigurationDataEntry -Node 'localhost' `
-Key 'CertificateFile' `
-Value 'M365DSC.cer' `
-Description 'Path of the certificate used to encrypt credentials in the file.'
}
}
else
{
Write-Host "$($Global:M365DSCEmojiYellowCircle) Warning {" -NoNewline
Write-Host "Cannot export Local Configuration Manager settings. This process isn't executed with Administrative Privileges!" -NoNewline -ForegroundColor DarkCyan
Write-Host '}'
}
$outputConfigurationData = $OutputDSCPath + 'ConfigurationData.psd1'
New-ConfigurationDataDocument -Path $outputConfigurationData
}
if ($shouldOpenOutputDirectory)
{
try
{
Invoke-Item -Path $OutputDSCPath
}
catch
{
Write-Verbose -Message $_
}
}
}
catch
{
Write-Host $_
$partialPath = Join-Path $env:TEMP -ChildPath "$($Global:PartialExportFileName)"
Write-Host "Partial Export file was saved at: $partialPath"
}
}
<#
.Description
This function gets all resources for the specified workloads
.Functionality
Internal, Hidden
#>
function Get-M365DSCResourcesByWorkloads
{
[CmdletBinding()]
[OutputType([System.String[]])]
param(
[Parameter(Mandatory = $true)]
[System.String[]]
$Workloads,
[Parameter()]
[ValidateSet('Lite', 'Default', 'Full')]
[System.String]
$Mode = 'Default'
)
$modules = Get-ChildItem -Path ($PSScriptRoot + '\..\DSCResources\') -Recurse -Filter '*.psm1'
$Components = @()
foreach ($Workload in $Workloads)
{
Write-Host "Finding all resources for workload {$Workload} and Mode {$Mode}" -ForegroundColor Gray
foreach ($resource in $modules)
{
$ResourceName = $resource.Name -replace 'MSFT_', '' -replace '.psm1', ''
if ($ResourceName.StartsWith($Workload, 'CurrentCultureIgnoreCase') -and
($Mode -eq 'Full' -or `
($Mode -eq 'Default' -and -not $Global:FullComponents.Contains($ResourceName)) -or `
($Mode -eq 'Lite' -and -not $Global:FullComponents.Contains($ResourceName) -and -not $Global:DefaultComponents.Contains($ResourceName))))
{
$Components += $ResourceName
}
}
}
return $Components
}
Export-ModuleMember -Function @(
'Start-M365DSCConfigurationExtract'
)