PowerShell-Scripts/GetInfoFromUniqueId.ps1

77 lines
No EOL
2.4 KiB
PowerShell

# 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 -id # Returns the ID, acts as a way of confirming the resource exists
# .\GetInfoFromUniqueId.ps1 -target ANPAXXXXXXXXXXXXXXXXX -json # Returns json of all the details
Param (
[parameter(Mandatory=$true)][string]$target,
[switch]$arn,
[switch]$name,
[switch]$id,
[switch]$json
)
function GetInfoFromUniqueId($target) {
$uniqueIdType = "$($target.Substring(0,4))"
switch($uniqueIdType){
# "ABIA" { $command="" }
# "ACCA" { $command="" }
"AGPA" { $command="list-groups"
$types="Groups"
$type="Group" }
"AIDA" { $command="list-users"
$types="Users"
$type="User" }
# "AIPA" { $command="" }
# "AKIA" { $command="" }
"ANPA" { $command="list-policies"
$types="Policies"
$type="Policy" }
# "ANVA" { $command="" }
# "APKA" { $command="" }
"AROA" { $command="list-roles"
$types="Roles"
$type="Role" }
# "ASCA" { $command="list-server-certificates"
# $types="ServerCertificateMetadataList" }
# "ASIA" { $command="" }
default { Write-Output "Invalid 'target' value."; return}
}
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
$awsCommand = "aws iam $command"
Invoke-Expression $awsCommand -OutVariable succOut -ErrorVariable errOut 2>&1 >$null
if ($errOut -ne $null) {
Write-Output "$($errOut[1].ToString())"
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"
}
}
return GetInfoFromUniqueId($target)