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.