Guest User

Untitled

a guest
Feb 18th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. # Settings
  2.  
  3. ## Email
  4.  
  5. $email_system = ""
  6.  
  7. $email_contact = ""
  8.  
  9. $email_host = ''
  10.  
  11. $email_username = ''
  12.  
  13. $email_password = ''
  14.  
  15.  
  16.  
  17. ## Database
  18.  
  19. $database_host = ""
  20.  
  21. $database_name = ""
  22.  
  23. $database_user = ""
  24.  
  25. $database_pass = ""
  26.  
  27.  
  28.  
  29. ## Run task settings
  30.  
  31. $run_tasks = 1
  32.  
  33. $run_reboots = 1
  34.  
  35. $run_poweroffs = 1
  36.  
  37. $run_powerups = 1
  38.  
  39.  
  40.  
  41. # Log file directory
  42.  
  43. $file_log_dir = 'C:\automation_logs\'
  44.  
  45.  
  46.  
  47. #
  48.  
  49. #
  50.  
  51. #
  52.  
  53. # Don't touch below here
  54.  
  55. #
  56.  
  57. #
  58.  
  59. #
  60.  
  61.  
  62.  
  63. # Session varibles
  64.  
  65.  
  66.  
  67. $base_log_name = $file_log_dir+$(Get-Date -format 'd-M-y-h-m-s')+'-'
  68.  
  69.  
  70.  
  71. # Functions
  72.  
  73.  
  74.  
  75. ## Core functions
  76.  
  77. function log([string]$message, [string]$type = 'info')
  78.  
  79. {
  80.  
  81. if ((Test-Path -path $file_log_dir) -ne $True){
  82.  
  83. # Make log dir if it dosn't exist
  84.  
  85. write-host 'Making log dir'
  86.  
  87. New-Item $file_log_dir -type directory
  88.  
  89. }
  90.  
  91.  
  92.  
  93. $log_file = $base_log_name+$($type)+'.log'
  94.  
  95. write-host $message
  96.  
  97. add-content $log_file $message
  98.  
  99. }
  100.  
  101.  
  102.  
  103. function error([string]$traceback)
  104.  
  105. {
  106.  
  107. $error = 'Hit exception:'+$traceback
  108.  
  109. log $error 'error'
  110.  
  111. mail 'Exception encountered' $error
  112.  
  113. }
  114.  
  115.  
  116.  
  117. function mail([string]$subject, [string]$message)
  118.  
  119. {
  120.  
  121. #$smtp_handler = new-object Net.Mail.SmtpClient($email_host)
  122.  
  123. #$smtp_handler.Credentials = New-Object System.Net.NetworkCredential($email_username, $email_password)
  124.  
  125. #$smtp_handler.Send($email_system, $email_contact, $subject, $message)
  126.  
  127. }
  128.  
  129.  
  130.  
  131. function marktaskFailed([int]$task_id)
  132.  
  133. {
  134.  
  135. $time = [int][double]::Parse((Get-Date -UFormat %s))
  136.  
  137.  
  138.  
  139. try {
  140. $command = $database_handler.CreateCommand()
  141. $command.CommandText = "update queue set status = 'f', run_time = '$($time)' where id = '$($task_id)'"
  142.  
  143. $command.ExecuteNonQuery()
  144.  
  145. } catch {
  146.  
  147. log "Could not set status = failed for task id $task_id"
  148.  
  149. error $_.Exception.ToString()
  150.  
  151. }
  152.  
  153. }
  154.  
  155.  
  156.  
  157. function marktaskSuccess([int]$task_id)
  158.  
  159. {
  160.  
  161. $time = [int][double]::Parse((Get-Date -UFormat %s))
  162.  
  163.  
  164.  
  165. try {
  166.  
  167. $command = $database_handler.CreateCommand()
  168.  
  169. $command.CommandText = "update queue set status = 'c', run_time = '$($time)' where id = '$($task_id)'"
  170.  
  171. $command.ExecuteNonQuery()
  172.  
  173. } catch {
  174.  
  175. log "Could not set status = complete for task id $task_id"
  176.  
  177. error $_.Exception.ToString()
  178.  
  179. }
  180.  
  181. }
  182.  
  183.  
  184.  
  185. ## Action functions
  186.  
  187. function rebootHyperVInstance($task)
  188.  
  189. {
  190.  
  191. # Un-tested bit but this is right according to the bizzare docs
  192.  
  193. $vm = gwmi -namespace root\virtualization -query "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName = '$($task['machine_id'])'"
  194.  
  195. $result = $vm.InitiateShutdown("$true", "Automated shutdown requested")
  196.  
  197.  
  198.  
  199. if($result.returnvalue -match "0"){
  200.  
  201. $vm = gwmi -namespace root\virtualization -query "SELECT * FROM msvm_computersystem WHERE SystemName = '$($task['machine_id'])'"
  202.  
  203. $result = $vm.requeststatechange(2)
  204.  
  205.  
  206.  
  207. if($result.returnvalue -match "0"){
  208.  
  209. log "Task '$($task['task_id'])' ($($task['type'])) completed with success status"
  210.  
  211. marktaskSuccess($task['task_id'])
  212.  
  213. }else{
  214.  
  215. log "Task '$($task['task_id'])' ($($task['type'])) completed with failed status, could not issue powerup"
  216.  
  217. marktaskFailed($task['task_id'])
  218.  
  219. }
  220.  
  221. }else{
  222.  
  223. log "Task '$($task['task_id'])' ($($task['type'])) completed with failed status, could not shutdown vm"
  224.  
  225. marktaskFailed($task['task_id'])
  226.  
  227. }
  228.  
  229. }
  230.  
  231.  
  232.  
  233. function poweroffHyperVInstance($task)
  234.  
  235. {
  236.  
  237. # Un-tested bit but this is right according to the bizzare docs
  238.  
  239. $vm = gwmi -namespace root\virtualization -query "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName = '$($task['machine_id'])'"
  240.  
  241. $result = $vm.InitiateShutdown("$true", "Automated shutdown requested")
  242.  
  243.  
  244.  
  245. if($result.returnvalue -match "0"){
  246.  
  247. log "Task '$($task['task_id'])' ($($task['type'])) completed with success status"
  248.  
  249. marktaskSuccess($task['task_id'])
  250.  
  251. }else{
  252.  
  253. log "Task '$($task['task_id'])' ($($task['type'])) completed with failed status, could not issue reboot"
  254.  
  255. marktaskFailed($task['task_id'])
  256.  
  257. }
  258.  
  259. }
  260.  
  261.  
  262.  
  263. function poweronHyperVInstance($task)
  264.  
  265. {
  266.  
  267. # Un-tested bit but this is right according to the bizzare docs
  268.  
  269. $vm = gwmi -namespace root\virtualization -query "SELECT * FROM msvm_computersystem WHERE SystemName = '$($task['machine_id'])'"
  270.  
  271. $result = $vm.requeststatechange(2)
  272.  
  273.  
  274.  
  275. if($result.returnvalue -match "0"){
  276.  
  277. log "Task '$($task['task_id'])' ($($task['type'])) completed with success status"
  278.  
  279. marktaskSuccess($task['task_id'])
  280.  
  281. }else{
  282.  
  283. log "Task '$($task['task_id'])' ($($task['type'])) completed with failed status, could not issue powerup"
  284.  
  285. marktaskFailed($task['task_id'])
  286.  
  287. }
  288.  
  289. }
  290.  
  291.  
  292.  
  293.  
  294.  
  295. # Main code
  296.  
  297.  
  298.  
  299. ## Load what we need!
  300.  
  301. try{
  302.  
  303. [void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
  304.  
  305. } catch {
  306.  
  307. log "Could not load mysql.net connector"
  308.  
  309. error $_.Exception.ToString()
  310.  
  311. break
  312.  
  313. }
  314.  
  315.  
  316.  
  317. ## Main loop
  318.  
  319. while (1) {
  320.  
  321. write-host 'Doing run....'
  322.  
  323.  
  324.  
  325. ## Connect to the database
  326.  
  327. try{
  328.  
  329. $database_handler = New-Object MySql.Data.MySqlClient.MySqlConnection
  330.  
  331. $database_handler.ConnectionString = "server=$database_host;uid=$database_user;pwd=$database_pass;database=$database_name;"
  332.  
  333. $database_handler.Open()
  334.  
  335. } catch {
  336.  
  337. log "Could not connect to the database :("
  338.  
  339. error $_.Exception.ToString()
  340.  
  341. break
  342.  
  343. }
  344.  
  345.  
  346.  
  347. write-host 'Connected to database'
  348.  
  349.  
  350.  
  351. try {
  352.  
  353. $command = $database_handler.CreateCommand()
  354.  
  355. $command.CommandText = "select * from `queue` where `status` = 'n'"
  356.  
  357. $data = $command.ExecuteReader()
  358.  
  359. } catch {
  360.  
  361. log "Could not run query against database"
  362.  
  363. error $_.Exception.ToString()
  364.  
  365. continue
  366.  
  367. }
  368.  
  369.  
  370.  
  371. write-host 'Got queue from database'
  372.  
  373.  
  374.  
  375. # Array where we will put our tasks!
  376.  
  377. $tasks_to_run = @()
  378.  
  379.  
  380.  
  381. write-host 'Loading queue'
  382.  
  383.  
  384.  
  385. while ($data.Read()) {
  386.  
  387. # task
  388.  
  389. try {
  390.  
  391. $task = @{}
  392.  
  393. $task['type'] = $data.GetValue(1).ToString()
  394.  
  395. $task['task_id'] = $data.GetValue(0).ToString()
  396.  
  397. $task['machine_id'] = $data.GetValue(3).ToString()
  398.  
  399.  
  400.  
  401. # Add task to list of tasks to run
  402.  
  403. $tasks_to_run += $task
  404.  
  405. write-host "Entered task $($task['task_id']) into queue"
  406.  
  407. } catch {
  408.  
  409. log "Could not add task to queue"
  410.  
  411. error $_.Exception.ToString()
  412.  
  413. continue
  414.  
  415. }
  416.  
  417. }
  418.  
  419. $data.close() # Close the data reader
  420.  
  421. write-host 'Loaded queue'
  422.  
  423.  
  424.  
  425. if($tasks_to_run.length -gt 0){
  426.  
  427. write-host "Tasks to run: $($tasks_to_run.length)!"
  428.  
  429.  
  430.  
  431. # Loop though the list of tasks to run
  432.  
  433. for ( $i = 0 ; $i -lt $tasks_to_run.length; $i++ ){
  434.  
  435. $task = $tasks_to_run[$i]
  436.  
  437.  
  438.  
  439. log "Running task $($task['task_id']) (Action '$($task['type'])' for instance '$($task['machine_id'])')"
  440.  
  441.  
  442. if($run_tasks -eq 1){
  443.  
  444. # The bit that decides what we are going to do
  445.  
  446. switch ($task['type']) {
  447.  
  448. poweron {
  449.  
  450. if($run_powerups -eq 1){
  451.  
  452. &poweronHyperVInstance($task)
  453. }else{
  454. log "Not running task '$($task['task_id'])', powerup tasks are disabled"
  455. marktaskFailed($task['task_id'])
  456. }
  457.  
  458. }
  459.  
  460.  
  461.  
  462. poweroff {
  463. if($run_poweroffs -eq 1){
  464.  
  465. &poweroffHyperVInstance($task)
  466. }else{
  467. log "Not running task '$($task['task_id'])', poweroff tasks are disabled"
  468. marktaskFailed($task['task_id'])
  469. }
  470.  
  471. }
  472.  
  473.  
  474.  
  475. reboot {
  476. if($run_reboots -eq 1){
  477.  
  478. &rebootHyperVInstance($task)
  479. }else{
  480. log "Not running task '$($task['task_id'])', reboot tasks are disabled"
  481. marktaskFailed($task['task_id'])
  482. }
  483.  
  484. }
  485.  
  486.  
  487.  
  488. default { # We were asked to do something we don't understand!
  489.  
  490. log "Tried to run an unknown action: '$($task['type'])' for task '$($task['task_id'])' this will need running manually!"
  491.  
  492. mail "Tried to run an unknown action: '$($task['type'])' for task '$($task['task_id'])' this will need running manually!"
  493.  
  494. marktaskFailed($task['task_id'])
  495.  
  496. }
  497.  
  498. }
  499. }else{
  500. log "Not running task '$($task['task_id'])', tasks are disabled"
  501.  
  502. marktaskFailed($task['task_id'])
  503. }
  504.  
  505. }
  506.  
  507. write-host 'Tasks run'
  508.  
  509. }else{
  510.  
  511. write-host "No tasks to run!"
  512.  
  513. }
  514.  
  515.  
  516.  
  517. write-host 'Run complete....'
  518.  
  519. # Sleep 10 seconds until next run
  520.  
  521. Start-Sleep 10
  522.  
  523. }
Add Comment
Please, Sign In to add comment