CPW-Scheduledtask.ps1

This scripts automates the password change for scheduled task run with a certain user.

#**   
#** Script:   CPW-ScheduledTask.ps1   
#** Version:  1.0   
#** Created:  30/08/2019 08:09AM   
#** Author:   Adriaan Westra   
#** Website:  http://www.westphil.nl  
#**   #** Purpose / Comments:   
#**    Change pasword for an account on multiple scheduled taska on Multiple    
#**    Machines.   
#**   
#** Changelog :   
** 30/12/2015 08:09AM  : Initial version   
#**   
#**
   #** account to check for
   $UserAcct='.\administrator'
   #** New password
   $UserPass= 'SuperSecret' 
   #** Array of computers to change the scheduled tasks on
   $aComputers = @('PC1','Pc2','PC3')
   #** Loop throug the collection of Computers
   ForEach ( $sComputer in $aComputers) {
      #** Handle unexpected errors
      Try { 
          #** Test if computer is responding over the network 
          If (test-connection -computername $sComputer -count 1 -quiet) {
             #** Create com object for Schedule Service
             $ScheduleObject=new-object -ComObject Schedule.Service
             #** Connect to the task scheduler on the remote PC
             $ScheduleObject.Connect($sComputer)
             #** Connect to the root folder of the task scheduler
             $taskFolder = $ScheduleObject.GetFolder('\')
             #** Display current PC
             Write-host "Processing Scheduled tasks on : " $sComputer -ForegroundColor Green
             #** loop through the tasks in the task scheduler
             foreach ($task in $taskFolder.GetTasks(0)) {
               #** Get the task properties in XML format
               $xml = [xml] $task.xml
               #** Check if the user account for the scheduled task is the account we're looking for.
               If ($xml.ChildNodes[1].Principals.Principal.UserId -eq $UserAcct ) {
                #** Display the task name           
                Write-host "Changing password on : " $task.name
                #** Change the password for the task
                $task = $taskFolder.RegisterTaskDefinition($task.Name, $task.Definition, 6, $UserAcct, $UserPass, 1)
               }
             }
          } Else {
            #** computer is not responding over the network
            Write-host $sComputer "is not responding" -ForegroundColor Red
          }
      }
      #** Handle unexpected errors and display the error 
      Catch {
          $ErrorMessage = $_.Exception.Message
          $innerException = $Error
          Write-host (Get-Date -UFormat "%Y-%m-%d %T") " ===> Error : " $ErrorMessage " | " $innerException -ForegroundColor Red
      }
   }
   exit