PowerShell-Scripts/GetInfoFromUniqueId.ps1
2023-01-20 13:59:52 +00:00

58 lines
1.7 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="" }
}
$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
}
}