-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWSAccounts.ps1
72 lines (58 loc) · 1.72 KB
/
AWSAccounts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
add-type @"
public struct AWSAccount {
public string Name;
public object Credentials;
}
"@
Function Get-NewAWSAccountObject($profile){
$account = new-object -TypeName AWSAccount
$account.Name = $profile
$account.Credentials = Get-AWSCredentials -ProfileName $profile
return $account
}
Function Get-DefaultAccount(){
if ($configuration.accounts){
if ($configuration.accounts.default){
return $configuration.accounts.default.name
}
}
}
Function Get-Account ($filter) {
foreach ($profilename in (Get-AWSCredentials -ListProfiles)) { if ($profilename -match $filter) {$profilename}}
}
Set-Alias -name accounts -value Get-Account
Function Select-Account ($filter=$null) {
if ($filter -eq $null){
return Get-Status
}
$ProfileName = get-account $filter
if ($ProfileName.count -eq 0) {
Write-Error "Unable to find account! $filter"
return $null
}
if ($ProfileName.count -eq 1){
$global:CurrentProfile = $ProfileName;
$global:CurrentAccount = Get-NewAWSAccountObject($global:CurrentProfile);
return $null
}else{
Write-Error "That would match more than one account"
Write-Output $ProfileName
return $null
}
}
set-alias -name account -Value Select-account
Function Set-Credentials{
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$AccessKey,
[Parameter(Mandatory=$True,Position=2)]
[string]$SecretKey,
[Parameter(Mandatory=$False)]
[string]$ProfileName=$null
)
if ($ProfileName -eq $null){
Set-AWSCredentials -AccessKey $AccessKey -SecretKey $SecretKey
}else{
Set-AWSCredentials -AccessKey $AccessKey -SecretKey $SecretKey -StoreAs $ProfileName
}
}