PowerShell-Scripts/GetInfoFromUniqueId.ps1

61 lines
1.8 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 -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="" }
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
}
foreach($object in $returnedObjects.Policies) {
if ("$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)"
}
return
}
}
Write-Output "Unique ID '$target' not found"
exit