# Produce output from Scripting policies

> Capture JSON output from OneFuse Scripting policies

---

Site [llms.txt](/llms.txt)

---

A managed object for OneFuse Scripting Policy can include "output" under the section "provisioningDetails" or "deprovisioningDetails".

## Prerequisites

The script being executed must produce valid JSON strings to STDOUT. This output is then captured by the Scripting Module and used to populate the "output" fields.

## How it works

Each JSON string written towards STDOUT will result in an escaped JSON string in the array called "output" under "provisioningDetails" or "deprovisioningDetails". The structure and key/value names of the JSON string depend on your requirement. The Scripting Module requires that the string must be a valid JSON string. This functionality is valid for both BASH scripts and PowerShell scripts.

## Bash Linux scripts

You can produce STDOUT easily by running a simple "echo" command in the script.

Simple BASH script sample:

```bash
echo '{"myfirstkey":"myfirstvalue"}'
echo '{"mysecondkey":"mysecondvalue"}'
```

### Result: managed object {#result-managed-object-bash}

```json
{
    "_links": {
        "self": {
            "href": "/api/v3/onefuse/scriptingDeployments/11/",
            "title": "Scripting Deployment id 11"
        },
        "workspace": {
            "href": "/api/v3/onefuse/workspaces/2/",
            "title": "Default"
        },
        "policy": {
            "href": "/api/v3/onefuse/scriptingPolicies/1/",
            "title": "scripting_basic"
        },
        "jobMetadata": {
            "href": "/api/v3/onefuse/jobMetadata/143/",
            "title": "Job Metadata Record id 143"
        }
    },
    "id": 11,
    "hostname": "10.30.63.2",
    "provisioningDetails": {
        "status": "successful",
        "output": [
            "{\"myfirstkey\":\"myfirstvalue\"}",
            "{\"mysecondkey\":\"mysecondvalue\"}"
        ]
    },
    "deprovisioningDetails": {},
    "archived": false
}
```

## PowerShell Windows script

For PowerShell scripts, you can produce STDOUT easily by running a "Write-Host" command in the script.

Simple PowerShell script sample:

```powershell
Write-Host '{"mykey": "myvalue"}'
```

### Result: managed object {#result-managed-object-powershell}

```json
{
    "_links": {
        "self": {
            "href": "/api/v3/onefuse/scriptingDeployments/13/",
            "title": "Scripting Deployment id 13"
        },
        "workspace": {
            "href": "/api/v3/onefuse/workspaces/2/",
            "title": "Default"
        },
        "policy": {
            "href": "/api/v3/onefuse/scriptingPolicies/2/",
            "title": "scripting_windows2019"
        },
        "jobMetadata": {
            "href": "/api/v3/onefuse/jobMetadata/155/",
            "title": "Job Metadata Record id 155"
        }
    },
    "id": 13,
    "hostname": "mads01.sovlabs.net",
    "provisioningDetails": {
        "status": "successful",
        "output": [
            "{\"mykey\": \"myvalue\"}"
        ]
    },
    "deprovisioningDetails": {},
    "archived": false
}
```

### Convert objects to JSON

As PowerShell works on Objects, you can take complex object structures and convert them to JSON. The following script shows how the "sshd" service is collected and converted to JSON and then written via "Write-Host" to STDOUT.

### Advanced PowerShell scripting sample

```powershell
Get-Service sshd | ConvertTo-Json | Write-Host
```

### Result: managed object {#result-managed-object-advanced-powershell}

```text
{
    "_links": {
        "self": {
            "href": "/api/v3/onefuse/scriptingDeployments/12/",
            "title": "Scripting Deployment id 12"
        },
        "workspace": {
            "href": "/api/v3/onefuse/workspaces/2/",
            "title": "Default"
        },
        "policy": {
            "href": "/api/v3/onefuse/scriptingPolicies/2/",
            "title": "scripting_windows2019"
        },
        "jobMetadata": {
            "href": "/api/v3/onefuse/jobMetadata/154/",
            "title": "Job Metadata Record id 154"
        }
    },
    "id": 12,
    "hostname": "mads01.sovlabs.net",
    "provisioningDetails": {
        "status": "successful",
        "output": [
            "{
    \"CanPauseAndContinue\":  false,
    \"CanShutdown\":  false,
    \"CanStop\":  true,
    \"DisplayName\":  \"OpenSSH SSH Server\",
    \"DependentServices\":  [

                          ],
    \"MachineName\":  \".\",
    \"ServiceName\":  \"sshd\",
    \"ServicesDependedOn\":  [

                           ],
    \"ServiceHandle\":  {
                          \"IsInvalid\":  false,
                          \"IsClosed\":  false
                      },
    \"Status\":  4,
    \"ServiceType\":  16,
    \"StartType\":  2,
    \"Site\":  null,
    \"Container\":  null,
    \"Name\":  \"sshd\",
    \"RequiredServices\":  [

                         ]
}"
        ]
    },
    "deprovisioningDetails": {},
    "archived": false
}
```
