-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunAzureGRAPHDump.ps1
More file actions
560 lines (461 loc) · 22.3 KB
/
RunAzureGRAPHDump.ps1
File metadata and controls
560 lines (461 loc) · 22.3 KB
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
#A script which uses the GRAPH API to extract useful and interseting data from Azure Active Directory
#It will then take that information and format it into an excel book
#Make sure it's not done in the x86 powershell
function AzureGraphDump{
#login to AzureAD
Connect-AzureAD | Out-Null
Connect-MsolService | Out-Null
Connect-ExchangeOnline | Out-Null
function Get-ConditionalAccessPolicies {
# Run the az rest command and store the output as a string
$output = az rest --method GET --uri https://graph.microsoft.com/v1.0/identity/conditionalAccess/policies | Out-String
# Convert the output string to a JSON object
$json = ConvertFrom-Json $output
# Extract the policy objects from the JSON object
$policies = $json.value
# Return the policies
return $policies
# Display success message
Write-Host "Successfully retrieved $($policies.count) conditional access policies."
}
Get-ConditionalAccessPolicies
function Export-PoliciesToCSV {
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Path,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[array]$Policies
)
$Policies | Select-Object -Property createdDateTime,displayName,grantControls,id,modifiedDateTime,@{Name="ApplicationEnforcedRestrictions";Expression={$_.sessionControls.applicationEnforcedRestrictions}},@{Name="CloudAppSecurity";Expression={$_.sessionControls.cloudAppSecurity}},@{Name="DisableResilienceDefaults";Expression={$_.sessionControls.disableResilienceDefaults}},@{Name="PersistentBrowser";Expression={$_.sessionControls.persistentBrowser}},@{Name="SignInFrequency";Expression={$_.sessionControls.signInFrequency}} | Export-Csv -Path $Path -NoTypeInformation
}
$policies = Get-ConditionalAccessPolicies
try {
Export-PoliciesToCSV -Path "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\Policies.csv" -Policies $policies
} catch {
# Do nothing or handle the exception as needed
}
# Get all global admins in the organization and convert to a csv
function Get-GlobalAdmins{
$globalAdmins = Get-AzureADDirectoryRoleMember -ObjectId (Get-AzureADDirectoryRole | Where-Object {$_.displayName -eq 'Global Administrator'}).ObjectId | Get-AzureADUser
# Select properties to export
$properties = 'DisplayName', 'Mail', @{Name='OtherMails';Expression={$_.OtherMails -join ';'}}, @{Name='ProxyAddresses';Expression={$_.ProxyAddresses -join ';'}}, 'TelephoneNumber', 'UserPrincipalName', 'ObjectId', 'AccountEnabled'
# Export global admins to CSV file
$globalAdmins | Select-Object $properties | Export-Csv -Path "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\global_admins.csv" -NoTypeInformation
}
Get-GlobalAdmins
#Check for users w/ MFA Disabled/Enabled/Enforced
<#
Enabled: The user has been enrolled in MFA but has not completed the registration process. They will be prompted to complete the registration process the next time they sign in
Enforced: The user has been enrolled and has completed the MFA registration process. Users are automatically switched from enabled to enforced when they register for Azure AD MFA
Disabled: This is the default state for a new user that has not been enrolled in MFA
#>
function MfaCheck {
$users = Get-MsolUser -All | Select-Object UserPrincipalName, DisplayName, @{Label="MFA Status"; Expression={If($_.StrongAuthenticationMethods.Count -eq 0){"Disabled"}Else{"Enabled"}}}, @{Label="Enabled"; Expression={$_.UserPrincipalName -ne $null -and $_.BlockCredential -ne "True"}}
$users | Export-Csv -Path "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\MFAEnabledUsers.csv" -NoTypeInformation
$users
}
MfaCheck
#Get legacy mail protocols
Get-CASMailbox -ResultSize unlimited | Select-Object PrimarySMTPAddress, ActiveSyncEnabled, EwsEnabled, PopEnabled, ImapEnabled | Export-Csv -Path "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\LegacyProtocols.csv" -NoTypeInformation
function Export-ConditionalAccessPoliciesToExcel {
$caFilePath = "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\Policies.csv"
if (Test-Path $caFilePath){
$data = Import-Csv -Path $caFilePath -Header "createdDateTime","displayName","grantControls","id","modifiedDateTime"
if ($data.Count -eq 0) {
Write-Host "No policies found in the CSV file. Exiting..."
return
}
# Load the Excel COM object
$excel = New-Object -ComObject Excel.Application
# Make Excel visible
$excel.Visible = $true
# Add a new workbook
$workbook = $excel.Workbooks.Add()
# Get the first worksheet
$worksheet = $workbook.Worksheets.Item(1)
# Set the header names and format
$worksheet.Cells.Item(1,1) = "Created Date Time"
$worksheet.Cells.Item(1,1).Font.Bold = $true
$worksheet.Cells.Item(1,1).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,2) = "Display Name"
$worksheet.Cells.Item(1,2).Font.Bold = $true
$worksheet.Cells.Item(1,2).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,3) = "Grant Controls"
$worksheet.Cells.Item(1,3).Font.Bold = $true
$worksheet.Cells.Item(1,3).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,4) = "ID"
$worksheet.Cells.Item(1,4).Font.Bold = $true
$worksheet.Cells.Item(1,4).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,5) = "Modified Date Time"
$worksheet.Cells.Item(1,5).Font.Bold = $true
$worksheet.Cells.Item(1,5).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,6) = "Application Enforced Restrictions"
$worksheet.Cells.Item(1,6).Font.Bold = $true
$worksheet.Cells.Item(1,6).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,7) = "Cloud App Security"
$worksheet.Cells.Item(1,7).Font.Bold = $true
$worksheet.Cells.Item(1,7).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,8) = "Disable Resilience Defaults"
$worksheet.Cells.Item(1,8).Font.Bold = $true
$worksheet.Cells.Item(1,8).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,9) = "Persistent Browser"
$worksheet.Cells.Item(1,9).Font.Bold = $true
$worksheet.Cells.Item(1,9).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,10) = "Sign In Frequency"
$worksheet.Cells.Item(1,10).Font.Bold = $true
$worksheet.Cells.Item(1,10).Font.ColorIndex = 2 # white
# set the background color of the header row
$headerRange = $worksheet.Range("A1:J1")
$headerRange.Interior.ColorIndex = 30
# Set the data starting row
$row = 2
# Loop through each policy and populate the Excel worksheet
foreach ($policy in $policies) {
# Populate the Created Date Time column
$worksheet.Cells.Item($row,1) = $policy.createdDateTime
# Populate the Display Name column
$worksheet.Cells.Item($row,2) = $policy.displayName
# Populate the Grant Controls column
$worksheet.Cells.Item($row,3) = $policy.grantControls
# Populate the ID column
$worksheet.Cells.Item($row,4) = $policy.id
# Populate the Modified Date Time column
$worksheet.Cells.Item($row,5) = $policy.modifiedDateTime
# Populate the Application Enforced Restrictions column
$worksheet.Cells.Item($row,6) = $policy.sessionControls.applicationEnforcedRestrictions
# Populate the Cloud App Security column
$worksheet.Cells.Item($row,7) = $policy.sessionControls.cloudAppSecurity
# Populate the Disable Resilience Defaults column
$worksheet.Cells.Item($row,8) = $policy.sessionControls.disableResilienceDefaults
# Populate the Persistent Browser column
$worksheet.Cells.Item($row,9) = $policy.sessionControls.persistentBrowser
# Populate the Sign In Frequency column
$worksheet.Cells.Item($row,10) = $policy.sessionControls.signInFrequency
# Move to the next row
$row += 1
}
# set the background color of the rows
$range = $worksheet.Range("A2:J$row")
$fill = $range.Interior
$fill.Pattern = 1
$fill.PatternColorIndex = -4105
$fill.ThemeColor = 1
$fill.TintAndShade = 0.599993896298105
# set the background color of every row to a different color
for ($i = 2; $i -le $row; $i++) {
$rangeA = $worksheet.Range("A$i")
$rangeB = $worksheet.Range("B$i")
$rangeC = $worksheet.Range("C$i")
$rangeD = $worksheet.Range("D$i")
$rangeE = $worksheet.Range("E$i")
$rangeF = $worksheet.Range("F$i")
$rangeG = $worksheet.Range("G$i")
$rangeH = $worksheet.Range("H$i")
$rangeI = $worksheet.Range("I$i")
$rangeJ = $worksheet.Range("J$i")
if (($i % 2) -eq 0) {
$rangeA.Interior.ColorIndex = 15
$rangeB.Interior.ColorIndex = 15
$rangeC.Interior.ColorIndex = 15
$rangeD.Interior.ColorIndex = 15
$rangeE.Interior.ColorIndex = 15
$rangeF.Interior.ColorIndex = 15
$rangeG.Interior.ColorIndex = 15
$rangeH.Interior.ColorIndex = 15
$rangeI.Interior.ColorIndex = 15
$rangeJ.Interior.ColorIndex = 15
}
else {
$rangeA.Interior.ColorIndex = 2
$rangeB.Interior.ColorIndex = 2
$rangeC.Interior.ColorIndex = 2
$rangeD.Interior.ColorIndex = 2
$rangeE.Interior.ColorIndex = 2
$rangeF.Interior.ColorIndex = 2
$rangeG.Interior.ColorIndex = 2
$rangeH.Interior.ColorIndex = 2
$rangeI.Interior.ColorIndex = 2
$rangeJ.Interior.ColorIndex = 2
}
}
# autofit the columns
$range = $worksheet.Range("A:J")
$range.EntireColumn.AutoFit() | Out-Null
# Save the workbook
$workbook.SaveAs("C:\Users\$([Environment]::UserName)\Desktop\AzFiles\caPolicies.xlsx")
# Close the workbook and Excel application
$workbook.Close($true)
$excel.Quit()
# release the COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
}
else {
Write-Host "Conditional Access Policies were not found. Check to see if default policies are enabled!" -ForegroundColor DarkYellow
}
}
Export-ConditionalAccessPoliciesToExcel
function Export-GlobalAdminsToExcel {
$gaFilePath = "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\global_admins.csv"
if (Test-Path $gaFilePath){
$data = Import-Csv -Path $gaFilePath
# Load the Excel COM object
$excel = New-Object -ComObject Excel.Application
# Make Excel visible
$excel.Visible = $true
# Add a new workbook
$workbook = $excel.Workbooks.Add()
# Get the first worksheet
$worksheet = $workbook.Worksheets.Item(1)
# Set the header names and format
$worksheet.Cells.Item(1,1) = "DisplayName"
$worksheet.Cells.Item(1,1).Font.Bold = $true
$worksheet.Cells.Item(1,1).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,2) = "Mail"
$worksheet.Cells.Item(1,2).Font.Bold = $true
$worksheet.Cells.Item(1,2).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,3) = "OtherMails"
$worksheet.Cells.Item(1,3).Font.Bold = $true
$worksheet.Cells.Item(1,3).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,4) = "Proxy Addresses"
$worksheet.Cells.Item(1,4).Font.Bold = $true
$worksheet.Cells.Item(1,4).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,5) = "TelephoneNumber"
$worksheet.Cells.Item(1,5).Font.Bold = $true
$worksheet.Cells.Item(1,5).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,6) = "UserPrincipalName"
$worksheet.Cells.Item(1,6).Font.Bold = $true
$worksheet.Cells.Item(1,6).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,7) = "ObjectId"
$worksheet.Cells.Item(1,7).Font.Bold = $true
$worksheet.Cells.Item(1,7).Font.ColorIndex = 2 # white
$worksheet.Cells.Item(1,8) = "AccountEnabled"
$worksheet.Cells.Item(1,8).Font.Bold = $true
$worksheet.Cells.Item(1,8).Font.ColorIndex = 2 # white
# set the background color of the header row
$headerRange = $worksheet.Range("A1:H1")
$headerRange.Interior.ColorIndex = 30
# Set the data starting row
$row = 2
# Loop through data and populate the Excel worksheet
foreach ($admin in $data) {
$worksheet.Cells.Item($row,1) = $admin.DisplayName
$worksheet.Cells.Item($row,2) = $admin.Mail
$worksheet.Cells.Item($row,3) = $admin.OtherMails
$worksheet.Cells.Item($row,4) = $admin.ProxyAddresses
$worksheet.Cells.Item($row,5) = $admin.TelephoneNumber
$worksheet.Cells.Item($row,6) = $admin.UserPrincipalName
$worksheet.Cells.Item($row,7) = $admin.ObjectId
$worksheet.Cells.Item($row,8) = $admin.AccountEnabled
# Move to the next row
$row += 1
}
# set the background color of the rows
$range = $worksheet.Range("A2:H$row")
$fill = $range.Interior
$fill.Pattern = 1
$fill.PatternColorIndex = -4105
$fill.ThemeColor = 1
$fill.TintAndShade = 0.599993896298105
# set the background color of every row to a different color
for ($i = 2; $i -le $row; $i++) {
$rangeA = $worksheet.Range("A$i")
$rangeB = $worksheet.Range("B$i")
$rangeC = $worksheet.Range("C$i")
$rangeD = $worksheet.Range("D$i")
$rangeE = $worksheet.Range("E$i")
$rangeF = $worksheet.Range("F$i")
$rangeG = $worksheet.Range("G$i")
$rangeH = $worksheet.Range("H$i")
if (($i % 2) -eq 0) {
$rangeA.Interior.ColorIndex = 15
$rangeB.Interior.ColorIndex = 15
$rangeC.Interior.ColorIndex = 15
$rangeD.Interior.ColorIndex = 15
$rangeE.Interior.ColorIndex = 15
$rangeF.Interior.ColorIndex = 15
$rangeG.Interior.ColorIndex = 15
$rangeH.Interior.ColorIndex = 15
}
else {
$rangeA.Interior.ColorIndex = 2
$rangeB.Interior.ColorIndex = 2
$rangeC.Interior.ColorIndex = 2
$rangeD.Interior.ColorIndex = 2
$rangeE.Interior.ColorIndex = 2
$rangeF.Interior.ColorIndex = 2
$rangeG.Interior.ColorIndex = 2
$rangeH.Interior.ColorIndex = 2
}
}
# autofit the columns
$range = $worksheet.Range("A:H")
$range.EntireColumn.AutoFit() | Out-Null
# Save the workbook
$workbook.SaveAs("C:\Users\$([Environment]::UserName)\Desktop\AzFiles\GlobalAdmins.xlsx")
# Close the workbook and Excel application
$workbook.Close($true)
$excel.Quit()
# release the COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
}
else{
}
}
Export-GlobalAdminsToExcel
function Convert-LegacyProtocolsCsvToExcel {
param(
[Parameter(Mandatory = $true)]
[string]$CsvFilePath,
[Parameter(Mandatory = $true)]
[string]$OutputExcelFilePath
)
$csvData = Import-Csv -Path $CsvFilePath
# Load the Excel COM object
$excel = New-Object -ComObject Excel.Application
# Make Excel visible
$excel.Visible = $true
# Add a new workbook
$workbook = $excel.Workbooks.Add()
# Get the first worksheet
$worksheet = $workbook.Worksheets.Item(1)
# Set the header names and format
$headers = @("PrimarySmtpAddress", "ActiveSyncEnabled", "EwsEnabled", "PopEnabled", "ImapEnabled")
for ($i = 1; $i -le $headers.Length; $i++) {
$worksheet.Cells.Item(1, $i) = $headers[$i - 1]
$worksheet.Cells.Item(1, $i).Font.Bold = $true
$worksheet.Cells.Item(1, $i).Font.ColorIndex = 2 # white
}
# Set the background color of the header row
$headerRange = $worksheet.Range("A1:E1")
$headerRange.Interior.ColorIndex = 30
# Set the data starting row
$row = 2
# Loop through data and populate the Excel worksheet
foreach ($item in $csvData) {
for ($col = 1; $col -le $headers.Length; $col++) {
$worksheet.Cells.Item($row, $col) = $item.($headers[$col - 1])
}
$row += 1
}
# Autofit the columns
$range = $worksheet.Range("A:E")
$range.EntireColumn.AutoFit() | Out-Null
# Save the workbook
$workbook.SaveAs($OutputExcelFilePath)
# Close the workbook and Excel application
$workbook.Close($true)
$excel.Quit()
# Release the COM objects
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
}
# Call the function to convert the CSV to Excel
Convert-LegacyProtocolsCsvToExcel -CsvFilePath "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\legacyprotocols.csv" -OutputExcelFilePath "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\legacyprotocols.xlsx"
function Export-MFAcsvToExcel {
$data = Import-Csv -Path "C:\Users\$([Environment]::UserName)\Desktop\AzFiles\MFAEnabledUsers.csv"
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)
# Set header values and formatting
$worksheet.Cells.Item(1, 1) = "UserPrincipalName"
$worksheet.Cells.Item(1, 1).Font.Bold = $true
$worksheet.Cells.Item(1, 1).Font.ColorIndex = 2
$worksheet.Cells.Item(1, 2) = "Display Name"
$worksheet.Cells.Item(1, 2).Font.Bold = $true
$worksheet.Cells.Item(1, 2).Font.ColorIndex = 2
$worksheet.Cells.Item(1, 3) = "MFA Status"
$worksheet.Cells.Item(1, 3).Font.Bold = $true
$worksheet.Cells.Item(1, 3).Font.ColorIndex = 2
$worksheet.Cells.Item(1, 4) = "Disabled"
$worksheet.Cells.Item(1, 4).Font.Bold = $true
$worksheet.Cells.Item(1, 4).Font.ColorIndex = 2
$worksheet.Cells.Item(1, 5) = "Enabled"
$worksheet.Cells.Item(1, 5).Font.Bold = $true
$worksheet.Cells.Item(1, 5).Font.ColorIndex = 2
$headerRange = $worksheet.Range("A1:E1")
$headerRange.Interior.ColorIndex = 30
$row = 2
foreach ($expression in $data) {
$worksheet.Cells.Item($row, 1) = $expression.UserPrincipalName
$worksheet.Cells.Item($row, 2) = $expression.DisplayName
$worksheet.Cells.Item($row, 3) = $expression.'MFA Status'
$worksheet.Cells.Item($row, 4) = $expression.Disabled
$worksheet.Cells.Item($row, 5) = $expression.Enabled
$rangeA = $worksheet.Range("A$row")
$rangeB = $worksheet.Range("B$row")
$rangeC = $worksheet.Range("C$row")
$rangeD = $worksheet.Range("D$row")
$rangeE = $worksheet.Range("E$row")
if (($row % 2) -eq 0) {
$rangeA.Interior.ColorIndex = 15
$rangeB.Interior.ColorIndex = 15
$rangeC.Interior.ColorIndex = 15
$rangeD.Interior.ColorIndex = 15
$rangeE.Interior.ColorIndex = 15
} else {
$rangeA.Interior.ColorIndex = 2
$rangeB.Interior.ColorIndex = 2
$rangeC.Interior.ColorIndex = 2
$rangeD.Interior.ColorIndex = 2
$rangeE.Interior.ColorIndex = 2
}
$row += 1
}
# Autofit the columns
$range = $worksheet.Range("A:E")
$range.EntireColumn.AutoFit() | Out-Null
$workbook.SaveAs("C:\Users\$([Environment]::UserName)\Desktop\AzFiles\MFAEnabledUsers.xlsx")
$workbook.Close($true)
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
}
Export-MFAcsvToExcel
function Merge-ExcelFilesAndCleanup {
Write-Host "Merging Files" -ForegroundColor Cyan
$sourceFilesPath = "C:\Users\$([Environment]::UserName)\Desktop\AzFiles"
$mergedFilePath = Join-Path -Path $sourceFilesPath -ChildPath "AzureGraphData.xlsx"
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$mergedWorkbook = $excel.Workbooks.Add()
$csvFiles = Get-ChildItem -Path $sourceFilesPath -Filter "*.csv"
$xlsxFiles = Get-ChildItem -Path $sourceFilesPath -Filter "*.xlsx" | Where-Object { $_.Name -ne "AzureData.xlsx" -and $_.Name -ne "AzureGraphData.xlsx" }
$sheetIndex = 1
foreach ($file in $xlsxFiles) {
$sourceWorkbook = $excel.Workbooks.Open($file.FullName)
$sourceWorksheet = $sourceWorkbook.Worksheets.Item(1)
$sourceWorksheet.UsedRange.Copy()
if ($sheetIndex -eq 1) {
$targetWorksheet = $mergedWorkbook.Worksheets.Item(1)
} else {
$targetWorksheet = $mergedWorkbook.Worksheets.Add()
}
$targetWorksheet.Paste()
$targetWorksheet.Name = $file.BaseName
$targetWorksheet.UsedRange.Columns.AutoFit() # Add this line to autofit the columns
$sourceWorkbook.Close($false)
$sheetIndex++
}
$mergedWorkbook.SaveAs($mergedFilePath)
$mergedWorkbook.Close($true)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($mergedWorkbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
# Delete CSV and individual XLSX files
foreach ($file in $csvFiles + $xlsxFiles) {
Remove-Item -Path $file.FullName -Force
}
}
# Call the function at the end of the script
Merge-ExcelFilesAndCleanup
}
AzureGraphDump