JorgeSandoval
JorgeSandoval

Reputation: 1185

How to administer scheduledjobs in powershell 3?

According to documentation, get-scheduledjob only returns scheduled jobs where owner is the current user. Other scheduled job commandlets like set-scheduledjob also only work for scheduled jobs where owner is current user. This makes it impossible for non owners to get job status, modify the job (such as setting other credentials), etc.

In a proper IT organization, I'm going to say its crucial for these jobs to allow adminstration from various administrators.

Am I missing some way to administer, review results, etc (other than looking directly at the powershell output files in the owner's appdata)?

To clarify - I'm looking for a method to work with Powershell created and administered ScheduledJobs. If you modify the scheduled task that executes the scheduled job through the UI, schtask or other scheduled task specific tool, you'll get unexpected results. If you change owner/credentials, the scheduled task will fail. You can use UI/schtasks to change schedule without causing any problems. In addition to changing owner, I want to get at the results of get-job in order to monitor the jobs progress.

Upvotes: 0

Views: 1798

Answers (2)

Bill_Stewart
Bill_Stewart

Reputation: 24575

I've written a few PowerShell scripts for managing scheduled tasks (they use the TaskService COM object):

Rename Scheduled Tasks in Windows 7, Windows Server 2008, and Windows Vista

How-To: Use PowerShell to Report on Scheduled Tasks

Updating a Scheduled Task's Credentials

Bill

Upvotes: 0

Local Needs
Local Needs

Reputation: 569

The only way I have ever been able to get this to work using Powershell was by invoking the schtask.exe utility:

Note: "/U" is for local administration and "/RU" is for remote administration, also "/S" is not needed with working locally.

Create by importing an XML previously exported from Task Scheduler

[string]$string = 'schtasks.exe /create /RU yourdomain\username /RP $password /TN Task-Name /XML "D:\Path\To\ExportedXML.xml" /S ServerName'

Delete:

[string]$string = 'schtasks.exe /delete /RU yourdomain\username /P $password /TN Task-Name /S ServerName /F'

Query:

[string]$string = 'schtasks.exe /query /RU yourdomain\username /P $password'

Run Locally:

Invoke-Expression -Command $string

Run Remotely:

Invoke-Command -ScriptBlock {$string}

Upvotes: 1

Related Questions