PowerShell script iterator

Kiel Diller's Avatar

Kiel Diller

02 Feb, 2018 07:09 PM

I created a PS script to create/delete some folders for a SQL backup. Creating worked fine, but when it came time to delete all but the newest 2 folders I used a foreach-object loop. The script was set to run on the tentacle but still in the middle of the iteration, OD used the $_ (current pipeline object) as an object on the SERVER, not the tentacle.

I solved this by going to a foreach loop, but is this normal behavior?

I could tell because the output I used wrote the filename, then on Remove-Item $_ -Recurse
it threw an error saying the directory on C:\Octopus\Work...\[folder in question] was not found when in fact it would have been on the tentacle's D:\ drive.

  1. Support Staff 1 Posted by Michael Compton on 05 Feb, 2018 02:02 AM

    Michael Compton's Avatar

    Hi,

    Thanks for getting in touch.

    When Octopus executes a script on a tentacle, it sends the script (as text) to the tentacle and then invokes PowerShell to run the script. So the behaviour you are describing is quite puzzling.

    One way you could see behaviour like this is if the directory is passed to the script as a variable. All variables come to the tentacle as a text file and those variables are setup before Octopus starts your script.

    One useful debugging technique is to run the script independent of Octopus on the target machine (using the account that the tentacle would use to run the script) and see what the result is.

    Michael

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac