Manage-AzureRMBlueprint.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
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
<#PSScriptInfo
 
.VERSION 2.3
 
.GUID 0fc48522-2362-4cc0-b46d-e1d88d87b4e2
 
.AUTHOR jbritt@microsoft.com
 
.COMPANYNAME Microsoft
 
.COPYRIGHT Microsoft
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
   https://aka.ms/ManageARMBlueprints/Video
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
   May 23, 2019 - version 2.3
   * Added BlueprintTarget to the parameters to allow for supporting subscription for Blueprint Location on
     import and export. Thank you https://github.com/mrptsai (Paul Towler) for your inputs to add this option!
   * Added new API version https://github.com/JimGBritt/AzureBlueprint/commit/c3304315745676912e7a958e40fa91b5da0d005e (thank you https://github.com/drmiru Michael Rueefli!)
   * Added enhanced error output https://github.com/JimGBritt/AzureBlueprint/commit/94658c99a7b8207937492bb5a71285a094b6c11e#diff-e7e495ba10b61589a1c555132692367f (thank you https://github.com/Oechiih Jan Oehen!)
   * Updated -ModuleMode to be Az by default (time to move on :)).
   * Added option to bypass subscription requirement and provide TenantID (https://github.com/JimGBritt/AzureBlueprint/issues/2)
     Thank you https://github.com/GPugnet (Guillaume Pugnet) your your feedback on bypassing the SubId requirement
#>



<#
.SYNOPSIS
  Import, Export, or Report for an Azure Blueprint and related artifacts into or out of an Azure AD Management Group.
   
.DESCRIPTION
  To learn how to use this script, please watch this video: https://aka.ms/ManageARMBlueprints/Video
 
  This script takes a SubscriptionID, ManagementGroupID, BlueprintName, Mode switch, and an optional
  NewBluePrintName as a parameter. This script is meant to provide the ability to export an Azure
  ARM Blueprint for backup or import into an other Management Group. You can also report on what artifacts are configured
  for a specific blueprint using the report mode.
 
  Use of "-Force" provides the ability to launch this script without prompting, if all required parameters are provided.
 
  NOTE: This version currently only supports exporting a latest full published version or current draft of a blueprint and
  related artifacts.
 
  ADDITIONAL NOTE: This script currently also does not export custom policies.
 
.PARAMETER ModuleMode
    The module (AzureRM or Az) used to authenticate to Azure to be leveraged with the script execution
 
.PARAMETER SubscriptionId
    The subscriptionID of the Azure Subscription that is within the Azure AD tenant with your Blueprint or where
    you will be targeting for import of your Blueprint
 
.PARAMETER ManagementGroupID
    Use this to reference a Management Group to export a named Blueprint and artifacts
 
.PARAMETER NewBluePrintName
    Use this to update the Blueprint name on a selected Blueprint to a new name on import / export.
 
.PARAMETER BlueprintName
    Use this to bypass searching for a Blueprint during the script on export
 
.PARAMETER Mode
    Indicates mode of operation (Import/Export/Report) for a Blueprint
 
.PARAMETER ExportDir
    This is the base folder for exporting the Azure Blueprint data. Example "Exports" or .\Exports or "c:\exports"
 
.PARAMETER ImportDir
    This is the base folder for importing a Blueprint and artifacts into an Azure AD Management Group. The folder would
    look something like ".\Exports\MG-Root\MyBlueprint" or "c:\exports\MG-Root\MyBlueprint"
  
 .PARAMETER Force
    Use Force to run silently [providing all parameters needed for silent mode]
    see get-help <scriptfile> -examples
 
 .PARAMETER ReportDir
    Use ReportDir in conjunction with report to export report results to a report directory
 
 .PARAMETER TenantId
    Use the -TenantId parameter with a proper guid formatted ID to bypass the subscriptionID requirement
    Note: Cannot use in conjunction with -SubscriptionID
 
 .PARAMETER BlueprintMode
    Use the -BlueprintMode parameter to specify you want to import/export/report from either a subscriptionID or ManagementGroupID
    Note: ManagementGroup is the default mode
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -ModuleMode Az -Mode import -ImportDir .\BPExports\MG-root\MyBlueprint
  Uses Az Module to authenticate to Azure and Imports a blueprint from the relative path.
  Will prompt for Azure Subscription to set context on AD Tenant.
  See: https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-1.0.0 to migrate to Az Module
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -ModuleMode AzureRM -Mode import -ImportDir .\BPExports\MG-root\MyBlueprint
  Uses AzureRM Module to authenticate to Azure and Imports a blueprint from the relative path.
  Will prompt for Azure Subscription to set context on AD Tenant.
  Note: Az is currently the default
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -Mode import -ImportDir .\BPExports\MG-root\MyBlueprint -TenantId "<guid>"
  Imports a blueprint from the relative path. Will prompt for Management Group to import into
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -Mode import -ImportDir .\BPExports\MG-root\MyBlueprint -BlueprintMode Subscription
  Imports a blueprint from the relative path. Will prompt for Azure Subscription to import blueprint into
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -Mode export -exportDir .\BPExports\ -BlueprintMode Subscription
  Exports a blueprint to the relative path. Will prompt for Azure Subscription to export blueprint from
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -Mode import -ImportDir .\BPExports\MG-root\MyBlueprint
  Imports a blueprint from the relative path. Will prompt for Azure Subscription to set context on AD Tenant
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -mode export -ManagementGroupID "<ManagementGroup where Blueprint is located>" -BlueprintName "<MyBlueprint>" -ExportDir "<Target Folder Name>"
  Take in parameters and exports a named blueprint and related artifacts to a sub directory named after the MG
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -mode export -ManagementGroupID "<ManagementGroup where Blueprint is located>" -BlueprintName "<MyBlueprint>" -ExportDir "Blueprints"
  Take in parameters and exports a named blueprint and related artifacts to a sub directory named after the new MG name
  This example allows you to export the named Managment Group in the blueprint and artifacts to a new one allowing you
  to import into another Azure AD tenant with a different naming / management group structure.
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -mode import -ImportDir ".\exports\MG-Root\MyBlueprint" -ManagementGroupID "<Target ManagementGroup for Blueprint>" -NewBlueprintName "<New Blueprint Name>"
  This will import a blueprint and artifacts from a source directory and targets a management group and new blueprint name on import
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -Mode Import -ImportDir ".\exports\MG-Root\MyBlueprint" -SubscriptionId "e69041bc-8e27-4272-9089-60ac8f508937" -force
  This will import a blueprint and artifacts from a source directory without prompting.
 
 .EXAMPLE
  .\Manage-AzureRMBlueprint.ps1 -mode report -ManagementGroupID "<ManagementGroup where Blueprint is located>" -BlueprintName "<MyBlueprint>" -ReportDir "<Target Folder Name>" -SubscriptionID "<a SubscriptionID within the tenant you want to report from>"
  Take in parameters and exports a named blueprint and related artifacts to a sub directory named after the MG
 
.NOTES
   AUTHOR: Jim Britt Senior Program Manager - Azure CAT
   LAST EDIT: May 23, 2019 - version 2.3
   * Added BlueprintTarget to the parameters to allow for supporting subscription for Blueprint Location on
     import and export. Thank you https://github.com/mrptsai (Paul Towler) for your inputs to add this option!
   * Added new API version https://github.com/JimGBritt/AzureBlueprint/commit/c3304315745676912e7a958e40fa91b5da0d005e (thank you https://github.com/drmiru Michael Rueefli!)
   * Added enhanced error output https://github.com/JimGBritt/AzureBlueprint/commit/94658c99a7b8207937492bb5a71285a094b6c11e#diff-e7e495ba10b61589a1c555132692367f (thank you https://github.com/Oechiih Jan Oehen!)
   * Updated -ModuleMode to be Az by default (time to move on :)).
   * Added option to bypass subscription requirement and provide TenantID (https://github.com/JimGBritt/AzureBlueprint/issues/2)
     Thank you https://github.com/GPugnet (Guillaume Pugnet) your your feedback on bypassing the SubId requirement
    
   January 04, 2019 - version 2.2
   * Added ModuleMode to the parameters to allow for supporting AzureRM and Az module sets
     Thank you Florent APPOINTAIRE (@florent_app) for the feedback and additional inputs for supporting Az modules
   * Added validation that all blueprint and artifact names are less than or equal to the maximum of 48 characters
     Thank you for your great inputs on these updates Guillaume Pugnet (@PugnetGuillaume)!
     https://github.com/JimGBritt/AzureBlueprint/issues/1
    
   November 30, 2018 - version 2.1
   * Updated REST Token code
   * Added exit 1 to terminating errors
   * Thank you for your great inputs on these updates Guillaume Pugnet (@PugnetGuillaume)!
    
   November 20, 2018 - version 2.0
   * Added function for standard error
   * Added function for building REST PUT payload
   * Updated error to indicate clear-AzureRMContext (to replace Logout-AzureRMAccount) to resolve 401
     Thanks https://twitter.com/JFE_CH (Jonas Feller) for the recommendation at this site:
     https://www.jfe.cloud/export-import-azure-blueprints/
   * Removed "ID" and "Name" fields from the export
   * Building "Name" and "ID" for imports dynamically based on folder and file name for blueprint and artifacts
   * Added proper order handling for import of blueprint first, then all artifacts
   * Added APIVersion variable
   * Thanks Alexander Frankel[MSFT] for your thoughts and feedback here across this release!
       
   November 13, 2018 ver 1.42
   * Added try/catch logic on json conversion to catch improper json files
   * Fixed an example in my get-help output
   * Thank you Jorge Cotillo (MSFT) AzureCAT for your inputs on improved logic for json validation!
    
   October 31, 2018 ver 1.41
   * Added more debug information to help in troubleshooting issues
   * Removed NewManagementGroupID and required ManagementGroupID as a parameter for import/report/export
   * Thank you Javier Soriano (MSFT) for the feedback and recommendations for a cleaner import experience!
   * Thank you Tao Yang (MVP) for your input around additional debug options
   * And special thanks to Aleksandar Nikolic (MVP) for your initial review and great feedback!
 
   October 24, 2018
   * Renamed ManagementGroup parameter to ManagementGroupID to make it clearer
   * Added ReportDir parameter to target a report directory
   * No longer navigating to script directory during execution
   * Updated Parameters / Sets in general - clean up
 
.LINK
    This script posted to and discussed at the following locations:
    https://aka.ms/ManageARMBlueprints/
    https://aka.ms/ManageARMBlueprints/Video
    https://github.com/JimGBritt/AzureBlueprint/
#>


<#
REST API Documentation here: https://docs.microsoft.com/en-us/azure/governance/blueprints/create-blueprint-rest-api
 
Blueprints are available via the following rest endpoint within your Azure AD tenant.
https://management.azure.com/providers/Microsoft.Management/managementGroups/<MG-NAME>/providers/Microsoft.Blueprint/blueprints/<BLUEPRINT-NAME>?api-version=2017-11-11-preview
 
And to get the artifacts the following REST API endpoint is available:
https://management.azure.com/providers/Microsoft.Management/managementGroups/<MG-NAME>/providers/Microsoft.Blueprint/blueprints/<BLUEPRINT-NAME>/artifacts?api-version=2017-11-11-preview
#>

[cmdletbinding(
        DefaultParameterSetName='Default'
    )]

param
(
    # Mode (Export/Import/Report)
    [Parameter(ParameterSetName='Default',Mandatory = $True)]
    [Parameter(ParameterSetName='Import')]
    [Parameter(ParameterSetName='Export')]
    [Parameter(ParameterSetName='Report')]
    [ValidateSet("Export","Import","Report")]
    [String]$Mode,
    
    # Module Mode (Az or AzureRM)
    [Parameter(Mandatory = $False)]
    [ValidateSet("Az","AzureRM")]
    [String]$ModuleMode="Az",

    # The Management Group ID (***not the friendly name***)
    [Parameter(ParameterSetName='Import')]
    [Parameter(ParameterSetName='Export')]
    [Parameter(ParameterSetName='Report')]
    [string]$ManagementGroupID,

    # Use ReportDir to export a report of the selected Blueprint and related artifacts
    # Used with the report mode
    [Parameter(ParameterSetName='Report')]
    [string]$ReportDir,

    # The Blueprint Name
    [Parameter(ParameterSetName='Export')]
    [Parameter(ParameterSetName='Report')]
    [string]$BlueprintName,

    # Provide SubscriptionID to bypass subscription listing
    [Parameter(ParameterSetName='force')]
    [Parameter(ParameterSetName='Export')]
    [Parameter(ParameterSetName='Import')]
    [Parameter(ParameterSetName='Report')]
    [guid]$SubscriptionId,

    # New Blueprint Name
    [Parameter(ParameterSetName='Export')]
    [Parameter(ParameterSetName='Import')]
    [string]$NewBlueprintName,        

    <#
    # Draft or Published **** future use ****
    [Parameter(Mandatory=$False,ParameterSetName='Export')]
    [ValidateSet("Draft","Published")]
    [string]$State,
 
    # Published Blueprint Version **** future use ****
    [Parameter(Mandatory=$False,ParameterSetName='Export')]
    [string]$Version,
    #>


    # Base folder for export
    [Parameter(ParameterSetName='Export')]
    [string]$ExportDir,

    # Base folder for import
    [Parameter(ParameterSetName='Import')]
    [Parameter(ParameterSetName='force')]
    [string]$ImportDir,

    # Blueprint Mode (Subscription or ManagementGroup)
    [Parameter(ParameterSetName='Export')]
    [Parameter(ParameterSetName='Import')]
    [Parameter(ParameterSetName='Report')]
    [ValidateSet("ManagementGroup","Subscription")]
    [string]$BlueprintMode="ManagementGroup",

    # Use Force to run in silent mode (requires certain parameters to be provided)
    [Parameter(ParameterSetName='Import')]
    [Parameter(ParameterSetName='force')]
    [switch]$Force,

    # TenantId to bypass subscription requirement
    [guid]$TenantId

)

# Function used to build numbers in selection tables for menus
function Add-IndexNumberToArray (
    [Parameter(Mandatory=$True)]
    [array]$array
    )
{
    for($i=0; $i -lt $array.Count; $i++) 
    { 
        Add-Member -InputObject $array[$i] -Name "#" -Value ($i+1) -MemberType NoteProperty 
    }
    $array
}

function StandardError
{
    param ($Exception)
    write-host "An error occurred - please check rights or parameters for proper configuration and try again"
    write-host "If you received " -NoNewline 
    write-host "The access token is invalid " -NoNewline -ForegroundColor Red
    write-host "or an error " -NoNewline
    write-host "(401)" -NoNewline -ForegroundColor Red 
    write-host ", then please type"
    write-host "Clear-AzureRmContext " -ForegroundColor Yellow
    write-host "from within your PowerShell prompt and try running the script again"
    write-host "Error 401 could indicate cached authentication tokens have expired"
    write-host "Error 403 could indicate target Management Group does not exist"
    write-host "Error 404 could indicate source blueprint not found"
    write-host "======================================================================="
    write-host "Specific Error is: " -NoNewline
    write-host "$Exception" -ForegroundColor Yellow

}
function build-PutContent
{
    param
    (
        $URI,
        $BodyContent    
    )
    $PutContent = @{
        URI = $URI
        Headers = @{
            Authorization = "Bearer $($token.AccessToken)"
            'Content-Type' = 'application/json'
        }
        Method = 'Put'
        UseBasicParsing = $true
        Body = $BodyContent
    }
    return $PutContent
}

# MAIN SCRIPT
Write-Host "Using " -NoNewline 
write-host "$ModuleMode " -NoNewline -ForegroundColor Yellow
write-host "module mode"

# Limitations to the script
write-host "Please note this script is using a preview API for Azure Blueprint and is subject to change." -ForegroundColor Green
write-host "This script currently only supports Draft Blueprints or most recently published and related artifacts." -ForegroundColor DarkYellow
write-host "This script currently does not support custom policies - only built-ins are supported." -ForegroundColor DarkYellow

# Determine where the script is running - build export dir
if ($MyInvocation.MyCommand.Path -ne $null)
{
    $CurrentDir = Split-Path $MyInvocation.MyCommand.Path
}
else
{
    # Sometimes $myinvocation is null, it depends on the PS console host
    $CurrentDir = "."
}
$APIVersion = "?api-version=2018-11-01-preview"
#cd $CurrentDir

# Determine what we are doing - export/import/report
if($Mode -eq "Export" -and !$ExportDir)
{
    Write-Host "Please " -NoNewline
    write-host "provide a directory " -NoNewline -ForegroundColor Yellow
    Write-Host "to EXPORT using the `$ExportDir parameter for your blueprint and artifacts"
    exit 1
}

if($Mode -eq "Import" -AND !$ImportDir)
{
    Write-Host "Please " -NoNewline
    write-host "provide a directory " -NoNewline -ForegroundColor Yellow
    write-host "to IMPORT using the `$ImportDir parameter for your blueprint and artifacts" 
    exit 1
}
If($Mode -eq "Report" -AND $ReportDir)
{
    IF(!$(Test-Path -Path "$ReportDir"))
    {
        write-host "Directory " -NoNewline
        Write-Host "$ReportDir " -ForegroundColor Yellow -NoNewline
        Write-Host "does not exist - please create and retry the operation"
        exit 1        
    }
}

# Login to Azure - if already logged in, use existing credentials.
Write-Host "Authenticating to Azure..." -ForegroundColor Cyan
try
{
    If($ModuleMode -eq "AzureRM"){$AzureLogin = Get-AzureRMSubscription}
    If($ModuleMode -eq "Az"){$AzureLogin = Get-AzSubscription}
}
catch
{
    If($ModuleMode -eq "AzureRM")
    {
        $null = Login-AzureRmAccount
        $AzureLogin = Get-AzureRmSubscription
    }
    If($ModuleMode -eq "Az")
    {
        $null = Login-AzAccount
        $AzureLogin = Get-AzSubscription
    }
}

# Authenticate to Azure if not already authenticated
# Ensure this is the subscription where your Management Groups are that house Blueprints for import/export operations
If($AzureLogin -and !($SubscriptionID) -and !($TenantId))
{
    If($ModuleMode -eq "AzureRM"){[array]$SubscriptionArray = Add-IndexNumberToArray (Get-AzureRmSubscription)}
    If($ModuleMode -eq "Az"){[array]$SubscriptionArray = Add-IndexNumberToArray (Get-AzSubscription)}
    [int]$SelectedSub = 0

    # use the current subscription if there is only one subscription available
    if ($SubscriptionArray.Count -eq 1) 
    {
        $SelectedSub = 1
    }
    # Get SubscriptionID if one isn't provided
    while($SelectedSub -gt $SubscriptionArray.Count -or $SelectedSub -lt 1)
    {
        Write-host "Please select a subscription from the list below for the " -NoNewline
        write-host $Mode -ForegroundColor Yellow -NoNewline
        write-host " Operation"
        $SubscriptionArray | select "#", Name, ID | ft
        try
        {
            $SelectedSub = Read-Host "Please enter a selection from 1 to $($SubscriptionArray.count) for the $Mode Operation"
        }
        catch
        {
            Write-Warning -Message 'Invalid option, please try again.'
        }
    }
    if($($SubscriptionArray[$SelectedSub - 1].Name))
    {
        $SubscriptionName = $($SubscriptionArray[$SelectedSub - 1].Name)
    }
    elseif($($SubscriptionArray[$SelectedSub - 1].SubscriptionName))
    {
        $SubscriptionName = $($SubscriptionArray[$SelectedSub - 1].SubscriptionName)
    }
    write-verbose "You Selected Azure Subscription: $SubscriptionName"
    
    if($($SubscriptionArray[$SelectedSub - 1].SubscriptionID))
    {
        [guid]$SubscriptionID = $($SubscriptionArray[$SelectedSub - 1].SubscriptionID)
    }
    if($($SubscriptionArray[$SelectedSub - 1].ID))
    {
        [guid]$SubscriptionID = $($SubscriptionArray[$SelectedSub - 1].ID)
    }
}
if($SubscriptionId -and !($TenantId))
{
    Write-Host "Selecting Azure Subscription: $($SubscriptionID.Guid) ..." -ForegroundColor Cyan
    If($ModuleMode -eq "AzureRM"){$Null = Select-AzureRmSubscription -SubscriptionId $SubscriptionID.Guid}
    If($ModuleMode -eq "Az"){$Null = Select-AzSubscription -SubscriptionId $SubscriptionID.Guid}
}
If(!($ManagementGroupID) -and $BlueprintMode -eq "ManagementGroup")
{
    If($ModuleMode -eq "AzureRM"){[array]$MgtGroupArray = Add-IndexNumberToArray (Get-AzureRmManagementGroup)}
    If($ModuleMode -eq "Az"){[array]$MgtGroupArray = Add-IndexNumberToArray (Get-AzManagementGroup)}
    if(!$MgtGroupArray)
    {
        Write-host "Please make sure you have Management Groups that are accessible"
        exit 1
    }
    [int]$SelectedMG = 0

    # use the current Managment Group if there is only one MG available
    if ($MgtGroupArray.Count -eq 1) 
    {
        $SelectedMG = 1
    }
    # Get Management Group if one isn't provided
    while($SelectedMG -gt $MgtGroupArray.Count -or $SelectedMG -lt 1)
    {
        Write-host "Please select a Management Group from the list below"
        $MgtGroupArray | select "#", Name, DisplayName, Id | ft
        try
        {
            write-host "If you don't see your ManagementGroupID try using the parameter -ManagementGroupID" -ForegroundColor Cyan
            $SelectedMG = Read-Host "Please enter a selection from 1 to $($MgtGroupArray.count)"
        }
        catch
        {
            Write-Warning -Message 'Invalid option, please try again.'
        }
    }
    if($($MgtGroupArray[$SelectedMG - 1].Name))
    {
        $ManagementGroupID = $($MgtGroupArray[$SelectedMG - 1].Name)
    }
    
    write-verbose "You Selected Management Group: $ManagementGroupID"
    Write-Host "Selecting Management Group: $ManagementGroupID ..." -ForegroundColor Cyan
}

# Set context for REST Auth Token
If($ModuleMode -eq "AzureRM"){$currentContext = Get-AzureRmContext}
If($ModuleMode -eq "Az"){$currentContext = Get-AzContext}

# Get token from current context to auth
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
if($TenantId)
{
    $token = $profileClient.AcquireAccessToken($currentContext.Tenant.TenantId)
}
else 
{
    $token = $profileClient.AcquireAccessToken($currentContext.Subscription.TenantId)
}

# If export or report mode is used
If($Mode -eq "Export" -or $Mode -eq "Report")
{
    # REST Header for REST call to get Blueprints and Artifacts
    $GetBlueprint = @{
        Headers = @{
            Authorization = "Bearer $($token.AccessToken)"
            'Content-Type' = 'application/json'
        }
        Method = 'Get'
        UseBasicParsing = $true
    }
    # Let's go get all blueprints available within a selected management group
    If(!$BlueprintName)
    {
        # Get all Blueprints
        if ($BlueprintMode -eq "Subscription" -and !($TenantId))
        {
            $BlueprintsURI = "https://management.azure.com/subscriptions/$($SubscriptionID)/providers/Microsoft.Blueprint/blueprints$($APIVersion)"    
        }
        else 
        {
            $BlueprintsURI = "https://management.azure.com/providers/Microsoft.Management/managementGroups/$ManagementGroupID/providers/Microsoft.Blueprint/blueprints$($APIVersion)"    
        }
        
        Try
        {
            $BPs = Invoke-WebRequest -uri $BlueprintsURI @GetBlueprint
        }
        catch
        {
            StandardError -Exception $($_.Exception.Message)
            exit 1
        }
        $BPValues = $($BPs|convertfrom-json).value
        if(!$BPValues)
        {
            write-host "No Blueprints found in $ManagementGroupID"
            exit 1
        }
        [array]$BPsArray = Add-IndexNumberToArray $BPValues
        [int]$SelectedBP = 0

        # use the only BP if there is only one BP available
        if ($BPsArray.Count -eq 1) 
        {
            $SelectedBP = 1
        }
        # Get Blueprint if one isn't provided
        while($SelectedBP -gt $BPsArray.Count -or $SelectedBP -lt 1)
        {
            Write-host "Please select a Blueprint from the list below to export"
            if ($BlueprintMode -eq "Subscription" -and !($TenantId))
            {
                $BPsArray | Select-Object "#", @{Label = "Blueprint Name";Expression={$_.name}}, @{Label = "Subscription"; Expression = {$($BPsArray.ID).split("/")[2]}}, @{Label = "Blueprint Description";Expression={$_.properties.description}} | Format-Table
            }
            else
            {
                $BPsArray | Select-Object "#", @{Label = "Blueprint Name";Expression={$_.name}}, @{Label = "ManagementGroup"; Expression = {$($BPsArray.ID).split("/")[4]}}, @{Label = "Blueprint Description";Expression={$_.properties.description}} | Format-Table
            }
            try
            {
                $SelectedBP = Read-Host "Please enter a selection from 1 to $($BPsArray.count)"
            }
            catch
            {
                Write-Warning -Message 'Invalid option, please try again.'
            }
        }
        if($($BPsArray[$SelectedBP - 1].Name))
        {
            $BlueprintName = $($BPsArray[$SelectedBP - 1].Name)
        }
        write-verbose "You Selected Blueprint: $BlueprintName"
    }
    Write-Host "Selecting Blueprint: $BlueprintName ..." -ForegroundColor Cyan

    <#
    # FUTURE USE
    # Get all possible published versions of a selected Blueprint (if they exist) to choose from
    $BluePrintVersionsURI = "https://management.azure.com/providers/Microsoft.Management/managementGroups/$ManagementGroupID/providers/Microsoft.Blueprint/blueprints/$BluePrintName/versions" + "?api-version=2017-11-11-preview"
    $BPVersions = Invoke-WebRequest -uri $BluePrintVersionsURI @GetBlueprint
    $BPVersions = $($BPVersions|convertfrom-json).content
    if($BPVersions)
    {
        [array]$BPVersionArray = Add-IndexNumberToArray ($BPVersions)
        [int]$SelectedBPVer = 0
 
        # If there is only one Blueprint version available - select it
        if ($BPVersionArray.Count -eq 1)
        {
            $SelectedBPVer = 1
        }
        # Get all blueprint versions
        while($SelectedBPVer -gt $BPVersionArray.Count -or $SelectedBPVer -lt 1)
        {
            Write-host "Please select a Blueprint Version from the list below to export"
            $BPVersionArray|select "#", @{Label = "Version";Expression={$_.name}}, @{Label = "Blueprint Name";Expression={$BlueprintName}}|ft
                 
            try
            {
                $SelectedBPVer = Read-Host "Please enter a selection from 1 to $($BPVersionArray.count)"
            }
            catch
            {
                Write-Warning -Message 'Invalid option, please try again.'
            }
        }
        if($($BPVersionArray[$SelectedBPVer - 1].Name))
        {
            $Version = $($BPVersionArray[$SelectedBPVer - 1].Name)
        }
     
        write-verbose "You Selected Blueprint Version: $Version"
        Write-Host "Selecting Blueprint: $Version for Blueprint $BlueprintName..." -ForegroundColor Cyan
        $BluePrintURI = "https://management.azure.com/providers/Microsoft.Management/managementGroups/$ManagementGroupID/providers/Microsoft.Blueprint/blueprints/$BlueprintName/versions/$Version" + "?api-version=2017-11-11-preview"
        $ArtifactsURI = "https://management.azure.com/providers/Microsoft.Management/managementGroups/$ManagementGroupID/providers/Microsoft.Blueprint/blueprints/$BlueprintName/versions/$Version/artifacts" + "?api-version=2017-11-11-preview"
    }
    Else
    {
        Write-host "No published versions present to export - defaulting to draft"#>

    #Blueprints and Artifacts URIs
    if ($BlueprintMode -eq "Subscription" -and !($TenantId))
    {
        $BluePrintURI = "https://management.azure.com/subscriptions/$($SubscriptionID)/providers/Microsoft.Blueprint/blueprints/$($BluePrintName)$($APIVersion)"
        $ArtifactsURI = "https://management.azure.com/subscriptions/$($SubscriptionID)/providers/Microsoft.Blueprint/blueprints/$($BluePrintName)/artifacts$($APIVersion)"
    } else
    {
        $BluePrintURI = "https://management.azure.com/providers/Microsoft.Management/managementGroups/$($ManagementGroupID)/providers/Microsoft.Blueprint/blueprints/$($BluePrintName)$($APIVersion)"
        $ArtifactsURI = "https://management.azure.com/providers/Microsoft.Management/managementGroups/$($ManagementGroupID)/providers/Microsoft.Blueprint/blueprints/$($BluePrintName)/artifacts$($APIVersion)"
    }
    #}
    try
    {
        $BP = Invoke-WebRequest -uri $BluePrintURI @GetBlueprint
    }
    catch
    {
        StandardError -Exception $($_.Exception.Message)
        exit 1
    }
    $BlueprintContent = $BP.content | ConvertFrom-Json

    If($NewBluePrintName)
    {
        $TargetBPName = $NewBluePrintName
    }
    Else
    {
        $TargetBPName = $BlueprintName
    }
    if($Mode -eq "Export")
    {
        # Create export directory if one doesn't exist
        IF(!$(Test-Path -Path "$ExportDir\$TargetBPName"))
        {
            $NewFolder = New-Item -Type Directory "$ExportDir\$TargetBPName"
        }
        # Exporting main Blueprint
        write-host "Export Folder for export: $ExportDir\$TargetBPName" 
        Write-Host "Exporting Blueprint: " -ForegroundColor Cyan -NoNewline
        write-host "$BlueprintName " -ForegroundColor Yellow -NoNewline
        write-host "to target Blueprint Name $TargetBPName" -ForegroundColor White
        # Remove ID to generalize JSON
        $BlueprintContent = $BlueprintContent | Select-Object -Property * -ExcludeProperty id

        # Remove Name to generalize JSON
        $BlueprintContent = $BlueprintContent | Select-Object -Property * -ExcludeProperty name

        $BlueprintContent|ConvertTo-Json -Depth 50|Out-File "$ExportDir\$TargetBPName\$TargetBPName.json"
    }
    # Build details for Blueprint basic report
    if($Mode -eq "Report")
    {
        $Report =@()
        $MyObj = New-Object System.Object
        Add-Member -InputObject $MyObj -Name "Type" -Value ("AzureBlueprint") -MemberType NoteProperty 
        Add-Member -InputObject $MyObj -Name "Display Name" -Value ($BlueprintName) -MemberType NoteProperty
        Add-Member -InputObject $MyObj -Name "ID" -value ($BlueprintContent.name)-MemberType NoteProperty
        $Report = $Report + $Myobj
    }
    # Get All Artifacts
    try
    {
        try
        {
            $BPArtifacts = Invoke-WebRequest -Uri $ArtifactsURI @GetBlueprint
        }
        catch
        {
            StandardError -Exception $($_.Exception.Message)
            exit 1
        }
        $Artifacts = $BPArtifacts.Content | ConvertFrom-Json
        
        # Logic for exporting artifacts from a selected Blueprint
        if($Mode -eq "Export")
        {
            Write-Host "Starting the export of Blueprint Artifacts" -ForegroundColor Cyan
            foreach($Artifact in $Artifacts.value)
            {

                # Exporting all artifacts by kind and name
                $Kind = $Artifact.kind
                $Name = $Artifact.Name

                # Remove ID to generalize json
                $Artifact = $Artifact | Select-Object -Property * -ExcludeProperty id
                # Removing name to generalize artifact
                $Artifact = $Artifact | Select-Object -Property * -ExcludeProperty name

                Write-Host "Exporting Artifact($Kind): " -NoNewline -ForegroundColor Cyan
                write-host "$Name.json" -ForegroundColor Yellow
                $Artifact|ConvertTo-Json -Depth 50|Out-File "$ExportDir\$TargetBPName\$Name.json"
            }
        }
        # Report logic for exporting a basic report of a Blueprint and Artifacts
        if($Mode -eq "Report")
        {
            
            foreach($Artifact in $Artifacts.value)
            {
                # Display Details
                $MyObj = New-Object System.Object
                Add-Member -InputObject $MyObj -Name "Type" -Value ($Artifact.kind) -MemberType NoteProperty 
                Add-Member -InputObject $MyObj -Name "Display Name" -Value ($Artifact.Properties.DisplayName) -MemberType NoteProperty 
                Add-Member -InputObject $MyObj -Name "ID" -Value ($Artifact.Name) -MemberType NoteProperty 
                $Report = $Report + $Myobj
            }
            IF(!$ReportDir)
            {
                Write-Host "No Report Directory parameter " -NoNewline
                write-host "(`$ReportDir) " -NoNewline -ForegroundColor Yellow
                write-host " provided. Writing to console!"
                $Report|ft
            }
            If($ReportDir)
            {
                $Time = $(Get-Date).ToString("yyyyMMddhhmm")
                Write-host "Writing Report to " -NoNewline
                write-host "$ReportDir\Report-$BlueprintName-$Time.csv"  -ForegroundColor Yellow
                $Report | Export-Csv "$ReportDir\Report-$BlueprintName-$Time.csv" -NoTypeInformation
            }
        } 
    }
    catch
    {}
    Write-host "Complete"
}
# Import logic for Azure Blueprints
If($Mode -eq "Import")
{
    # Array for JSONs processing
    $JSONArray =@()
    if($BlueprintMode -eq "Subscription" -and !($TenantId))
    {
        $TargetStr = "$SubscriptionId subscription"
    }
    else
    {
        $TargetStr = "$ManagementGroupID Management Group"        
    }
    
    # Validate customer wants to continue to import Blueprint and artifacts
    # If Force used, will update without prompting
    if ($Force -OR $PSCmdlet.ShouldContinue("This operation will attempt to import the Blueprint from $ImportDir into your $TargetStr. Continue?",$ImportDir) )
    {
        $filesToImport = Get-ChildItem $ImportDir\*.json -rec
        Write-Host "Starting the import of a Blueprint and Artifacts" -ForegroundColor Cyan
        Write-Host "Importing Blueprint from: " -ForegroundColor Cyan -NoNewline
        write-host "$ImportDir" -ForegroundColor Yellow 
        
        # Getting BlueprintName from base folder
        $BlueprintName = $filesToImport[0].directory.Name

        # Get each file
        foreach ($file in $filesToImport)
        {
            try
            {
                $FileContent = Get-Content -Path $File.pspath|ConvertFrom-Json -ErrorAction stop
            }
            catch
            {
                # Throw an error to screen and exit script on invalid json
                write-host "ERROR: " -NoNewline -ForegroundColor Red
                Write-Host "Check to ensure " -NoNewline
                write-host "$($File.Name) " -ForegroundColor Yellow -NoNewline
                write-host "is a valid JSON"
                exit 1
            }
            # Add ID to the PSObject to allow for importing to proper path
            If($FileContent.ID)
            {
                $FileContent.ID = $null
            }
            else
            {
                $FileContent | Add-Member -Name 'id' -Type NoteProperty -Value $Null
            }
            
            if($FileContent.type -eq "Microsoft.Blueprint/blueprints")
            {
                # Add Name to the PSObject to allow for importing to proper path
                If(!($FileContent.Name))
                {
                    $FileContent | Add-Member -Name 'Name' -Type NoteProperty -Value $File.Directory.Name
                }

                if($NewBluePrintName)
                {
                    # Only supported on draft (non versioned blueprint exports)
                    $FileContent.Name = $NewBluePrintName
                    $BlueprintName = $NewBluePrintName
                }
                #Ensure we update the Management Group / subscriptionId and BlueprintName for the target ID
                if ($BlueprintMode -eq "Subscription" -and !($TenantId))
                {
                    $FileContent.ID = "/subscriptions/$($SubscriptionID)/providers/Microsoft.Blueprint/blueprints/$($BluePrintName)"
                }
                else
                {
                    $FileContent.ID = "/providers/Microsoft.Management/managementGroups/$($ManagementGroupID)/providers/Microsoft.Blueprint/blueprints/$($BluePrintName)"    
                }
                
            }
            if($FileContent.type -eq "Microsoft.Blueprint/blueprints/artifacts") 
            {
                # Add Name to the PSObject to allow for importing to proper path
                If(!($FileContent.Name))
                {
                    $FileContent | Add-Member -Name 'Name' -Type NoteProperty -Value $File.BaseName
                }
                                
                if($NewBluePrintName)
                {
                    $BlueprintName = $NewBluePrintName
                }
                #Ensure we update the Management Group /subscriptionId and BlueprintName for the target ID
                if ($BlueprintMode -eq "Subscription" -and !($TenantId))
                {
                    $FileContent.ID = "/subscriptions/$($SubscriptionID)/providers/Microsoft.Blueprint/blueprints/$($BluePrintName)/artifacts/$($FileContent.Name)"
                }
                else
                {
                    $FileContent.ID = "/providers/Microsoft.Management/managementGroups/$($ManagementGroupID)/providers/Microsoft.Blueprint/blueprints/$($BluePrintName)/artifacts/$($FileContent.Name)"    
                }
                
            }
            $JSONArray = $JSONArray + $FileContent
        }

        # Let's publish the Blueprint First
        foreach($JSON in $JSONArray)
        {
            # Ensuring we are 48 or less chars - limit imposed for maximum naming for a blueprint or artifact
            If($JSON.Name.Length -gt 48)
            {
                Write-Host "Blueprint and artifact names must be 48 characters or less" -ForegroundColor Green
                Write-Host "Blueprint or artifact named $($JSON.Name) is " -ForegroundColor red -NoNewline
                write-host "$($JSON.Name.Length) " -ForegroundColor Yellow -NoNewline
                Write-host "chars long - please fix before import" -ForegroundColor Red
                exit 1
            }
            if($JSON.type -EQ "Microsoft.Blueprint/blueprints")
            {
                Write-Host "Importing main Blueprint first " -ForegroundColor White -NoNewline
                write-host "$($JSON.Name)" -ForegroundColor Yellow
                $ImportURI = "https://management.azure.com$($JSON.ID)$($APIVersion)"
                $Body = $JSON|ConvertTo-Json -depth 50 -Compress -ErrorAction Stop

                # Put call
                $Putconfig = build-PutContent -URI $ImportURI -BodyContent $Body
           
                try
                {
                    $PutEvent = Invoke-WebRequest @Putconfig
                }
                catch
                {
                    StandardError -Exception $($_.ErrorDetails.Message)
                    exit 1
                }
            }
        }
        foreach($JSON in $JSONArray)
        {
            if($JSON.type -EQ "Microsoft.Blueprint/blueprints/artifacts")
            {
                Write-Host "Importing $($JSON.Kind) artifact " -ForegroundColor White -NoNewline
                write-host "$($JSON.Name)" -ForegroundColor Yellow
                $ImportURI = "https://management.azure.com$($JSON.ID)$($APIVersion)"
                $Body = $JSON|ConvertTo-Json -depth 50 -Compress -ErrorAction Stop

                # Put call
                $Putconfig = build-PutContent -URI $ImportURI -BodyContent $Body
           
                try
                {
                    $PutEvent = Invoke-WebRequest @Putconfig
                }
                catch
                {
                    StandardError -Exception $($_.ErrorDetails.Message)
                    exit 1
                }
            }
        }
        write-host "Complete!"
    }
    else
    {
            Write-Host "You selected No - exiting"
            Write-Host "Complete" -ForegroundColor Cyan
            exit
    }
    
}