From 7148c51f6e0b44124480b40e382f804220a39c52 Mon Sep 17 00:00:00 2001 From: Oscar Pocock Date: Fri, 20 Jan 2023 13:59:52 +0000 Subject: [PATCH] Added GetInfoFromUniqueId.ps1 --- GetInfoFromUniqueId.ps1 | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 GetInfoFromUniqueId.ps1 diff --git a/GetInfoFromUniqueId.ps1 b/GetInfoFromUniqueId.ps1 new file mode 100644 index 0000000..1ccea96 --- /dev/null +++ b/GetInfoFromUniqueId.ps1 @@ -0,0 +1,58 @@ +# Used for finding the relevant ARN or name for a policy when given the unique ID +# Use: +# .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -name # Returns name of resource +# .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -arn # Returns arn of resource +# .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -arn -name # Returns both arn and name of resource +# .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -arn -name -id # Returns arn, name and unique id of resource + +Param ( +[string]$target, +[switch]$arn, +[switch]$name, +[switch]$id +) + +$uniqueIdType = "$($target.Substring(0,4))" + +switch($uniqueIdType){ + # "ABIA" { $command="" } + # "ACCA" { $command="" } + "AGPA" { $command="list-groups" } + "AIDA" { $command="list-users" } + # "AIPA" { $command="" } + "AKIA" { $command="" } + "ANPA" { $command="list-policies" } + # "ANVA" { $command="" } + # "APKA" { $command="" } + "AROA" { $command="list-roles" } + "ASCA" { $command="list-server-certificates" } + # "ASIA" { $command="" } +} + +$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") +$awsCommand = "aws iam $command" +$retunedObjects = Invoke-Expression $awsCommand | ConvertFrom-Json + +if ($returnedObjects -eq $null) { + return +} + +$objectFound=$false + +foreach($object in $retunedObjects.Policies) { + if ($objectFound -eq $true) { + break + } + elseif ("$target" -eq "$($object.PolicyId)") { + if ($name -eq $true) { + Write-Output "$($object.PolicyName)" + } + if ($arn -eq $true) { + Write-Output "$($object.Arn)" + } + if ($id -eq $true) { + Write-Output "$($object.PolicyId)" + } + $objectFound=$true + } +}