Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. param (
  2. [ValidateNotNullOrEmpty()]
  3. [string] $ScriptPath,
  4. [ValidateNotNullOrEmpty()]
  5. [string] $TaskName,
  6. [string] $ScriptArgs
  7. )
  8.  
  9. # Setup error handling.
  10. Trap
  11. {
  12. $_
  13. Write-Output "Trapped an Error"
  14. Exit 1
  15. }
  16.  
  17. # Stop On Error
  18. $ErrorActionPreference = "Stop"
  19.  
  20. # Disable progress bar
  21. $ProgressPreference = "SilentlyContinue"
  22.  
  23. # Run script as Vagrant User
  24. $username = "vagrant"
  25. $password = "vagrant"
  26.  
  27. # Generate a Unique ID for this execution
  28. $guid = [guid]::NewGuid()
  29. $logFile = "c:\Windows\Temp\$guid.log"
  30.  
  31. $argument = "-NoProfile -ExecutionPolicy unrestricted -Command ""& {""$ScriptPath"" $ScriptArgs} 2>&1 > $logFile"""
  32.  
  33. Write-Output "Creating Scheduled Task - $TaskName - for Script $ScriptPath"
  34. Write-Output "Scheduled Task Command: powershell.exe $argument"
  35.  
  36. $a = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $argument
  37. Register-ScheduledTask -TaskName $TaskName -RunLevel Highest -User $username -Password $password -Action $a | Start-ScheduledTask
  38. do{
  39. Start-Sleep -Seconds 30
  40. $task = Get-ScheduledTask -TaskName $TaskName
  41. }while($task.State -eq 4)
  42.  
  43. Write-Output "Scheduled Task $TaskName - Execution Finished"
  44. if ( (Test-Path $logFile)) {
  45. Write-Output "Scheduled Task $TaskName Log Output:"
  46. Get-Content $logFile
  47. }
  48.  
  49. Exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement