Delete task from task scheduler based on userid powershell - powershell

I need to find tasks which runs using SYSTEM and delete rest of the tasks running using any other account
I was trying to find the job first, then unregister, but not getting any filters to do that.
Get-ScheduledTask -TaskName "SPOC_Inventory"
Need help in this

Try this (without -whatif):
PS C:\> Get-ScheduledTask SPOC_Inventory | ? {($_.Principal.UserId -ne "SYSTEM")}| Unregister-ScheduledTask -WhatIf
Enjoy
tom

Related

how to set trigger "on connection to user session" in task scheduler from powershell?

I want to schedule a task in task scheduler from Powershell.
I want to set 2 trigger "On connection to user session" with local and remote both.
And also,I want to uncheck some settings and conditions in case if they are marked in task scheduler.
Can someone please tell me how to set above trigger and how to unchheck settings and conditions?
Thank You
The only way i have found to accomplish that is by creating a new task using the UI. Then export the task to XML.
Then use powershell command for creating a new task by importing the XML.
Register-ScheduledTask -Xml (Get-Content "your-exported.xml" | Out-String) -TaskName "some task name"
you can also add credentials to the command if needed, hence
Register-ScheduledTask -Xml (Get-Content "your-exported.xml" | Out-String) -TaskName "some task name" -User "domain\user" -Password "pa55w0rd"

scheduled tasks don't show up in Get-ScheduledTask result

I have defined some scheduled task using Windows Task Scheduler GUI under "" [default] path but when i run Get-ScheduledTask in powershell, it does not return them. why?
I have tried with Get-ScheduledTask -TaskName "MyTaskName" with one of my task name but it comes up with "No MSFT_ScheduledTask objects found with property 'TaskName' equal to 'MyTaskName'"
Actually I have tried https://library.octopusdeploy.com/step-template/actiontemplate-windows-scheduled-task-disable but it doesn't work so I have tried running script directly.
UPDATE
I have found the following script to get task list on http://www.fixitscripts.com/problems/getting-a-list-of-scheduled-tasks:
# PowerShell script to get scheduled tasks from local computer
$schedule = new-object -com("Schedule.Service")
$schedule.connect()
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks | Format-Table Name , LastRunTime # -AutoSize
IF($tasks.count -eq 0) {Write-Host “Schedule is Empty”}
Read-Host
UAC
The result is likely affected by UAC. To see everything try right clicking the PowerShell icon, select Run as Administrator and then run your Get-ScheduledTask command again and see if that changes anything.
Further reading: http://david-homer.blogspot.co.uk/2017/10/not-all-scheduled-tasks-show-up-when.html
Have you tried using a com object? This code works for me:
# FOR A REMOTE MACHINE
$s = 'SERVER_NAME' # update this with server name
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect($s)
# FOR LOCAL MACHINE
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect()
#now we can query the schedules...
cls;$TaskScheduler.GetFolder('\').GetTasks(0) | Select Name, State, Enabled, LastRunTime, LastTaskResult | Out-GridView
This code will retrieve a particular task and enable it:
$task = $TaskScheduler.GetFolder('\').GetTask("TASKNAME")
$task.Enabled = $true
When running Get-ScheduledTask from a user-level prompt, even if the user is an administrator, they will see only the tasks that they can read with user-level permissions. Seeing them in the TaskSchd.Msc window indicates that program is running with different permissions.
Therefore, running a PowerShell prompt as administrator solves the problem.
The same issue occurs when using SchTasks.exe from a command prompt.

Powershell - Manually trigger a scheduled job

I have created a powershell job that I can see when I use the following command:
PS C:\WINDOWS\system32> Get-ScheduledJob -Name KillTicker | Get-JobTrigger
Id Frequency Time DaysOfWeek Enabled
-- --------- ---- ---------- -------
1 AtStartup True
As this is a startup job I really don't fancy restarting to test it out - how to do I manually start this job?
If you run Get-ScheduledJob -id 1 | Get-Member to retrieve all Members of a Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition you will see that the object expose a method called StartJob:
(Get-ScheduledJob -id 1).StartJob()
To retrieve the result, use the Receive-Job cmdlet:
Receive-Job -id 1
I think it is worth it to mention that Start-Job or StartJob() both run the defined job from the current security context. If the job runs as different user or accesses network resources, you might get unexpected results.
To get the same behavior use either (Get-ScheduledJob -Name xxx).RunAsTask() or Start-ScheduledTask -TaskName xxx -TaskPath xxx. The latter provides a -CimSession option and could be better for remote operations.
You can use the Start-Job cmdlet and supply the name of the Scheduled Job as the
-DefinitionName parameter.
Start-Job -DefinitionName *MyJobName*

Powershell get-scheduledjob to list jobs created by other users

Somehow I can't see scheduled jobs created by another user with get-scheduled job (can see them using task scheduler in powershell\scheduledjobs). What is the trick? powershell v3.
I've got some solution for you.
As you notice you can see all Scheduled Job into Task Scheduler. So I Run :
Get-ScheduledTask
and then
Get-ScheduledTask -TaskPath "\Microsoft\Windows\PowerShell\ScheduledJobs\"
So You can see all your PowerShell Jobs. I you really want ot retreive ScheduledJob objects then, you can run :
Get-ScheduledTask -TaskPath "\Microsoft\Windows\PowerShell\ScheduledJobs\" | Select-Object -ExpandProperty Actions | Select-Object -ExpandProperty Arguments | % {$_ -match '^.*= (.*);.*$'; Invoke-Expression $Matches[1]}
I just put some glue to retreive the jobs from the launch of each task.

Powershell V3 new ScheduleJob and ScheduleTask cmdlets

I'm currently learning all about the new cmd-lets in powershell version 3 and attempting to use some of them.
I'm in need to schedule different things and I'm trying to learn the new new-scheduledtask and new-scheduledjob cmdlets but I'm having a little difficulty.
I was wondering if anyone can help me discover what would be the simplest way to schedule a service restart (apache in this case), via a script? the script would schedule a restart when a certain condition is met ( basically when I need to restart to add new apache vhosts).
Any ideas? Currently I'm leaning towards creating a scriptblock that will do it and then registering a job and then a job trigger for the time of the day I want to restart which would be that night that the script schedules the restart.
Thanks!
<#So Here's an exmaple i thought to bring up for you it does a daily restart of BITS Service at 3am,i guess you can modify the script with your apache service example#>
<#Here Im Creating a Trigger for the Job and im setting it to start at 3am daily#>
$trigger = New-JobTrigger -Daily -At 3am
<# Below Im Registering the ScheduledJob with a Name of and a trigger of $trigger which is at daily 3am, Also im placing the script to be executed inside of a script block#>
Register-ScheduledJob -Name BitsServiceRestart -Trigger $trigger -ScriptBlock {
<# Suppose Lets say that you want to schedule a restart of BITS Service#>
Restart-Service -ServiceName BITS
}
<# Once the Job is registered you can get the Job details using Get-ScheduledJob
You can also view task details using TaskScheduler --> Windows --> PowerShell #>
Get-ScheduledJob BitsServiceRestart | fl *
<# You can recieve the output of Scheduled Job using Recieve Job #>
Receive-Job -Name BitsServiceRestart