-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_aws_instances.ps1
137 lines (117 loc) · 4.63 KB
/
check_aws_instances.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#Import (unversioned) config_file.ps1
. ./config_file.ps1
function Check_token_repiration {
$NowDate = Get-Date ## -Format yyyy-M-dd"T"HH:mm:ssZ
$myJson = Get-Content -Raw -Path $env:USERPROFILE\.aws\cli\cache\*.json | ConvertFrom-Json
##2023-04-10T18:41:53Z
$ExpirationDate = $myJson.Credentials.Expiration
Write-Host "Date expiration token : "$ExpirationDate " A comparer : " $NowDate.AddHours(-2)
if($NowDate.AddHours(-2) -ge $ExpirationDate){
aws sso login --profile $AwsProfile
}
}
## Function to check if folder where shortcuts will be created exist or not.
## It will delete
function CreateFolder {
Write-Host "## Check if $FolderName folder exist. ##"
if (Test-Path $FolderName) {
Write-Host "## Deleting every AWS shortcuts.##"
# Perform Delete file from folder operation
Get-ChildItem $FolderName\*-AWS-*.lnk | Remove-Item
}
else
{
#PowerShell Create directory if not exists
New-Item $FolderName -ItemType Directory
Write-Host "## Folder Created successfully. ##"
}
}
function Create-instance-shortcut {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$InstanceName = $env:INSTANCENAME
,
[Parameter(Mandatory)]
[string]$InstanceID = $env:INSTANCEID
,
[Parameter(Mandatory)]
[string]$InstanceStatus = $env:INSTANCESTATUS
,
[string]$InstanceIP = $env:INSTANCEIP
)
Write-Host "Creating shortcut for instance $InstanceName."
$Action=""
$ShortcutIcon=""
Switch ($InstanceStatus)
{
"stopped" {
$Action="start"
# key icon
$IconArrayIndex = 44
}
"running" {
$Action="stop"
# stop icon
$IconArrayIndex = 27
}
default
{
$Action="describe"
$IconArrayIndex = 22
# magnifying glass icon
}
}
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$FolderName\$Action-AWS-$InstanceName-$InstanceIP.lnk")
$Shortcut.IconLocation = "$IconLocation, $IconArrayIndex"
$Shortcut.TargetPath = "cmd.exe"
$Shortcut.Arguments = "/q /c START /MIN cmd /k (aws ec2 $Action-instances --profile $AwsProfile --instance-ids $InstanceID)"
$Shortcut.Save()
}
function GetInstancesList {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
[string]$UserName = $env:USERNAME
,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
[string]$AwsProfile = $env:AWSPROFILE
)
$MultipleInstancesStop=""
Write-Host "## Getting AWS Instances for $UserName user. ##"
Write-Host "## With tag Schedule stop-only (to exclude shared instances). ##"
## List all instances with tag User=@Username@ and tag Schedule=stop-only.
# Example of output
# i-0c30xxxxx avea-xxxxxxx stopped
# i-0a7xxxxxx avea-old-x stopped
# i-04fbxxxxx avea-old-xxxx stopped
# i-02xxxxxxxx avea-old-xxxxx stopped
# i-018xxxxx avea-cent running
aws ec2 describe-instances --filters Name=tag:User,Values=$UserName Name=tag:Schedule,Values=stop-only --profile $AwsProfile --output text --query "Reservations[*].Instances[*].{Instance_id:InstanceId,Name:Tags[?Key=='Name'].Value | [0],Instance_id:InstanceId,State:State.Name,IP:PrivateIpAddress}" | %{
Create-instance-shortcut -InstanceIP $_.split("`t")[0] -InstanceID $_.split("`t")[1] -InstanceName $_.split("`t")[2] -InstanceStatus $_.split("`t")[3]
#Case for multi-instances stop (from instance status) in the loop for each.
If($_.split("`t")[3] -eq "running")
{
$MultiInstanceSeparator=" "
If($MultipleInstancesStop -eq "")
{
$MultiInstanceSeparator=""
}
$MultipleInstancesStop=$MultipleInstancesStop+$MultiInstanceSeparator+$_.split("`t")[1]
}
# End of for each
}
#Case for multi-instances stop (from instance status) outside the loop for each, to create the shortcut.
if($MultipleInstancesStop -ne "" -And $MultipleInstancesStop -match " ")
{
Create-instance-shortcut -InstanceName all-running-instances-of-$UserName-stop-only -InstanceID $MultipleInstancesStop -InstanceStatus running
}
}
CreateFolder
Check_token_repiration
GetInstancesList -UserName $UserName -AwsProfile $AwsProfile
Write-Host "## Closing in $SleepTime seconds...##"
Start-Sleep $SleepTime