Advertisement
Guest User

Untitled

a guest
Jun 18th, 2016
177
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.     # Tag values: PowerOn,Mon:5,Tue:5,Wed:6,Thu:6,Fri:6,Sat:6,Sun:6;PowerOff,Mon:18,Tue:18,Wed:22,Thu:18,Fri:18,Sat:19,Sun:19;Notify,user_example@example.com,user_example2@example.com
  5.     #
  6.     # Requires AWS plugin: https://aws.amazon.com/powershell/
  7.     # Set-AWSCredentials -AccessKey <access key> -SecretKey <secret key> -Storeas <profile name>
  8.     # Full instructions found http://i-script-stuff.electric-horizons.com/
  9.     #
  10.     #
  11.  
  12.  
  13.     #profiles to check
  14.     $profile_list = ("Example_1")
  15.  
  16.  
  17.  
  18.     $from = "helpdesk@example.com"
  19.     #SMTP server to use
  20.     $smtp = "mail.example.com"
  21.     $smtp_port = "25"
  22.  
  23.     #Just a quick check for powershell 3.0 and older changes the method of plugin loading.
  24.     if($PSVersionTable.PSVersion.Major -ge 4) {
  25.     Import-Module AWSPowerShell
  26.     } else {
  27.     Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
  28.     }
  29.  
  30.     #converts AWS tag into an object and notes if an action should happen or not.
  31.     function action_object($power_options) {
  32.     $power_procedure = $power_options -split(";")
  33.     $Notify = $Null
  34.     $PowerOn = $False
  35.     $PowerOff = $False
  36.        
  37.         foreach($procedure in $power_procedure) {
  38.         $action_queue = $procedure -split ","
  39.             switch -case ($action_queue[0]) {
  40.                 "PowerOn" {
  41.                     for($i=1;$i -le ($action_queue.count); $i++) {
  42.                         if(time_action $action_queue[$i]) {
  43.                         $PowerOn = $True
  44.                         }
  45.                     }
  46.                 }
  47.            
  48.                 "PowerOff" {
  49.                     for($i=1;$i -le ($action_queue.count); $i++) {
  50.                         if(time_action $action_queue[$i]) {
  51.                         $PowerOff = $True
  52.                         }
  53.                     }
  54.                 }
  55.                
  56.                 "Notify" {
  57.                     for($i=1;$i -le ($action_queue.count); $i++) {
  58.                         if($action_queue[$i]) {
  59.                        
  60.                         $Notify += $action_queue[$i] + ","
  61.                         }
  62.                     }
  63.                 $Notify = $Notify.Substring(0,$Notify.Length-1)
  64.                 }
  65.                
  66.             }
  67.         }
  68.  
  69.     $Object = @{
  70.     PowerOn = $PowerOn
  71.     PowerOff =  $PowerOff
  72.     Notify = $Notify
  73.     }
  74.  
  75.     return $Object 
  76.     }
  77.  
  78.     #Parses the time segment of the AWS tag and returns true or false normally only called in action_object
  79.     function time_action($action_options) {
  80.     $action_timing = $action_options -split ":"
  81.     $day = Get-date -format ddd
  82.     $hour = get-date -format HH
  83.  
  84.         if(($action_timing[0] -like $day) -and ($action_timing[1] -like $hour)){
  85.         return $true
  86.         } elseif (($action_timing[0] -like "weekday") -and ($action_timing[1] -like $hour) -and (($day -notlike "Sat") -and ($day -notlike "Sun"))) {
  87.         return $true
  88.         } elseif (($action_timing[0] -like "allweek") -and ($action_timing[1] -like $hour)) {
  89.         return $true
  90.         } elseif (($action_timing[0] -like "weekend") -and ($action_timing[1] -like $hour) -and (($day -like "Sat") -and ($day -like "Sun"))) {
  91.         return $true
  92.         } else {
  93.         return $false
  94.         }
  95.     }
  96.  
  97.  
  98.     #Actual Engine.
  99.     #Parses through profiles and regions.
  100.     foreach($profile in $profile_list) {
  101.     Set-AWSCredentials -ProfileName $profile
  102.     $region_list = Get-AWSRegion | select -expandproperty Region
  103.  
  104.         foreach($region in $region_list) {
  105.         $Instance_list = Get-EC2Instance -region $region |select -expandproperty instances
  106.  
  107.         $VPC_list = Get-EC2Vpc -Region $region
  108.             foreach ($VPC in $VPC_list) {
  109.                 $Instance_list | Where-Object {$_.VpcId -eq $VPC.VpcId} | foreach-object {
  110.                 $Instance_name = ($_.Tags | Where-Object {$_.Key -eq 'Name'}).Value
  111.                 $power_Action = $NULL
  112.                     if($Power_Options = ($_.Tags | Where-Object {$_.Key -eq 'Power_Options'}).Value) {
  113.                     $power_action = action_object $Power_Options
  114.                     }
  115.                    
  116.                     if(($power_action.PowerOff)) {
  117.                         $subject_power_off = "Scheduled PowerOff of $instance_name has begun"
  118.                         $body_power_off = "The EC2 instance $instance_name has started"
  119.                         if($power_action.Notify) {
  120.                         Stop-EC2Instance -InstanceId $_.InstanceId -Region $region
  121.                         Send-MailMessage -from $from -To $power_action.Notify -Subject $subject_power_off -bodyashtml($body_power_off) -smtpServer "$smtp" -port "$smtp_port"
  122.                         } else {
  123.                         Stop-EC2Instance -InstanceId $_.InstanceId -Region $region
  124.                         }
  125.                     }
  126.                            
  127.                     if(($power_action.PowerON)) {
  128.                     #Email Messages to send
  129.                     $subject_power_on = "Scheduled PowerOn of $instance_name has started"
  130.                     $body_power_on = "The EC2 instance $instance_name has started"
  131.                         if($power_action.Notify) {
  132.                         Start-EC2Instance -InstanceId $_.InstanceId -Region $region
  133.                         Send-MailMessage -from $from -To $power_action.Notify -Subject $subject_power_on -bodyashtml($body_power_on) -smtpServer "$smtp" -port "$smtp_port"
  134.                         } else {
  135.                         Start-EC2Instance -InstanceId $_.InstanceId -Region $region
  136.                         }
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement