Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit the credentials for custom scheduled jobs using powershell script

I have multiple scheduled jobs running in server. All i want is to change the credentials for all scheduled jobs using powershell.

The below is used to list the scheduled jobs in the machine. Now how can i change the credentials for all these jobs. How to use the Set-ScheduledJob command ?

# PowerShell script to get scheduled tasks from local computer
$schedule = New-Object -ComObject Schedule.Service
$schedule.Connect()
$tasks = $schedule.GetFolder(".").GetTasks(0)
$tasks | Format-Table Name
like image 579
Ikruzzz Avatar asked Nov 26 '15 07:11

Ikruzzz


1 Answers

Thankfully, PowerShell now has options for doing this with less hassle than having a separate script.

$NewTaskCreds = Get-Credential
Get-ScheduledTask | Set-ScheduledTask -User $NewTaskCreds.UserName -Password $NewTaskCreds.GetNetworkCredentials().Password

Of course, that will update EVERY scheduled task, so if you're just updating the password for a specific user, I suggest this:

$NewTaskCreds = Get-Credential
Get-ScheduledTask | Where-Object { $_.Principal.UserId -eq $NewTaskCreds.UserName } | Set-ScheduledTask -User $NewTaskCreds.UserName -Password $NewTaskCreds.GetNetworkCredentials().Password
like image 200
baumgart Avatar answered Oct 12 '22 09:10

baumgart