GetInfoFromUniqueId.ps1: Added ability to resolve users, groups, and roles
This commit is contained in:
parent
cf16558acc
commit
53e290bd48
1 changed files with 63 additions and 47 deletions
|
@ -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){
|
switch($uniqueIdType){
|
||||||
# "ABIA" { $command="" }
|
# "ABIA" { $command="" }
|
||||||
# "ACCA" { $command="" }
|
# "ACCA" { $command="" }
|
||||||
"AGPA" { $command="list-groups" }
|
"AGPA" { $command="list-groups"
|
||||||
"AIDA" { $command="list-users" }
|
$types="Groups"
|
||||||
|
$type="Group" }
|
||||||
|
"AIDA" { $command="list-users"
|
||||||
|
$types="Users"
|
||||||
|
$type="User" }
|
||||||
# "AIPA" { $command="" }
|
# "AIPA" { $command="" }
|
||||||
"AKIA" { $command="" }
|
# "AKIA" { $command="" }
|
||||||
"ANPA" { $command="list-policies" }
|
"ANPA" { $command="list-policies"
|
||||||
|
$types="Policies"
|
||||||
|
$type="Policy" }
|
||||||
# "ANVA" { $command="" }
|
# "ANVA" { $command="" }
|
||||||
# "APKA" { $command="" }
|
# "APKA" { $command="" }
|
||||||
"AROA" { $command="list-roles" }
|
"AROA" { $command="list-roles"
|
||||||
"ASCA" { $command="list-server-certificates" }
|
$types="Roles"
|
||||||
|
$type="Role" }
|
||||||
|
# "ASCA" { $command="list-server-certificates"
|
||||||
|
# $types="ServerCertificateMetadataList" }
|
||||||
# "ASIA" { $command="" }
|
# "ASIA" { $command="" }
|
||||||
default { Write-Output "Invalid 'target' value."; return}
|
default { Write-Output "Invalid 'target' value."; return}
|
||||||
}
|
}
|
||||||
|
|
||||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
|
||||||
$awsCommand = "aws iam $command"
|
$awsCommand = "aws iam $command"
|
||||||
Invoke-Expression $awsCommand -OutVariable succOut -ErrorVariable errOut 2>&1 >$null
|
Invoke-Expression $awsCommand -OutVariable succOut -ErrorVariable errOut 2>&1 >$null
|
||||||
|
|
||||||
if ($errOut -ne $null) {
|
if ($errOut -ne $null) {
|
||||||
Write-Output "$($errOut[1].ToString())"
|
Write-Output "$($errOut[1].ToString())"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$returnedObjects = $succOut | ConvertFrom-Json
|
$returnedObjects = ($succOut | ConvertFrom-Json).$types
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($object in $returnedObjects.Policies) {
|
$selectedObject = $returnedObjects | Where-Object {$_."$($type)Id" -eq $target}
|
||||||
if ("$target" -eq "$($object.PolicyId)") {
|
|
||||||
if ($name -eq $true) {
|
if ($selectedObject -eq $null) {
|
||||||
Write-Output "$($object.PolicyName)"
|
Write-Output "Unique ID '$target' not found"
|
||||||
}
|
|
||||||
if ($arn -eq $true) {
|
|
||||||
Write-Output "$($object.Arn)"
|
|
||||||
}
|
|
||||||
if ($id -eq $true) {
|
|
||||||
Write-Output "$($object.PolicyId)"
|
|
||||||
}
|
|
||||||
return
|
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
|
|
Loading…
Reference in a new issue