﻿# Passing parameters to scripts

Octopus can pass parameters to your custom script files for any of the supported scripting languages. This means you can use existing scripts, or write and test your own parameterized scripts that have no knowledge of Octopus, passing Octopus Variables directly to your scripts as parameters. The Octopus scripting API is still available within the context of your script, meaning you can use a mixture of parameters and other Octopus variables and functions.

Consider this example PowerShell script:

```powershell PowerShell script using Octopus Variables
$environment = $OctopusParameters["Octopus.Environment.Name"]
Write-Host "Environment: $environment"
```

You can parameterize this script making it easier to test outside of Octopus:

```powershell PowerShell script using parameters
param (
    [Parameter(Mandatory=$True)]
    [string]$Environment
)
Write-Host "Environment: $Environment"
```

When you call external scripts (sourced from a file inside a package) you can pass parameters to your script. This means you can write "vanilla" scripts that are unaware of Octopus, and test them in your local development environment.

You can define your parameters in the **Script Parameters** field using the format expected by your scripting execution environment (see below for examples).

:::figure
![Script Parameters](/docs/img/deployments/custom-scripts/images/script-parameters.png)
:::

:::div{.hint}
**Delimiting string values**
Don't forget to correctly delimit your parameters correctly for the scripting engine. In the example above we have surrounded the parameter value in double-quotes to handle cases where the Environment Name has spaces: `"#{Octopus.Environment.Name}"`
:::

## Passing parameters to PowerShell scripts \{#passing-parameters-powershell}

You can pass parameters to PowerShell scripts as if you were calling the script yourself from PowerShell, using positional or named parameters.

```bash Script Parameters in Octopus
-Environment "#{Octopus.Environment.Name}" -StoragePath "#{MyApplication.Storage.Path}"
```

```powershell Usage in PowerShell script
Param (
    [Parameter(Mandatory=$True)]
    [string]$Environment,
    [Parameter(Mandatory=$True)]
    [string]$StoragePath
)
 
Write-Host "$Environment storage path: $StoragePath"
```

## Passing parameters to C# scripts \{#passing-parameters-csharp}

You can pass parameters to C# scripts [as described here for the dotnet-script engine](https://github.com/dotnet-script/dotnet-script#passing-arguments-to-scripts).

```bash Script Parameters in Octopus
-- "#{Octopus.Environment.Name}" "#{MyApplication.Storage.Path}"
```

```csharp Usage in C# script
var environment = Args[0]
var storagePath = Args[1]
Console.WriteLine("{0} storage path: {1}", environment, storagePath);
```

## Passing parameters to Bash scripts \{#passing-parameters-bash}

You can pass parameters to Bash scripts [as described in Bash manual.](https://www.gnu.org/software/bash/manual/bash.html#Positional-Parameters)

```powershell Script Parameters in Octopus
"#{Octopus.Environment.Name}" "#{MyApplication.Storage.Path}"
```

```bash Usage in Bash script
environment="$1"
storagePath="$2"
echo "$environment storage path: $storagePath"
```

## Passing parameters to Python3 scripts \{#passing-parameters-python}

You can pass parameters to python scripts [as described by the python documentation.](https://docs.python.org/3/tutorial/interpreter.html#argument-passing)

```python Script Parameters in Octopus
'#{Octopus.Environment.Name}' '#{MyApplication.Storage.Path}'
```

```python Usage in Python3 script
environment=sys.argv[1]
storagePath=sys.argv[2]
print("Parameters {} {}".format(environment, storagePath))
```

:::div{.hint}
**Note:** If your python scripts make use of [argparse](https://docs.python.org/3/library/argparse.html), it's possible you might encounter an error at execution time, as Calamari bootstraps the execution of the python script as part of the deployment or runbook run.
:::
