Add several Runbook steps using the same contained file in a package but with different variables

I have a file maintenance script that is in Octopus and I want to re-use it in several steps in a few runbooks.
Variables defined in Octopus can be used in the the file but only where the variable names match in the script. So I have so far had to copy the same file for every different variable used. I want to re-use the same script, but how can I pass a different Octopus variables through to used.

I want to preceed the sscript with somethig like this:
Step 1

  • FileLocationVar1 ="\x\files"
  • File Age = 4
    Step 2
  • FileLocationVar2 = “\y\files”
  • File Age = 6

Any thoughts?

Hey @robert.firth,

Thanks for reaching out!

Are you currently running this script using the Run a Script step template in your runbooks?

Best regards,
Mark Butler

Hi Mark

The script is contained in a package, i.e. the repository is published into Octopus and I call the script by path and name. I have altered the script to be a function, and I am now passing parameters to it. It isn’t working yet (no errors). I am unsure if this function can be invoked in this way.

Script parameters

Get-DeleteFiles -$DBA_BackupRetention_Days “#{$DBA_BackupRetention_Days}” -$DBA_BackupRetention_Path “#{$DBA_BackupRetention_Path}” -$DBA_BackupRetention_FileExtension"#{$DBA_BackupRetention_FileExtension}"

The stripped down function is in the file, for now it is supposed to echo out the variable inputs so I now this works:
Function/ file contents

Function Get-DeleteFiles
{
Param (
[Parameter(Mandatory=$True)]
[int]$DBA_BackupRetention_Days,
[Parameter(Mandatory=$True)]
[string]$DBA_BackupRetention_FileExtension,
[Parameter(Mandatory=$True)]
[string]$DBA_BackupRetention_Path
)

$ErrorActionPreference = "Continue"
write-host Started Task at (Get-Date -Format "MM-dd-yyyy_hh-mm-ss") -ForegroundColor Green 
Write-Host "DBA_BackupRetention_Days: $DBA_BackupRetention_Days , DBA_BackupRetention_FileExtension: $DBA_BackupRetention_FileExtension , DBA_BackupRetention_Path: $DBA_BackupRetention_Path "
foreach ($Path in $DBA_BackupRetention_Path) 
{ 
    if (!(Test-Path $Path ))
    {            write-host $Path " does not exist"  -ForegroundColor Yellow
    } 
    else       
    {                 
        Write-Host "Checking" $Path  
    }
}
write-host Completed Task at (Get-Date -Format "MM-dd-yyyy_hh-mm-ss") -ForegroundColor Green

}

Any thouhgts

Hey @robert.firth,

Thanks for sharing this information with me. The good news is you’re on the right path!

Since your script is contained in a package, you’ll want to parameterize the script so that you can pass in the Octopus variables as values.

Based on the information you gave me, it looks like your function inside the script is the thing that is currently parameterized. We do have documentation on Passing Parameters to Scripts that might be helpful.

You’ll want to move the parameters to the root of the file outside of the function. You can then keep your function and pass those parameters to it or reference the parameters in the script without the function. The following is an example where you’d keep the function:

param (
  [Parameter(Mandatory=$True)]
  [int]$Days,
  [Parameter(Mandatory=$True)]
  [string]$FileExtension,
  [Parameter(Mandatory=$True)]
  [string]$Path
)

Function Get-DeletedFiles
{
  param (
    [Parameter(Mandatory=$True)]
    [int]$DBA_BackupRetention_Days,
    [Parameter(Mandatory=$True)]
    [string]$DBA_BackupRetention_FileExtension,
    [Parameter(Mandatory=$True)]
    [string]$DBA_BackupRetention_Path
  )
  # Function body that does the things
}

Get-DeletedFiles -DBA_BackupRetention_Days $Days -DBA_BackupRetention_FileExtension $FileExtension -DBA_BackupRetention_Path $Path

I hope this helps. Please let me know if you have any other questions.

Best regards,
Mark Butler

Many Thanks Mark. That makes senses, and hopefully do the trick.
It did work outside of the function, and that is the method I resided myself to implementing. I’ll confirm back if it worked much later.

Great! Glad to hear that helped. I’ll be here if you need it! :blush: