-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-AwsEc2InstanceInfobyTag.ps1
More file actions
105 lines (97 loc) · 4.46 KB
/
Copy pathGet-AwsEc2InstanceInfobyTag.ps1
File metadata and controls
105 lines (97 loc) · 4.46 KB
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
#Requires -Modules AwsPowerShell
function Get-AwsEc2InstanceInfoByTag {
<#
.SYNOPSIS
Gather AWS EC2 Instance detailed information based on a given tag.
This function assumes that your instance is already have a tag called "hostname" with a value.
.DESCRIPTION
Based on a given value for the hostname tag in your EC2 instance. This function will create
a small table with useful information about your instance. It helps to visualize your instance
details at a glance.
.EXAMPLE
Get-AwsEc2InstanceInfoByTag -HostnameTag myhost01 -KeyPairPath C:\mykeypairs\aws-keypair.pem -ProfileName "myawsprofile"
.NOTES
- Make sure that the "hostname" tag is created and has a value in your instance.
- Make sure you have the AWSPowerShell module is installed in your system and you have a functional IAM user setup.
- https://aws.amazon.com/powershell/
#>
[CmdletBinding(DefaultParameterSetName = 'HostnameTag',
SupportsShouldProcess = $false,
PositionalBinding = $false,
HelpUri = 'http://www.microsoft.com/',
ConfirmImpact = 'Medium')]
[Alias()]
[OutputType([String])]
Param (
# The tag value of your hostname tag field in Amazon.
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'HostnameTag')]
[ValidateNotNullOrEmpty()]
[Alias("h")]
[string[]]$HostnameTag,
# Path to your keypair file. This was created during your initial instance setup for security.
[Parameter(Mandatory = $true,
Position = 1,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false)]
[ValidateScript( { Test-Path $_ })]
[Alias("k")]
[string]$KeyPairPath,
# Profilename of your AWS account. This must be setup before using this cmdlet with Set-AWSCredentials cmdlet.
[Parameter(Mandatory = $true,
Position = 2,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false)]
[ValidateNotNullOrEmpty()]
[Alias("p")]
[string]$ProfileName
)
begin {
$saveErrActPref = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"
#create the main table that will be converted to custom object later. This is the master table that contains all info.
$mainTable = @()
}
process {
foreach ($tag in $hostnameTag) {
try {
$instanceTag = Get-EC2Tag -Filter @{Name = "resource-type"; Value = "instance" }, @{Name = "tag:hostname"; Value = $tag } -ProfileName $ProfileName
$instance = (Get-Ec2Instance -InstanceId $instanceTag.ResourceId -ProfileName $ProfileName).Instances
$instanceState = ((Get-Ec2Instance -InstanceId $instanceTag.ResourceId -ProfileName $ProfileName).Instances | Select-Object State -ExpandProperty State).Name
$instancePass = Get-EC2PasswordData -InstanceID $instanceTag.ResourceId -PemFile $KeyPairPath -ProfileName $ProfileName
}
catch {
$errMessage = $_.Exception.Message
Write-Warning "An error has occured gathering instance info: $errMessage"
}
finally {
foreach ($i in $instance) {
# Filling in the earlier created table with items.
$mainTable += [PSCustomObject] @{
HostnameTag = $instanceTag.Value
InstanceID = $i.InstanceID
InstanceState = $InstanceState
InstanceType = $i.InstanceType
Platform = $i.Platform
HostPassword = $instancePass
PrivateIP = $i.PrivateIpAddress
PublicIP = $i.PublicIpAddress
SubnetID = $i.SubnetID
VpcID = $i.VpcID
ImageID = $i.ImageID
}
}
}
}
$mainTable
}
end {
$ErrorActionPreference = $saveErrActPref
}
}