templates/PowerShellAction/script.ps1

<#
    BrazenCloud Action Overview
 
    Online Action Developer Guide
    https://docs.runway.host/runway-documentation/action-developer-guides/overview
 
    1. Load the settings.json
    2. Use a string parameter
    3. Use a number parameter
    4. Use a boolean parameter
    5. Use a password parameter
    6. Creating Results
#>

# 1. Load the settings.json
## The settings.json file is where the parameters are
## passed to the action.
##
## Since the settings file is json encoded, we are passing
## it to ConvertFrom-Json to convert it to an object. This
## allows us to access it later by using the $settings
## variable.
$settings = Get-Content .\settings.json | ConvertFrom-Json

# 2. Use a string parameter
## Accessing the 'Sample String Parameter'
$settings.'Sample String Parameter'

## Outputting, this shows up in the UI under Job -> logs
Write-Host $settings.'Sample String Parameter'

## Using the string as a parameter(s) for an executable
## using a scriptblock and Invoke-Command.
## For example, you could call tracert and assume that the
## 'Sample String Parameter' is arguments for tracert:
## '-d 1.1.1.1'
$scriptBlock = [scriptblock]::Create("tracert $($settings.'Sample String Parameter')")
Invoke-Command -ScriptBlock $scriptBlock

<#
Example output:
    Output of $scriptblock:
        tracert -d 1.1.1.1
    Output of Invoke-Command
 
1 <1 ms <1 ms <1 ms <gateway>
2 17 ms 15 ms 12 ms <ip>
3 11 ms 12 ms 16 ms <ip>
4 14 ms 16 ms 18 ms <ip>
5 14 ms 12 ms 15 ms <ip>
6 18 ms 15 ms 22 ms <ip>
7 21 ms 22 ms 23 ms 1.1.1.1
#>


# 3. Use a number parameter
## Accessing the 'Sample Number Parameter'
$settings.'Sample Number Parameter'

## Comparisons using the 'Sample Number Parameter'
if ($settings.'Sample Number Parameter' -gt 0) {
    # do the things
}

## For loop with 'Sample Number Parameter'
for ($x = 0; $x -lt $settings.'Sample Number Parameter'; $x++) {
    # do the things
}

# 4. Use a boolean parameter
## Accessing the 'Sample Boolean Parameter'
$settings.'Sample Boolean Parameter'

## Comparisons using the 'Sample Boolean Parameter'
if ($settings.'Sample Boolean Parameter' -eq $true) {
    # do the things
}

# 5. Using a Password parameter
## The Password parameter is treated as a string inside
## of a BrazenCloud Action.

## Accessing the 'Sample Password Parameter'
$settings.'Sample Password Parameter'

## Converting the 'Sample Passsword Parameter' to a
## SecureString
$pw = ConvertTo-SecureString $settings.'Sample Password Parameter' -AsPlainText -Force

# 6. Creating results
## Results refer to any file that ends up in .\results
## during the action execution. This can be any file or
## files.

## Get services
Get-Service | ConvertTo-Json | Out-File .\results\services.json

## Get specific service using parameters
Get-Service $settings.'Sample String Parameter' | `
        Select-Object -First $settings.'Sample Number Parameter' | `
            ConvertTo-Json | Out-File .\results\services.json