-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS_Tools_AddOns.psm1
983 lines (864 loc) · 35.4 KB
/
AWS_Tools_AddOns.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
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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# Updated
function Get-S3Folder () {
Param(
[Parameter(Mandatory = $true )]
[String]$BucketName,
[String]$Prefix,
[switch]$Files,
[switch]$Folders,
[Int]$MaxServiceCallHistory=50
)
If ($MaxServiceCallHistory) {
Set-AWSHistoryConfiguration -MaxServiceCallHistory $MaxServiceCallHistory
}
$result = [System.Collections.Generic.List[PSObject]]::New()
$Prefix += ($Prefix.EndsWith("/")) ? "" : "/"
$s3Files = Get-S3Object -BucketName $BucketName -Prefix $Prefix -Delimiter '/'
$s3Folders = ($AWSHistory.LastCommand.Responses.History).CommonPrefixes
if ($Files) {
foreach ($s3File in $s3Files) {
$result.add(
[PSCustomObject]@{
Key = $S3File.Key
}
)
}
} elseif ($Folders) {
foreach ($s3Folder in $s3Folders) {
$result.Add(
[PSCustomObject]@{
Key = $S3Folder
}
)
}
} else {
foreach ($s3File in $s3Files) {
$result.add(
[PSCustomObject]@{
Key = $S3File.Key
}
)
}
foreach ($s3Folder in $s3Folders) {
$result.Add(
[PSCustomObject]@{
Key = $S3Folder
}
)
}
}
return $result.ToArray() | Sort-Object -Property Key
<#
.SYNOPSIS
List S3 Folders
.DESCRIPTION
This function emulates working with folders "Common prefixes" in S3. It will list the files and top level prefixes for a given bucket and prefix.
.PARAMETER BucketName
The name of the bucket
.PARAMETER Prefix
The prefix to list.
.PARAMETER Files
Only return the files in the top level prefix.
.PARAMETER Folders
Only return the folders in the top level prefix
.PARAMETER MaxServiceCallHistory
To get the common prefixes we call the $AWSHistory.LastCommand. By default that only returns the last 10 commands.
So we set this to 50 as our default. This is usually fine for most uses unless you have a prefix with lot of sub-prefixes and files.
.OUTPUTS
An array of prefixes and/or files.
#>
}
function Restore-S3Folder () {
Param(
[Parameter(Mandatory)]
[string]$BucketName,
[Parameter(Mandatory)]
[string]$Prefix,
[Parameter(Mandatory)]
[int]$CopyLifetime,
[Parameter(Mandatory)]
[ValidateSet("Standard", "Expedited","Bulk")]
[string]$Tier
)
#Validate the bucket exists
$Bucket = Get-S3Bucket -BucketName $BucketName
If (-not $Bucket) {
Write-Host "Bucket not found!" -ForegroundColor Red
exit
}
$Prefix += ($Prefix.EndsWith("/")) ? "" : "/"
$s3Keys = Get-S3Object -BucketName $BucketName -Prefix $Prefix
if ($s3Keys) {
foreach ($S3Key in $s3Keys) {
$msg = "Restoring object {0} to {1} for {2} days" -f $s3Key.Key, $Tier, $CopyLifetime
Write-Host $msg -ForegroundColor Yellow
$S3Key | Restore-S3Object -CopyLifetimeInDays $CopyLifetime -Tier $Tier -ErrorAction SilentlyContinue | Out-Null
}
}
<#
.SYNOPSIS
Restore an S3 folder, i.e. "common prefix", from Glacier.
.DESCRIPTION
AWS Powershell Tools for S3 only has the ability to restore a single s3 object from glacier.
This function allows you to restore all object with a common prefix.
.PARAMETER BucketName
The bucket name.
.PARAMETER Prefix
The Prefix to restore.
.PARAMETER CopyLifetime
The Number of days to keep the restored objects before returning them to glacier.
.PARAMETER Tier
The storage tier to restore the objects to. Valid entries are Standard, Expedited, Bulk
.OUTPUTS
Response indicating success or failure.
#>
}
Function Get-S3RestoreProgress() {
Param(
[Parameter(Mandatory)]
[String]$BucketName,
[Parameter(ParameterSetName='prefix')]
[string]$Prefix,
[Parameter(ParameterSetName='key')]
[string]$Key
)
#validate bucket name
$Bucket = Get-S3Bucket -BucketName $BucketName
If (-not $Bucket) {
Throw "Bucket not found"
}
if ($Key) {
Get-S3ObjectMetadata -BucketName $BucketName -Key $key | Select-Object @{Name="Key";Expression={$Key}},RestoreInProgress, RestoreExpiration
} else {
$Prefix += ($Prefix.EndsWith("/")) ? "" : "/"
$s3Keys = Get-S3Object -BucketName $BucketName -Prefix $Prefix
$S3Keys |Foreach-Object {
# Remove any by-directional unicode characters the stupid users may have entered into the file path & name
$_.Key = $_.Key -cReplace '\P{IsBasicLatin}'
$MetaData = $_ | Get-S3ObjectMetadata
$Progress = [PSCustomObject]@{
Key = $_.Key
RestoreInProgress = $MetaData.RestoreInProgress
RestoreExpiration = $MetaData.RestoreExpiration
}
Write-Output $Progress
}
}
<#
.SYNOPSIS
Display the progress of a Glacier Restore.
.PARAMETER BucketName
The bucket name.
.PARAMETER Prefix
The prefix to check the restore progress. Required if Key is omitted.
.PARAMETER Key
The full key of an object to check. Required if Prefix is omitted.
.OUTPUTS
An array of AWS S3 Metadata objects. Check the RestoreInProgress property. A value of false indicates the restore has completed.
#>
}
function Get-IAMGroupPermissions() {
Param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string]$GroupName
)
$IamGroup = (Get-IAMGroup -GroupName $GroupName).Group
$Group = [PsCustomObject]@{
Arn = $IamGroup.Arn
GroupName = $IamGroup.GroupName
GroupId = $IamGroup.GroupId
CreateDate = $IamGroup.CreateDate
Policies = [List[PsObject]]::New()
}
# Get any inline policies
$GroupInlinePolicies = Get-IAMGroupPolicyList -GroupName $GroupName
if ($GroupInlinePolicies) {
foreach ($GroupInlinePolicy in $GroupInLinePolicies) {
$iamPolicy = Get-IAMGroupPolicy -PolicyName $GroupInlinePolicy -GroupName $GroupName
$PolicyDocument = [HttpUtility]::UrlDecode($IamPolicy.PolicyDocument) | ConvertFrom-Json
$Policy = [PsCustomObject]@{
PolicyName = $IamPolicy.PolicyName
Arn = "N/A"
PolicyDocument = $PolicyDocument
PolicyType = 'Inline'
}
$Group.Policies.Add($Policy)
}
}
# Get any Group Attached Policies.
$GroupAttachedPolicies = Get-IAMAttachedGroupPolicyList -GroupName $GroupName
if ($GroupAttachedPolicies) {
foreach($GroupAttachedPolicy in $GroupAttachedPolicies) {
$IamPolicy = Get-IamPolicy -PolicyArn $GroupAttachedPolicy.PolicyArn
if ($IamPolicy.arn -like "*aws:policy*") {
$PolicyTYpe = 'AWS Managed'
} else {
$PolicyType = 'Customer Managed'
}
# Get the default version Id
$VersionId = (Get-IAMPolicyVersions -PolicyArn $IamPolicy.Arn | Where-Object {$_.IsDefaultVersion -eq $True}).VersionId
$PolicyDocument = [HttpUtility]::UrlDecode((Get-IAMPolicyVersion -PolicyArn $IamPolicy.Arn -VersionId $VersionId).Document)
$Policy = [PsCustomObject]@{
PolicyName = $IamPolicy.Name
Arn = $IamPolicy.Arn
PolicyDocument = $PolicyDocument.ToString()
PolicyType = $PolicyType
}
$Group.Policies.Add($Policy)
}
}
return $Group
<#
.SYNOPSIS
Returns the permissions assigned to this group.
.DESCRIPTION
Returns an object containing the permissions and policies assigned to a group.
.PARAMETER GroupName
The name of the group.
.OUTPUTS
An object with the following properties.
.NOTES
The Group Object contains the following properties.
Name Type
--------------- -------------
Arn String
GroupName String
GroupId String
CreateData DateTime
Policies Collection
The Policies property contains a collection of Policy objects with the following properties.
Name Type
--------------- ----------------------
PolicyName String
Arn String (Applicable for managed policies only)
PolicyDocument PsObject
PolicyType String (either 'inline', 'AWSManaged', or 'Customer Managed')
The PolicyDocument (and the role AssumedRolePolicyDocument) have the following properties
Name Type
------------------- -------------------
Version String
Statement PsObject(s) May be a single object or an array.
The Statement Object has the following Properties
Name Type
-------------------- -------------------
Sid String
Effect String (either Allow or Deny)
Actions Array of String objects
Resource String
#>
}
function Get-IAMUserPermissions() {
[CmdletBinding()]
Param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string]$Username
)
# Get the IAM User
$IamUser = Get-IAMUser -UserName $Username
# Create a PSObject for the User Permissions
$UserPermissions = [psCustomObject]@{
Arn = $IamUser.Arn
CreateDate = $IamUser.CreateDate
PasswordLastUsed = $IamUser.PasswordLastUsed
UserId = $IamUser.UserId
UserName = $IamUser.UserName
Policies = [List[PsObject]]::New()
Groups = [List[PsObject]]::New()
}
#Get any inline policies assigned to the user.
$UserInlinePolicies = Get-IamUserPolicyList -UserName $IamUser.Username
foreach ($UserInlinePolicy in $UserInlinePolicies) {
$UserPolicy = Get-IAMUserPolicy -PolicyName $UserInlinePolicies.PolicyName -UserName $IamUser.UserName
$PolicyDocument = [HttpUtility]::UrlDecode($UserPolicy.PolicyDocument)
$Policy = [PsCustomObject]@{
PolicyName = $IamPolicy.Name
Arn = "N/A"
PolicyDocument = $PolicyDocument
PolicyType = 'Inline'
}
$UserPermissions.InlinePolicies.Add($Policy)
}
# Get any attached Policies for this user.
$UserAttachedPolicies = Get-IAMAttachedUserPolicies -UserName $Username
foreach ($UserAttachedPolicy in $UserAttachedPolicies) {
$IamPolicy = Get-IAMPolicy -PolicyArn $UserAttachedPolicy.PolicyArn
if ($IamPolicy.Arn -like "*aws:policy") {
$PolicyType | Add-Member -MemberType NoteProperty -Name "PolicyType" -Value "AWS Managed"
} else {
$PolicyType | Add-Member -MemberType NoteProperty -Name "MemberType" -Value "Customer Managed"
}
$PolicyDocument = [HttpUtility]::UrlDecode($IamPolicy.PolicyDocument)
$Policy = [PsCustomObject]@{
PolicyName = $IamPolicy.Name
Arn = $IamPolicy.Arn
PolicyDocument = $PolicyDocument.ToString()
PolicyType = $PolicyType
}
UserPermissions.Policies.Add($Policy)
}
# Get any Groups that the user is a member of.
$UserGroups = Get-IAMGroupForUser -UserName $IamUser.Username
foreach ($UserGroup in $UserGroups) {
$Group = Get-IAMGroupPermissions -GroupName $UserGroup.GroupName
$UserPermissions.Groups.Add($Group)
}
return $UserPermissions
<#
.SYNOPSIS
Returns permissions assigned to a user.
.DESCRIPTION
Returns an object containing user information and the permissions and policies assigned to a user.
Also contains a collection of group objects the User is a member of (See Get-IAMGroupPermissions).
.PARAMETER Username
The Name of the user.
.OUTPUTS
An object containing user information and policies assigned to the user.
.NOTES
The User Object contains the following properties.
Name Type
------------------- ----------------------
Arn String
Username String
UserId String
CreateDate DateTime
PasswordLastUsed DateTime
Policies Collection of Policies assigned to the group.
Groups Groups the user is a member of (Group Objects, see Get-IAMGroupPermissions)
The Policy objects in the Policies collection have the following properties.
Name Type
------------------- ---------------------
PolicyName String
Arn String (Applicable for managed policies only)
PolicyDocument PsObject
PolicyType String (either 'inline', 'AWSManaged', or 'Customer Managed')
The PolicyDocument (and the role AssumedRolePolicyDocument) have the following properties
Name Type
------------------- -------------------
Version String
Statement PsObject(s) May be a single object or an array.
The Statement Object has the following Properties
Name Type
-------------------- -------------------
Sid String
Effect String (either Allow or Deny)
Actions Array of String objects
Resource String
#>
}
function Get-IAMRolePermissions() {
Param(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string]$RoleName
)
$IamRole = Get-IAMRole -RoleName $RoleName
$AssumeRolePolicyDocument = [HttpUtility]::UrlDecode($IamRole.AssumeRolePolicyDocument) | ConvertFrom-Json
$Role = [PSCustomObject]@{
Arn = $IamRole.Arn
RoleName = $IamRole.RoleName
RoleId = $IamRole.RoleId
RoleLastUsed = $IamRole.RoleLastUsed
CreateDate = $IamRole.CreateDate
MaxSessionDuration = $IamRole.MaxSessionDuration
AssumeRolePolicyDocument = $AssumeRolePolicyDocument
Policies = [List[PsObject]]::New()
}
# Get inline policies
$RoleInlinePolicies = Get-IAMRolePolicies -RoleName $RoleName
If ($RoleInlinePolicies) {
foreach ($RoleInlinePolicy in $RoleInlinePolicies) {
$IamPolicy = Get-IAMRolePolicy -PolicyName $RoleInlinePolicy -RoleName $RoleName
$PolicyDocument = [HttpUtility]::UrlDecode($IamPolicy.PolicyDocument) | ConvertFrom-Json
$Policy = [PSCustomObject]@{
PolicyName = $IamPolicy.PolicyName
Arn = "N/A"
PolicyDocument = $PolicyDocument
PolicyType = 'InLine'
}
$Role.Policies.Add($Policy)
}
}
# Get any attached Policies
$RoleAttachedPolicies = Get-IAMAttachedRolePolicies -RoleName $RoleName
if ($RoleAttachedPolicies) {
foreach ($RoleAttachedPolicy in $RoleAttachedPolicies) {
$IamPolicy = Get-IamPolicy -PolicyArn $RoleAttachedPolicy.PolicyArn
if ($IamPolicy.arn -like "*aws:policy*") {
$PolicyType = 'AWS Managed'
} else {
$PolicyType = 'Customer Managed'
}
$VersionId = (Get-IAMPolicyVersions -PolicyArn $IamPolicy.Arn | Where-Object {$_.IsDefaultVersion -eq $True}).VersionId
$PolicyDocument = [HttpUtility]::UrlDecode((Get-IAMPolicyVersion -PolicyArn $IamPolicy.Arn -VersionId $VersionId).Document) | ConvertFrom-Json
$Policy = [PSCustomObject]@{
PolicyName = $IamPolicy.PolicyName
Arn = $IamPolicy.Arn
PolicyDocument = $PolicyDocument
$PolicyType = $PolicyType
}
$Role.Policies.Add($Policy)
}
}
return $Role
<#
.SYNOPSIS
Returns the permissions assigned to a role.
.DESCRIPTION
Returns an object containing the permissions and policies assigned to a role.
.PARAMETER RoleName
The name of the role.
.OUTPUTS
A role permissions object.
.NOTES
The Role permissions object has the following properties.
Name Type
--------------------------- ----------------
Arn String
RoleName String
RoleId String
CreateDate DateTime
MaxSessionDuration Integer
AssumeRolePolicyDocument PsObject
Policies Collection of Policies assigned to the role.
The Policy objects in the Policies collection have the following properties.
Name Type
------------------- -------------------
PolicyName String
Arn String (Applicable for managed policies only)
PolicyDocument PsObject
PolicyType String (either 'inline', 'AWSManaged', or 'Customer Managed')
The PolicyDocument (and the role AssumedRolePolicyDocument) have the following properties
Name Type
------------------- -------------------
Version String
Statement PsObject(s) May be a single object or an array.
The Statement Object has the following Properties
Name Type
-------------------- -------------------
Sid String
Effect String (either Allow or Deny)
Actions Array of String objects
Resource String
#>
}
function Get-EC2InstanceList() {
[CmdletBinding()]
Param(
[string]$Name,
[string]$InstanceId,
[string]$ProfileName
)
If ($ProfileName) {
try {
Set-AWSCredential -ProfileName $ProfileName
} catch {
throw $_
}
}
$Ec2Subnets = @{}
$EC2INstanceTypes = @{}
$EC2InstanceStatus = @{}
function Get-AssociatedData() {
Param (
[PsObject]$Instance
)
# Get accociated data and create indexed lists
$Params = @{}
if ($Instance) {
$Params.Add("SubnetId", $Instance.SubnetId)
}
Get-EC2Subnet @Params | ForEach-Object {
$Ec2Subnets.Add($_.SubnetId, $_)
}
$Params = @{}
if ($Instance) {
$Params.Add("InstanceType", $Instance.InstanceType)
}
Get-EC2InstanceType @Params | Foreach-Object {
$EC2INstanceTypes.Add($_.InstanceType, $_)
}
$Params = @{}
if ($Instance) {
$Params.Add("InstanceId", $Instance.InstanceId)
}
Get-EC2InstanceStatus @Params | ForEach-Object {
$EC2InstanceStatus.Add($_.Instanceid, $_)
}
}
$EC2InstanceList = [List[psObject]]::New()
if ($InstanceId) {
[array]$EC2Instances = (Get-EC2Instance -InstanceId $InstanceId).Instances
Get-AccociatedData -Instance $EC2Instances[0]
} elseIf ($Name) {
[array]$EC2INstances = (Get-EC2Instance).Instances | Where-Object {$_.Tags[$_.Tags.Key.IndexOf("Name")].Value -eq $Name}
Get-AssociatedData -Instance $EC2Instances[0]
} else {
[array]$EC2Instances = (Get-EC2instance).Instances
Get-AssociatedData
}
$AccountId = (Get-STSCallerIdentity).Account
foreach ($EC2Instance in $EC2INstances) {
$Tags = $EC2Instance.Tags
$Name = $Tags[$Tags.Key.IndexOf("Name")].Value
$State = $EC2Instance.State.Name
$AvailabilityZone = $EC2InstanceStatus[$EC2Instance.InstanceId].AvailabilityZone #(Get-EC2InstanceStatus -InstanceId $EC2Instance.InstanceId -IncludeAllInstance $true).AvailabilityZone
$SecurityGroup = $EC2INstance.SecurityGroups.GroupName -join ","
$SubnetTags = $Ec2Subnets[$EC2Instance.SubnetId].Tags #(Get-EC2Subnet -SubnetId $EC2Instance.SubnetId).Tags
if ($SubnetTags) {
$SubnetName = $SubnetTags[$SubnetTags.Key.IndexOf("Name")].Value
} else {
$SubnetName = $null
}
$InstanceType = $EC2INstanceTypes[$EC2INstance.InstanceType] #Get-EC2InstanceType -InstanceType $EC2Instance.InstanceType
$ProcessorMfr = $InstanceType.ProcessorInfo.Manufacturer
$ProcessorArchitectures = $InstanceType.ProcessorInfo.SupportedArchitectures
#$ProcessorFeatures = $InstanceType.ProccessorInfo.SupportedFeatures
$ProcessorClockSpeed = $InstanceType.ProcessorInfo.SustainedClockSpeedInGhz
if ($InstanceType.InstanceStorageSupported) {
$InstanceStorageSupported = $InstanceType.InstanceStorageSupported
$InstanceStorageNvmeSupport = $InstanceType.InstanceStorageInfo.NvmeSupport
$InstanceStorageEncryption = $InstanceType.InstanceStorageInfo.EncryptionSupport
$InstanceStorageSize = $InstanceType.InstanceStorageInfo.TotalSizeInGB
$InstanceStorageType = $InstanceType.InstanceStorageInfo.Disks.Type
}
$Memory = $InstanceType.memoryInfo.SizeInMiB / 1024
$EnaSupported = $InstanceType.NetworkInfo.EnaSupport
$NetworkPerformance = $InstanceType.NetworkInfo.NetworkPerformance
$Instance = [PSCustomObject]@{
Name = $Name
InstanceId = $EC2Instance.InstanceId
InstanceState = $State
InstanceType = $EC2Instance.InstanceType
ImageId = $EC2Instance.ImageId
AvailabilityZone = $AvailabilityZone
SecurityGroup = $SecurityGroup
KeyName = $EC2Instance.KeyName
PrivateIpAddress = $EC2Instance.PrivateIpAddress
PrivateDnsName = $EC2Instance.PrivateDnsName
PublicIPAddress = $EC2Instance.PublicIpAddress
PublicDNSName = $EC2Instance.PublicDnsName
SubnetId = $EC2Instance.SubnetId
Subnet = $SubnetName
LaunchTime = $EC2Instance.LaunchTime
Platform = $EC2Instance.PlatformDetails
Instance = $EC2Instance
ProcessorMgr = $ProcessorMfr
ProcessorArchitecture = $ProcessorArchitectures
#ProcessorFeatures = $ProcessorFeatures
ProcessorClockSpeed = $ProcessorClockSpeed
InstanceStorageSupported = $InstanceStorageSupported
InstanceStorageNvmeSupport = $InstanceStorageNvmeSupport
InstanceStorageEncryption = $InstanceStorageEncryption
InstanceStorageSize = $InstanceStorageSize
InstanceStorageType = $InstanceStorageType
Memory = $Memory
EnaSupported = $EnaSupported
NetworkPerformance = $NetworkPerformance
AccountId = $AccountId
}
$EC2InstanceList.Add($Instance)
}
return $EC2InstanceList.ToArray()
<#
.SYNOPSIS
Returns a list of EC2 Instances
.DESCRIPTION
Returns a list of EC2 INstances with relevant properties.
.PARAMETER Name
Returns a single instance with this name (The value of ther Tag: Name)
.PARAMETER InstanceId
Returns a single instance with this instance Id/
.PARAMETER ProfileName
The saved EC2 profile to used to retrieve the data.
.OUTPUTS
A collection of custom EC2 Instance objects.
#>
}
function Get-DiskMappings() {
function Convert-SCSITargetIdToDeviceName {
param(
[int]$SCSITargetId
)
If ($SCSITargetId -eq 0) {
return "sda1"
}
$deviceName = "xvd"
If ($SCSITargetId -gt 25) {
$deviceName += [char](0x60 + [int]($SCSITargetId / 26))
}
$deviceName += [char](0x61 + $SCSITargetId % 26)
return $deviceName
}
# [string[]]$array1 = @()
# [string[]]$array2 = @()
# [string[]]$array3 = @()
# [string[]]$array4 = @()
$Win32_Volumes = Get-CimInstance -ClassName Win32_Volume
$VolNames = $Win32_Volumes.Name
$VolIDs = $Win32_Volumes.DeviceID
# Get-CimInstance -ClassName Win32_Volume | Select-Object Name, DeviceID | ForEach-Object {
# $array1 += $_.Name
# $array2 += $_.DeviceID
# }
$Serials = [List[PsObject]]::New()
$FriendlyNames = [List[PsObject]]::New()
foreach ($VolID in $VolIDs) {
Write-Host "Getting disk information for volume $VolId" -ForegroundColor Green
$disk = Get-Volume -Path $VolID | Get-Partition | Get-Disk
$Serials.Add(($Disk.SerialNumber -replace "_[^ ]*$" -replace "vol", "vol-"))
$FriendlyNames.Add($Disk.FriendlyName)
}
# $i = 0
# While ($i -ne ($array2.Count)) {
# $array3 += ((Get-Volume -Path $array2[$i] | Get-Partition | Get-Disk).SerialNumber) -replace "_[^ ]*$" -replace "vol", "vol-"
# $array4 += ((Get-Volume -Path $array2[$i] | Get-Partition | Get-Disk).FriendlyName)
# $i ++
# }
$DiskInfo = @{}
foreach ($VolName in $VolNames) {
$i = $VolNames.IndexOf($VolName)
$Drive = [PSCustomObject]@{
DriveLetter = ($VolName.replace(':\\', ''))
VolumeId = $VolIDs[$i]
Serial = $Serials[$i]
FriendlyName = $FriendlyNames[$i]
}
$DiskInfo.Add($VolName.replace(':\',''), $Drive)
}
# [array[]]$array = $VolNames, $VolIds, $Serials, $FriendlyNames
Try {
$InstanceId = Get-EC2InstanceMetadata -Category "InstanceId"
$Region = Get-EC2InstanceMetadata -Category "Region" | Select-Object -ExpandProperty SystemName
}
Catch {
Write-Host "Could not access the instance Metadata using AWS Get-EC2InstanceMetadata CMDLet.
Verify you have AWSPowershell SDK version '3.1.73.0' or greater installed and Metadata is enabled for this instance." -ForegroundColor Yellow
}
Try {
$BlockDeviceMappings = (Get-EC2Instance -Region $Region -Instance $InstanceId).Instances.BlockDeviceMappings
[array]$VirtualDeviceMap = (Get-EC2InstanceMetadata -Category "BlockDeviceMapping").GetEnumerator() | ForEach-Object {$_}
}
Catch {
Write-Host "Could not access the AWS API, therefore, VolumeId is not available.
Verify that you provided your access keys or assigned an IAM role with adequate permissions." -ForegroundColor Yellow
}
Get-disk | ForEach-Object {
$DriveLetter = $null
$VolumeName = $null
$VirtualDevice = $null
$DeviceName = $_.FriendlyName
$DiskDrive = $_
$Disk = $_.Number
$Partitions = $_.NumberOfPartitions
$EbsVolumeID = $_.SerialNumber -replace "_[^ ]*$" -replace "vol", "vol-"
if ($Partitions -ge 1) {
$PartitionsData = Get-Partition -DiskId $_.Path
$DriveLetter = $PartitionsData.DriveLetter | Where-object { $_ -notin @("", $null) }
$VolumeName = (Get-PSDrive | Where-Object { $_.Name -in @($DriveLetter) }).Description | Where-object { $_ -notin @("", $null) }
}
If ($DiskDrive.path -like "*PROD_PVDISK*") {
$BlockDeviceName = Convert-SCSITargetIdToDeviceName((Get-CIMInstance -ClassName Win32_Diskdrive | Where-Object { $_.DeviceID -eq ("\\.\PHYSICALDRIVE" + $DiskDrive.Number) }).SCSITargetId)
$BlockDeviceName = "/dev/" + $BlockDeviceName
$BlockDevice = $BlockDeviceMappings | Where-Object { $BlockDeviceName -like "*" + $_.DeviceName + "*" }
$EbsVolumeID = $BlockDevice.Ebs.VolumeId
# $VirtualDevice = ($VirtualDeviceMap.GetEnumerator() | Where-Object { $_.Value -eq $BlockDeviceName }).Key | Select-Object -First 1
$VirtualDevice = $VirtualDeviceMap.Where({$_.Value -eq $BlockDeviceName}).Key | Select-Object -First 1
}
ElseIf ($DiskDrive.path -like "*PROD_AMAZON_EC2_NVME*") {
$BlockDeviceName = Convert-SCSITargetIdToDeviceName((Get-CIMInstance -ClassName Win32_Diskdrive | Where-Object { $_.DeviceID -eq ("\\.\PHYSICALDRIVE" + $DiskDrive.Number) }).SCSITargetId)
$BlockDevice = $null
#$VirtualDevice = ($VirtualDeviceMap.GetEnumerator() | Where-Object { $_.Value -eq $BlockDeviceName }).Key | Select-Object -First 1
$VirtualDevice = $VirtualDeviceMap.Where({$_.Value -eq $BlockDeviceName}).Key | Select-Object -First 1
}
ElseIf ($DiskDrive.path -like "*PROD_AMAZON*") {
if ($DriveLetter -match '^[a-zA-Z0-9]') {
$DeviceName = $DiskInfo["$DriveLetter"].FriendlyName
# $i = 0
# While ($i -ne ($array3.Count)) {
# if ($array[2][$i] -eq $EbsVolumeID) {
# $DriveLetter = $array[0][$i]
# $DeviceName = $array[3][$i]
# }
# $i ++
# }
}
$BlockDevice = ""
$BlockDeviceName = ($BlockDeviceMappings | Where-Object { $_.ebs.VolumeId -eq $EbsVolumeID }).DeviceName
$VirtualDevice = $VirtualDeviceMap.Where({$_.Value -eq $BlockDeviceName}).Key | Select-Object -First 1
}
ElseIf ($DiskDrive.path -like "*NETAPP*") {
if ($DriveLetter -match '^[a-zA-Z0-9]') {
$DeviceName = $DiskInfo["$DriveLetter"].FriendlyName
# $i = 0
# While ($i -ne ($array3.Count)) {
# if ($array[2][$i] -eq $EbsVolumeID) {
# $DriveLetter = $array[0][$i]
# $DeviceName = $array[3][$i]
# }
# $i ++
# }
}
$EbsVolumeID = "FSxN Volume"
$BlockDevice = ""
$BlockDeviceName = ($BlockDeviceMappings | Where-Object { $_.ebs.VolumeId -eq $EbsVolumeID }).DeviceName
}
Else {
$BlockDeviceName = $null
$BlockDevice = $null
}
New-Object PSObject -Property @{
Disk = $Disk;
Partitions = $Partitions;
DriveLetter = If ($null -eq $DriveLetter) { "N/A" } Else { $DriveLetter };
EbsVolumeId = If ($null -eq $EbsVolumeID) { "N/A" } Else { $EbsVolumeID };
Device = If ($null -eq $BlockDeviceName) { "N/A" } Else { $BlockDeviceName };
VirtualDevice = If ($null -eq $VirtualDevice) { "N/A" } Else { $VirtualDevice };
VolumeName = If ($null -eq $VolumeName) { "N/A" } Else { $VolumeName };
DeviceName = If ($null -eq $DeviceName) { "N/A" } Else { $DeviceName };
}
} | Sort-Object Disk | Select-Object Disk, Partitions, DriveLetter, EbsVolumeId, Device, VirtualDevice, DeviceName, VolumeName
<#
.SYNOPSIS
LIst disk mappings on an EC2 Instance.
.DESCRIPTION
List the disk mappings on an EC2 instance to reference the volume ID with the Windows volume and drive letter.
This function is only supported on EC2 Windows instance.
.OUTPUTS
An array of disk objects.
#>
}
function Set-SecretVault() {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$VaultName,
[ValidateSet('Password','None')]
[string]$Authentication,
[ValidateSet('Prompt','None')]
[string]$Interaction
)
$Config = Get-SecretStoreConfiguration | Where-Object {$_.Scope -eq "CurrentUser"}
$Params = @{}
If ($Authentication) {
if ($Authentication -ne $Config.Authentication) {
$Params.Add("Authentication", $Authentication)
}
}
if ($Interaction) {
if ($Interaction -ne $Config.Interaction) {
$Params.Add("Interaction", $Interaction)
}
}
If ($Params.Count -gt 0) {
Set-SecretStoreConfiguration -Scope CurrentUser @Params
}
Register-SecretVault -Name $VaultName -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault -AllowClobber
Write-Host "Vault $VaultName Created"
<#
.DESCRIPTION
Creates a Secrets vault and sets the option configuration parameters.
Note: If you plan to use this vault for automation purposes you must set Authentication and Interaction to 'None'.
.PARAMETER VaultName
The Name of the vault.
.PARAMETER Authentication
The type of Authentication, Either 'Password' or 'None'
.PARAMETER Interaction
Allow or suppress user interaction. Either 'Prompt' or 'None'. If set to none and the vault requires a password an error will occur.
.NOTES
The powershell module SecretStore which this module uses does not support vaults outside the user scope. Therefor creating multiple vaults is redundant.
SecretStore will just save secrets in all vaults. This is by design from Microsoft. It may change in the future.
If you are using this vault for secure AWS Access keys you CANNOT set authentication to password. The credential_process functionality of the
AWS credential process will hang waiting on the prompt and will not accept input from the console. You must set authentication and Interaction to 'none'.
#>
}
function Set-SecureAWSCredentials() {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[String]$ProfileName,
[string]$AccessKeyId,
[Parameter(Mandatory)]
[string]$SecretAccessKey,
[string]$SessionToken,
[Parameter(Mandatory)]
[String]$Region,
[datetime]$Expiration,
[string]$VaultName
)
$Cred = Get-AWSCredential -ProfileName $ProfileName
if ($Cred) {
Remove-AWSCredentialProfile -ProfileName $ProfileName -Force
}
$secretIn = @{
Version=1;
AccessKeyId= $AccessKeyId;
SecretAccessKey=$SecretAccessKey;
}
if ($SessionToken) {
$SecretIn.Add("SessionToken", $SessionToken)
}
if ($Expiration) {
$ExpDate = $Expiration.ToString("yyyy-MM-dd HH:mm:ss")
$SecretIn.Add("Expiration", $ExpDate)
}
$secret = $secretIn | ConvertTo-Json
$Params = @{
Name = $ProfileName
Secret = $secret
}
if ($VaultName) {
$Params.Add("Vault", $VaultName)
}
Set-Secret @Params
$CredFile ="{0}/.aws/credentials" -f $home
# Add-Content -Path $CredFile -Value "[$ProfileName]"
if ($IsWindows) {
$content = @"
[$ProfileName]
credential_process = credential_process.cmd "$ProfileName"
region = $Region
"@
Add-Content -Path $CredFile -Value $content
} else {
$content = @"
[$ProfileName]
credential_process = credential_process.sh "$ProfileName"
region = $Region
"@
Add-Content -Path $CredFile -Value $content
}
<#
.SYNOPSIS
Creates a secure entry in the aws credentials file.
.DESCRIPTION
Creates a secure entry in the AWS Credentials file. The AWS Keys are stored in a Secret vault created by Set-SecretVault.
This credential entry uses a credential process. This process calls a script based on the Operating System.
For Windows: credential_process.cmd
For Linux/Mac: credential_process.sh
Copy the appropriate file into a directory that is in the path.
For Linux the best place is ~/.local/bin
For Windows any directory that is in the path. For optimal security create a folder under the user profile and add that path to the User section of the Path Environment variable configuration.
.PARAMETER ProfileName
The name of the profile. To set a default profile you must name the profile default.
.PARAMETER AccessKeyId
The AWS Access Key ID.
.PARAMETER SecretAccessKey
The AWS Secret Access key.
.PARAMETER SessionToken
A Session Token for this credential.
.PARAMETER Region
The AWS Region for this profile.
.PARAMETER Expiration
An option expiration date, the stored secret will expire after this date/time.
.PARAMETER VaultName
An optional vault name. If omitted, the secret will be created in the default vault.
#>
}