Advertisement
Guest User

Eaglesoft Schedule Stop and Start

a guest
Aug 17th, 2017
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Original Script located at:
  2. # http://blogs.msdn.com/b/virtual_pc_guy/archive/2010/09/23/a-self-elevating-powershell-script.aspx
  3.  
  4. # Get the ID and security principal of the current user account
  5. $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
  6. $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
  7.  
  8. # Get the security principal for the Administrator role
  9. $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
  10.  
  11. # Check to see if we are currently running "as Administrator"
  12. if ($myWindowsPrincipal.IsInRole($adminRole))
  13.  
  14.    {
  15.    # We are running "as Administrator" - so change the title and background color to indicate this
  16.    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
  17.    $Host.UI.RawUI.BackgroundColor = "DarkBlue"
  18.    clear-host
  19.  
  20.    }
  21. else
  22.    {
  23.    # We are not running "as Administrator" - so relaunch as administrator
  24.  
  25.    # Create a new process object that starts PowerShell
  26.    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
  27.  
  28.    # Specify the current script path and name as a parameter
  29.    $newProcess.Arguments = $myInvocation.MyCommand.Definition;
  30.  
  31.    # Indicate that the process should be elevated
  32.    $newProcess.Verb = "runas";
  33.  
  34.    # Start the new process
  35.    [System.Diagnostics.Process]::Start($newProcess);
  36.  
  37.    # Exit from the current, unelevated, process
  38.    exit
  39.  
  40.    }
  41.  
  42. $msg = "Enter the username and password that will run the scheduled  task";
  43. $credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
  44. $username = $credential.UserName
  45. $password = $credential.GetNetworkCredential().Password
  46.  
  47. #####  Create Patterson folder in root of Task Scheduler  #########
  48.  
  49. Function New-ScheduledTaskFolder
  50.  
  51.     {
  52.  
  53.      $taskpath = "Patterson"
  54.  
  55.      $ErrorActionPreference = "stop"
  56.  
  57.      $scheduleObject = New-Object -ComObject schedule.service
  58.  
  59.      $scheduleObject.connect()
  60.  
  61.      $rootFolder = $scheduleObject.GetFolder("\")
  62.  
  63.         Try {$null = $scheduleObject.GetFolder($taskpath)}
  64.  
  65.         Catch { $null = $rootFolder.CreateFolder($taskpath) }
  66.  
  67.         Finally { $ErrorActionPreference = "continue" } }
  68. New-ScheduledTaskFolder
  69.  
  70.  
  71.  #### Stop Patterson Daily at 9 pm to allow for full database backups
  72.  
  73.  $taskname = "Stop Patterson Database"
  74.  $Taskpath = "Patterson"
  75.  
  76.  $action = New-ScheduledTaskAction -Execute "E:\Eaglesoft\Shared Files\PattersonServerStatus.exe" -argument -stop
  77.  $trigger =  New-ScheduledTaskTrigger -Daily -At 9pm
  78.  $username = $cred.UserName
  79.  $password = ($cred.GetNetworkCredential()).Password
  80.  
  81.  
  82.  Register-ScheduledTask -Action $action -Trigger $trigger -Taskname $taskname  -Description "Stop Patterson Database for Backups" -TaskPath $taskpath -RunLevel Highest
  83.  
  84.  
  85.  ######  Start Patterson Database at 6 am daily.
  86.  
  87.  
  88.  $taskname = "Start Patterson Database"
  89.  $taskpath = "Patterson"
  90.  
  91.  $action = New-ScheduledTaskAction -Execute "E:\Eaglesoft\Shared Files\PattersonServerStatus.exe" -argument -start
  92.  $trigger =  New-ScheduledTaskTrigger -Daily -At 6am
  93.  $username = $cred.UserName
  94.  $password = ($cred.GetNetworkCredential()).Password
  95.  
  96.  Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname -Description "Start Patterson Database" -TaskPath $taskpath -RunLevel Highest
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement