Issue with updating a scheduled task action with the location of the new version of the executable

I am having an issue with updating an action associated with a scheduled task.
I want to set the actions execute property to the new file location of the executable from the new deployment.

I need all scheduled tasks in the path ‘Example_task_path’ to change the executable that it triggers to the newly deployed version

When running the following PowerShell script from the Custom Deployment Scripts section of a Deploy a package step, the script runs but never completes.

Get-ScheduledTask -TaskPath 'Example_task_path' | ForEach-Object {
    $newActionObjectProperties = @{Execute = $packageDir}
    if ($_.Actions.WorkingDirectory) {$newActionObjectProperties.WorkingDirectory = $_.Actions.WorkingDirectory}
    if ($_.Actions.Arguments) {$newActionObjectProperties.Argument = $_.Actions.Arguments}  

    $newAction = New-ScheduledTaskAction @newActionObjectProperties

    Set-ScheduledTask -User $username -Password $password -TaskPath $_.TaskPath -Action $newAction 
  }

(Using Octopus v2019.5.3)

Hi Lewis,

Thanks for getting in touch and I’m sorry to hear you’re having these issues.

If you run the same script manually on the server, does it run to completion?

Can you send through the raw log from a deployment that had this issue with not running to completion.

Thank you and best regards,
Henrik

Hi Henrik,

When running through Windows PowerShell ISE on the server as administrator the script runs fine all the way through.
When run without administrator rights it hits User Account Control prompt.

Hi Lewis,

OK, is your Tentacle running under an account that is not a local administrator on the server? This would cause the script to prompt for UAC when run and this would of course hang the task in Octopus.

Thanks,
Henrik

Hi Henrik,

I can confirm that the OctopusDeploy Tentacle service is logging on as a user which is a member of the Administrators group on the server.


To be more clear about the situation the following code is giving the error below

ForEach-Object : Cannot validate argument on parameter ‘User’. The argument is null or empty. Provide an argument that


$octopusPath = $OctopusParameters["Octopus.Action.Package.InstallationDirectoryPath"] + "\ProjectName\bin\Release\ExecutableName.exe"
$octopusUsername = $OctopusParameters["SpecificUsername"]
$octopusPassword = $OctopusParameters["SpecificPassword"]

Get-ScheduledTask -TaskPath 'Example_task_path' | ForEach-Object {
    $newActionObjectProperties = @{}
    $newActionObjectProperties.Execute = $octopusPath

    if ($_.Actions.WorkingDirectory) {$newActionObjectProperties.WorkingDirectory = $_.Actions.WorkingDirectory}
    if ($_.Actions.Arguments) {$newActionObjectProperties.Argument = $_.Actions.Arguments}

    $newAction = New-ScheduledTaskAction @newActionObjectProperties
    Set-ScheduledTask -User $username -Password $password -TaskPath $_.TaskPath -Action 
 $newAction
}

Essentially It cannot find any Scheduled Tasks in the given TaskPath if it’s not run as an administrator


As far as I can tell it should be running with administrator rights so I tried to elevate the admin rights to run the script which is where I am now having issues

param (
	[string]$path = $null,
	[string]$username = $null,
	[string]$password = $null
)

If ([string]::IsNullOrEmpty($path))
{ 
  echo "* Respawning PowerShell child process with elevated privileges"
  $octopusPath = $OctopusParameters["Octopus.Action.Package.InstallationDirectoryPath"] + "\ProjectName\bin\Release\ExecutableName.exe"
  $octopusUsername = $OctopusParameters["UsernameVariable"]
  $octopusPassword = $OctopusParameters["PasswordVariable"]
  $pinfo = New-Object System.Diagnostics.ProcessStartInfo
  $pinfo.FileName = "powershell"
  $pinfo.Arguments = "& '" + $myinvocation.mycommand.definition + "' '$octopusPath' '$octopusUsername' '$octopusPassword'"
  $pinfo.Verb = "RunAs"
  $pinfo.RedirectStandardError = $false
  $pinfo.RedirectStandardOutput = $false
  $pinfo.UseShellExecute = $true
  $p = New-Object System.Diagnostics.Process
  $p.StartInfo = $pinfo
  $p.Start() | Out-Null
  $p.WaitForExit()
  echo "* Child process finished"
  type "C:/Temp/transcript.txt"
  Remove-Item "C:/Temp/transcript.txt"
  Exit $p.ExitCode
} Else {
    echo "Child process starting with admin privileges"

    **INSERT CODE BLOCK TO RUN HERE** 
}

Hi Lewis,

Can you send through the raw log from a deployment that had this issue with not running to completion.

Thank you,
Henrik

Hi Henrik,

Here is a copy of the raw log from one of the deployments which has had the issue

This release had to be manually stopped as it ran for 2 hours without completing

ServerTasks-18137.log (2).txt (9.5 KB)

Hi Lewis,

Can you run the below script via Tasks -> Script Console on the Tentacle that you ran that deployment on and see if that returns True or False

([Security.Principal.WindowsPrincipal] `
  [Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

If it returns False, then trying to open an elevated command prompt will cause UAC and that in turn will cause your deployment to hang.

Thank you and best regards,
Henrik

Hi Henrik,

I have run the command and it is returning True.

Thank you,
Lewis

Hi Lewis,

My sincere apologies for the delay in getting back to you, the notification seems to have fallen through the cracks and I did not see it.

Starting a new powershell process with elevated rights won’t work when running a script on the Tentacle.

Looking at the error that you got ForEach-Object : Cannot validate argument on parameter ‘User’. The argument is null or empty. Provide an argument that, it seems to point to Set-ScheduledTask call is the source of the error as it’s complaining about the User parameter.

Could it be that the $OctopusParameters["SpecificUsername"] variable isn’t set correctly?

Thanks,
Henrik

Thank you for your help!

Through various iterations of attempts to find an answer I have just managed to successfully deploy!

One of the issues was with my powershell and was nothing to do with octopus at all!
Set-ScheduledTask requires a -TaskName parameter apparently that was missing from the code above.
I’m still not sure why it wasn’t working previously, as all the code I ran through octopus I had tested direct on the server first and they were functioning correctly.

Oh well, It’s sorted now!

Thank you,
Lewis

Hi Lewis,

Great to hear that you were able to find a solution to the problem at hand!

Cheers,
Henrik

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.