Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Original Script located at:
- # http://blogs.msdn.com/b/virtual_pc_guy/archive/2010/09/23/a-self-elevating-powershell-script.aspx
- # Get the ID and security principal of the current user account
- $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
- $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
- # Get the security principal for the Administrator role
- $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
- # Check to see if we are currently running "as Administrator"
- if ($myWindowsPrincipal.IsInRole($adminRole))
- {
- # We are running "as Administrator" - so change the title and background color to indicate this
- $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
- $Host.UI.RawUI.BackgroundColor = "DarkBlue"
- clear-host
- }
- else
- {
- # We are not running "as Administrator" - so relaunch as administrator
- # Create a new process object that starts PowerShell
- $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
- # Specify the current script path and name as a parameter
- $newProcess.Arguments = $myInvocation.MyCommand.Definition;
- # Indicate that the process should be elevated
- $newProcess.Verb = "runas";
- # Start the new process
- [System.Diagnostics.Process]::Start($newProcess);
- # Exit from the current, unelevated, process
- exit
- }
- $msg = "Enter the username and password that will run the scheduled task";
- $credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
- $username = $credential.UserName
- $password = $credential.GetNetworkCredential().Password
- ##### Create Patterson folder in root of Task Scheduler #########
- Function New-ScheduledTaskFolder
- {
- $taskpath = "Patterson"
- $ErrorActionPreference = "stop"
- $scheduleObject = New-Object -ComObject schedule.service
- $scheduleObject.connect()
- $rootFolder = $scheduleObject.GetFolder("\")
- Try {$null = $scheduleObject.GetFolder($taskpath)}
- Catch { $null = $rootFolder.CreateFolder($taskpath) }
- Finally { $ErrorActionPreference = "continue" } }
- New-ScheduledTaskFolder
- #### Stop Patterson Daily at 9 pm to allow for full database backups
- $taskname = "Stop Patterson Database"
- $Taskpath = "Patterson"
- $action = New-ScheduledTaskAction -Execute "E:\Eaglesoft\Shared Files\PattersonServerStatus.exe" -argument -stop
- $trigger = New-ScheduledTaskTrigger -Daily -At 9pm
- $username = $cred.UserName
- $password = ($cred.GetNetworkCredential()).Password
- Register-ScheduledTask -Action $action -Trigger $trigger -Taskname $taskname -Description "Stop Patterson Database for Backups" -TaskPath $taskpath -RunLevel Highest
- ###### Start Patterson Database at 6 am daily.
- $taskname = "Start Patterson Database"
- $taskpath = "Patterson"
- $action = New-ScheduledTaskAction -Execute "E:\Eaglesoft\Shared Files\PattersonServerStatus.exe" -argument -start
- $trigger = New-ScheduledTaskTrigger -Daily -At 6am
- $username = $cred.UserName
- $password = ($cred.GetNetworkCredential()).Password
- 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