<#
.SYNOPSIS
Script to retrieve Instance Details under AWS Account
This script will also gather last 24hrs Average CPU Value using the function getStats
.FILE NAME
Instance.ps1
#>
clear
Set-ExecutionPolicy Unrestricted
Set-DefaultAWSRegion -Region {Specify the region}
Set-AWSCredentials –AccessKey {Specify the Access Key} –SecretKey {Specify the Secret Key}
Import-Module 'C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1'; Initialize-AWSDefaults
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
try
{
$Environment = "PROD"
$Insts = ((Get-EC2Instance -Region {Specify the region}).RunningInstance | Where-Object -FilterScript {
$_.Tags | Where-Object {
($_.Key -eq "Environment" -and $_.Value -eq $Environment )
}
})
$Instances = $Insts | Where-Object {$_.State.Name.Value -eq "Running"}
$Ins = @{Expression={$_.InstanceId};Label="Instance ID"}, `
@{Expression={$_.Tags | ? { $_.key -eq "Name" } | select -expand Value};Label="Instance Name"},
@{Expression={$_.PrivateIpAddress};Label="Private IP"},
@{Expression={$_.Tags | ? { $_.key -eq "Environment" } | select -expand Value};Label="Environment"},
@{Expression={$_.Architecture};Label="Architecture"},
@{Expression={$_.Tags | ? { $_.key -eq "Component" } | select -expand Value};Label="Component"},
@{Expression={$_.Platform};Label="Platform"},
@{Expression={(Get-EC2InstanceStatus -InstanceId $_.InstanceId).AvailabilityZone};Label="AvailZone"},
@{Expression={getStats($_.InstanceId)};Label="Avg Cpu (last 24hrs)"}
$Instances | Format-Table -AutoSize $Ins
}
catch
{
$originalException = $_.Exception
try
{
"Error: " + $originalException.Message
}
catch
{
"Original Error: " + $originalException.Message
"Logging Error:" + $_.Exception.Message
}
Exit 1
}
Function getStats($InstID)
{
$Stats = Get-CWMetricStatistics -MetricName CPUUtilization -Dimension @{Name = "InstanceId"; Value = $InstID} -StartTime (Get-Date).AddDays(-1) -EndTime (Get-Date) -Namespace "AWS/EC2" -Period 12000 -Statistic Average | Select-Object -ExpandProperty DataPoints
$aveCpu = $Stats | sort TimeStamp | select -ExpandProperty Average | measure -Average | select -ExpandProperty Average
return $aveCpu
}
.SYNOPSIS
Script to retrieve Instance Details under AWS Account
This script will also gather last 24hrs Average CPU Value using the function getStats
.FILE NAME
Instance.ps1
#>
clear
Set-ExecutionPolicy Unrestricted
Set-DefaultAWSRegion -Region {Specify the region}
Set-AWSCredentials –AccessKey {Specify the Access Key} –SecretKey {Specify the Secret Key}
Import-Module 'C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1'; Initialize-AWSDefaults
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
try
{
$Environment = "PROD"
$Insts = ((Get-EC2Instance -Region {Specify the region}).RunningInstance | Where-Object -FilterScript {
$_.Tags | Where-Object {
($_.Key -eq "Environment" -and $_.Value -eq $Environment )
}
})
$Instances = $Insts | Where-Object {$_.State.Name.Value -eq "Running"}
$Ins = @{Expression={$_.InstanceId};Label="Instance ID"}, `
@{Expression={$_.Tags | ? { $_.key -eq "Name" } | select -expand Value};Label="Instance Name"},
@{Expression={$_.PrivateIpAddress};Label="Private IP"},
@{Expression={$_.Tags | ? { $_.key -eq "Environment" } | select -expand Value};Label="Environment"},
@{Expression={$_.Architecture};Label="Architecture"},
@{Expression={$_.Tags | ? { $_.key -eq "Component" } | select -expand Value};Label="Component"},
@{Expression={$_.Platform};Label="Platform"},
@{Expression={(Get-EC2InstanceStatus -InstanceId $_.InstanceId).AvailabilityZone};Label="AvailZone"},
@{Expression={getStats($_.InstanceId)};Label="Avg Cpu (last 24hrs)"}
$Instances | Format-Table -AutoSize $Ins
}
catch
{
$originalException = $_.Exception
try
{
"Error: " + $originalException.Message
}
catch
{
"Original Error: " + $originalException.Message
"Logging Error:" + $_.Exception.Message
}
Exit 1
}
Function getStats($InstID)
{
$Stats = Get-CWMetricStatistics -MetricName CPUUtilization -Dimension @{Name = "InstanceId"; Value = $InstID} -StartTime (Get-Date).AddDays(-1) -EndTime (Get-Date) -Namespace "AWS/EC2" -Period 12000 -Statistic Average | Select-Object -ExpandProperty DataPoints
$aveCpu = $Stats | sort TimeStamp | select -ExpandProperty Average | measure -Average | select -ExpandProperty Average
return $aveCpu
}
No comments:
Post a Comment