-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-EC2.ps1
57 lines (54 loc) · 3.16 KB
/
Get-EC2.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
function Get-EC2
{
<#
.Synopsis
Gets EC2 Instances
.Description
Gets EC2 instances from AWS
.Example
Get-EC2
.Link
Add-EC2
.Link
Reset-EC2
.Link
Remove-EC2
#>
[CmdletBinding(DefaultParameterSetName='GetAll')]
param(
# The name of the EC2 instance
[Parameter(Mandatory=$true,
ParameterSetName='ByName',
ValueFromPipeline=$true)]
[string]
$Name,
# The EC2 instance ID
[Parameter(Mandatory=$true,
ParameterSetName='ById',
ValueFromPipelineByPropertyName=$true)]
[string]
$InstanceId
)
process {
if (-not $AwsConnections) {
return
}
$AwsConnections.EC2.DescribeInstances((New-Object Amazon.EC2.Model.DescribeInstancesRequest)).DescribeInstancesResult.Reservation |
Select-Object -ExpandProperty RunningInstance |
Where-Object {
# Skip EC2 instances that are terminated or terminating
if ($_.InstanceState.Name -like "*terminat*") { return }
if ($psboundParameters.Name ) {
if ($_.KeyName -like $name) {
$true
}
} elseif ($psBoundparameters.InstanceId) {
if ($_.InstanceId -eq $InstanceId) {
$true
}
} else {
$true
}
}
}
}