Advertisement
Guest User

Untitled

a guest
Jun 18th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # AWS PowerOn/Off Schedular
  3. # Tag Name: Power_Options
  4. # Requires AWS plugin: https://aws.amazon.com/powershell/
  5. # Set-AWSCredentials -AccessKey <access key> -SecretKey <secret key> -Storeas <profile name>
  6. # Full instructions found http://i-script-stuff.electric-horizons.com/
  7. #
  8. #
  9.  
  10.  
  11. #profiles to check
  12. $profile_list = ("Example_1")
  13.  
  14.  
  15. #Just a quick check for powershell 3.0 and older changes the method of plugin loading.
  16. if($PSVersionTable.PSVersion.Major -ge 4) {
  17. Import-Module AWSPowerShell
  18. } else {
  19. Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
  20. }
  21.  
  22.  
  23. #Actual Engine.
  24. #Parses through profiles and regions.
  25. foreach($profile in $profile_list) {
  26. Set-AWSCredentials -ProfileName $profile
  27. $region_list = Get-AWSRegion | select -expandproperty Region
  28.  
  29.     foreach($region in $region_list) {
  30.     $Instance_list = Get-EC2Instance -region $region |select -expandproperty instances
  31.  
  32.     $VPC_list = Get-EC2Vpc -Region $region
  33.         foreach ($VPC in $VPC_list) {
  34.             $Instance_list | Where-Object {$_.VpcId -eq $VPC.VpcId} | foreach-object {
  35.             $Instance_name = ($_.Tags | Where-Object {$_.Key -eq 'Name'}).Value
  36.             $power_Action = $NULL
  37.                 if($Power_Options = ($_.Tags | Where-Object {$_.Key -eq 'Power_Options'}).Value) {
  38.                 if($_.State.Name -like "running") {
  39.                 stop-EC2Instance -InstanceId $_.InstanceId -Region $region
  40.                 } else {
  41.                 Start-EC2Instance -InstanceId $_.InstanceId -Region $region
  42.                 }
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement