Jump to content
Sign in to follow this  
support

Reset Scheduled Task Password (by Click Studios)

Recommended Posts

Script Name

Reset Scheduled Task Password

 

Description

Update the password for a Scheduled Task

 

System Requirements

  • PowerShell 2.0 or above
  • PowerShell Remoting enabled
  • Firewall to allow access

 

Successfully Tested Against

  • Windows Server 2008 & R2
  • Windows Server 2012 & R2
  • Windows 7 Desktop
  • Windows 8 & 8.1 Desktop

 

Failed Testing Against

  • None reported 

 
PowerShell Script

<#
.SYNOPSIS
Connect to a Windows host using the supplied Privileged Account Credentials, and change the password for a Scheduled Task's identity.
.NOTES
Requires PowerShell Remoting to be enabled
#>
function Set-ScheduledTaskPassword
{
	[CmdletBinding()]
	param (
		[String]$HostName,
		[String]$TaskName,
		[String]$UserName,
		[String]$NewPassword,
		[String]$PrivilegedAccountUserName,
		[String]$PrivilegedAccountPassword
	)
		
	$scriptBlock = {
		param ($TaskName, $UserName, $NewPassword)
		schtasks.exe /change /RU `"$UserName`" /RP `"$NewPassword`" /TN `"$TaskName`"
	}
	
	try
	{
		$pass = ConvertTo-SecureString $PrivilegedAccountPassword -AsPlainText -Force
		$cred = new-object System.Management.Automation.PSCredential($PrivilegedAccountUserName, $pass)
		
		#Execute the command and put the output in an array.
		$resultsarray = Invoke-Command -ComputerName $HostName -Authentication 'Default' -Credential $cred -ScriptBlock $scriptBlock -ArgumentList $TaskName, $UserName, $NewPassword 2>&1 #Using 2>&1 to ensure STDERR is piped to STDOUT
		
		#create a single string by joining together the array
		$resultsarray = [string]::join("`r`n", $resultsarray)
		$resultsarray = $resultsarray.Substring(7) #Strip Error: from the results (this includes a space after the :
		$resultsarray = $resultsarray.replace("`n", "").replace("`r", "") #Remove line feeds
	
		switch -wildcard ($resultsarray.ToString().ToLower())
		{
			"*The parameters of scheduled task ""$TaskName"" have been changed*" { Write-Output "Success"; break }
			"*WinRM cannot complete the operation*" { Write-Output "Failed to reset the password for the password for Scheduled Task '$TaskName' on Host '$HostName' as it appears the Host is not online, or PowerShell Remoting is not enabled."; break }
			"*WinRM cannot process the request*" { Write-Output "Failed to reset the password for the password for Scheduled Task '$TaskName' on Host '$HostName' as it appears the Host is not online, or PowerShell Remoting is not enabled."; break }
			"*WS-Management service running*" { Write-Output "Failed to reset the password for the password for Scheduled Task '$TaskName' on Host '$HostName' as it appears the Host is not online, or PowerShell Remoting is not enabled."; break }
			"*user name or password is incorrect*" { Write-Output "Failed to reset the password for the password for Scheduled Task '$TaskName' on Host '$HostName' as the Privileged Account password appears to be incorrect, or the account is currently locked."; break }
			#Add other wildcard matches here as required
			default { Write-Output "Failed to reset the password for the Scheduled Task '$TaskName' on Host '$HostName'. Error = $resultsarray" }
		}		
	}
	catch
	{
		switch -wildcard ($error[0].Exception.ToString().ToLower())
		{
			"*cannot bind argument to parameter*" { Write-Output "Failed to reset the password for the password for Scheduled Task '$TaskName' on Host '$HostName' as it appears you may not have associated a Privileged Account Credential with the Password Reset script."; break }
			#Add other wildcard matches here as required
			default { Write-Output "Failed to reset the password for Scheduled Task '$TaskName' on Host '$HostName'. Error = " $error[0].Exception }
		}
	}
}

#Make a call to the Set-ScheduledTaskPassword function
Set-ScheduledTaskPassword -HostName '[HostName]' -TaskName '[ResourceName]' -UserName '[UserName]' -NewPassword '[NewPassword]' -PrivilegedAccountUserName '[PrivilegedAccountUserName]' -PrivilegedAccountPassword '[PrivilegedAccountPassword]'

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×