Created
June 19, 2018 23:47
-
-
Save Windos/acf8fb5a211c327ed2c1ed426ca23750 to your computer and use it in GitHub Desktop.
Changing PowerShell.exe to full path for Windows PowerShell scheduled jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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