Skip to content

Instantly share code, notes, and snippets.

@Windos
Created June 19, 2018 23:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Windos/acf8fb5a211c327ed2c1ed426ca23750 to your computer and use it in GitHub Desktop.
Save Windos/acf8fb5a211c327ed2c1ed426ca23750 to your computer and use it in GitHub Desktop.
Changing PowerShell.exe to full path for Windows PowerShell scheduled jobs
$Option = New-ScheduledJobOption -RunElevated -RequireNetwork
Register-ScheduledJob -ScriptBlock {Add-Content -Path C:\temp\joblog.txt -Value (Get-Date)} -Name 'Test Job' -ScheduledJobOption $Option -RunNow
$CurrentAction = (Get-ScheduledTask -TaskName 'Test Job').actions
$Params = @{
Id = $CurrentAction.Id
Argument = $CurrentAction.Arguments
Execute = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
}
if ($CurrentAction.WorkingDirectory) {
$Params.Add('WorkingDirectory', $CurrentAction.WorkingDirectory)
}
$NewAction = New-ScheduledTaskAction @Params
# Id doesn't seem to work via the cmdlet, so let's add it:
$NewAction.Id = $CurrentAction.Id
Set-ScheduledTask -TaskName 'Test Job' -TaskPath '\Microsoft\Windows\PowerShell\ScheduledJobs\' -Action $NewAction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment