Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param (
- [ValidateNotNullOrEmpty()]
- [string] $ScriptPath,
- [ValidateNotNullOrEmpty()]
- [string] $TaskName,
- [string] $ScriptArgs
- )
- # Setup error handling.
- Trap
- {
- $_
- Write-Output "Trapped an Error"
- Exit 1
- }
- # Stop On Error
- $ErrorActionPreference = "Stop"
- # Disable progress bar
- $ProgressPreference = "SilentlyContinue"
- # Run script as Vagrant User
- $username = "vagrant"
- $password = "vagrant"
- # Generate a Unique ID for this execution
- $guid = [guid]::NewGuid()
- $logFile = "c:\Windows\Temp\$guid.log"
- $argument = "-NoProfile -ExecutionPolicy unrestricted -Command ""& {""$ScriptPath"" $ScriptArgs} 2>&1 > $logFile"""
- Write-Output "Creating Scheduled Task - $TaskName - for Script $ScriptPath"
- Write-Output "Scheduled Task Command: powershell.exe $argument"
- $a = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $argument
- Register-ScheduledTask -TaskName $TaskName -RunLevel Highest -User $username -Password $password -Action $a | Start-ScheduledTask
- do{
- Start-Sleep -Seconds 30
- $task = Get-ScheduledTask -TaskName $TaskName
- }while($task.State -eq 4)
- Write-Output "Scheduled Task $TaskName - Execution Finished"
- if ( (Test-Path $logFile)) {
- Write-Output "Scheduled Task $TaskName Log Output:"
- Get-Content $logFile
- }
- Exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement