0

I am trying to use PowerShell scheduled job to logoff my machine at specific time.

$trigger = New-JobTrigger -Daily -At (Get-Date).AddMinutes(2)
Register-ScheduledJob -Trigger $Trigger -Name "LogoffComputer5" -ScriptBlock {
    shutdown /l /f
    Get-Process | Out-File -FilePath "c:\process.txt"
} -Credential $credential

I thought it is permission so I used Invoke-Command with credentials, also I used shutdown with full path.

I tried command as following

Start-Process "cmd.exe" "/C shutdown /l /f"

All the commands works from PowerShell but not from scheduled job.

Import-Module PSScheduledJob;
$jobDef = [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore('LogoffComputer5', 'C:\Users\wkassem\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs');
$jobDef.Run()

The above command works if I run from PowerShell.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • [Related](https://stackoverflow.com/a/41635982/1630171). – Ansgar Wiechers Feb 04 '19 at 09:33
  • My issue after debugging it seems that PowerShell scheduled job does not execute any command other than powershell commands (Restart-Computer -Force) worked but (shutdown /f) does not work. Anyone has an idea? – Waleed Kassem Feb 05 '19 at 05:21
  • That would be very weird, since PowerShell can normally run external commands just fine. Try with full path and extension: `& C:\Windows\System32\shutdown.exe /f` or `& C:\Windows\system32\cmd.exe --% /c "C:\Windows\system32\shutdown.exe /f"`. For further help we need more information. – Ansgar Wiechers Feb 05 '19 at 08:26
  • Neither worked for me. the job doesn't execute any external command from outside powershell commands. – Waleed Kassem Feb 08 '19 at 23:13
  • Software execution policies preventing command execution perhaps? Anyway, this doesn't seem to be a programming problem, so I suggest moving the question to [SuperUser](https://superuser.com/). – Ansgar Wiechers Feb 09 '19 at 12:07

0 Answers0