GetInfoFromUniqueId.ps1: Added ability to resolve users, groups, and roles

This commit is contained in:
Oscar Pocock 2023-01-26 16:56:24 +00:00
parent cf16558acc
commit 53e290bd48

View file

@ -2,60 +2,76 @@
# Use: # Use:
# .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -name # Returns name of resource # .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -name # Returns name of resource
# .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -arn # Returns arn 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 -id # Returns the ID, acts as a way of confirming the resource exists
# .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -arn -name -id # Returns arn, name and unique id of resource # .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -json # Returns json of all the details
Param ( Param (
[string]$target, [parameter(Mandatory=$true)][string]$target,
[switch]$arn, [switch]$arn,
[switch]$name, [switch]$name,
[switch]$id [switch]$id,
[switch]$json
) )
$uniqueIdType = "$($target.Substring(0,4))" function GetInfoFromUniqueId($target) {
$uniqueIdType = "$($target.Substring(0,4))"
switch($uniqueIdType){
# "ABIA" { $command="" } switch($uniqueIdType){
# "ACCA" { $command="" } # "ABIA" { $command="" }
"AGPA" { $command="list-groups" } # "ACCA" { $command="" }
"AIDA" { $command="list-users" } "AGPA" { $command="list-groups"
# "AIPA" { $command="" } $types="Groups"
"AKIA" { $command="" } $type="Group" }
"ANPA" { $command="list-policies" } "AIDA" { $command="list-users"
# "ANVA" { $command="" } $types="Users"
# "APKA" { $command="" } $type="User" }
"AROA" { $command="list-roles" } # "AIPA" { $command="" }
"ASCA" { $command="list-server-certificates" } # "AKIA" { $command="" }
# "ASIA" { $command="" } "ANPA" { $command="list-policies"
default { Write-Output "Invalid 'target' value."; return} $types="Policies"
} $type="Policy" }
# "ANVA" { $command="" }
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") # "APKA" { $command="" }
$awsCommand = "aws iam $command" "AROA" { $command="list-roles"
Invoke-Expression $awsCommand -OutVariable succOut -ErrorVariable errOut 2>&1 >$null $types="Roles"
$type="Role" }
if ($errOut -ne $null) { # "ASCA" { $command="list-server-certificates"
Write-Output "$($errOut[1].ToString())" # $types="ServerCertificateMetadataList" }
return # "ASIA" { $command="" }
} default { Write-Output "Invalid 'target' value."; return}
else { }
$returnedObjects = $succOut | ConvertFrom-Json
} $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
$awsCommand = "aws iam $command"
foreach($object in $returnedObjects.Policies) { Invoke-Expression $awsCommand -OutVariable succOut -ErrorVariable errOut 2>&1 >$null
if ("$target" -eq "$($object.PolicyId)") {
if ($name -eq $true) { if ($errOut -ne $null) {
Write-Output "$($object.PolicyName)" Write-Output "$($errOut[1].ToString())"
}
if ($arn -eq $true) {
Write-Output "$($object.Arn)"
}
if ($id -eq $true) {
Write-Output "$($object.PolicyId)"
}
return return
} }
else {
$returnedObjects = ($succOut | ConvertFrom-Json).$types
}
$selectedObject = $returnedObjects | Where-Object {$_."$($type)Id" -eq $target}
if ($selectedObject -eq $null) {
Write-Output "Unique ID '$target' not found"
return
}
if ($json) {
return $selectedObject | ConvertTo-Json
}
if ($name) {
return $selectedObject."$($type)Name"
}
if ($arn) {
return $selectedObject.Arn
}
if ($id) {
return $selectedObject."$($type)Id"
}
} }
Write-Output "Unique ID '$target' not found" return GetInfoFromUniqueId($target)
exit