# Scripting for DeployR Server

### Authenicating to DeployR Server for Scripting

Depending on how you have configured security for your DeployR server, you might need to leverage the Connect-DeployR function to be able to authenticate before running commands.   In my example, I'll be leveraging the passcode method of autentication.

```powershell
Import-Module 'C:\Program Files\2Pint Software\DeployR\Client\PSModules\DeployR.Utility'
Connect-DeployR -Passcode PASSWORD
```

Once you've authenticated, you'll be able to retrieve information and content using PowerShell.  One of the primary reasons for this would be to do backups of your items, for disaster recovery, or for importing into another DeployR instance, like prompting from test to prod.

You can create extensive backup scripts, filtering for specific items, or generic to backup everything.  Getting data and exporting can be done with a couple of functions.

### Exporting Content Items

Get-DeployRContentItem, when used without parameters, will pull back all content items, which you can then pipe into Export-DeployRContentItem

```powershell
Get-DeployRContentItem | Export-DeployRContentItem -DestinationFolder "D:\Backups\Temp\"
```

<figure><img src="/files/bZBp0QKTRxrDNfQpXsoQ" alt=""><figcaption></figcaption></figure>

Here is a sample code block where we filter out built in packages (-notlike '0000000-\*'), and filter on purpose of "Other".&#x20;

```powershell
#Backup DeployR content items
Write-Host "Backing up DeployR content items..." -ForegroundColor Yellow
$ContentItems = Get-DeployRContentItem | Where-Object {$_.id -notlike '00000000-*'} | Where-Object {$_.contentItemPurpose -match "Other"} 
$ContentItems | ForEach-Object {
    write-host "Backing up content item: $($_.name) | $($_.id)" -ForegroundColor Cyan
    Export-DeployRContentItem -Id $_.id -DestinationFolder "D:\Backups\ContentItems\$($_.name)-$($_.id)"
}
```

### Exporting Step Definitions

If you're creating step definitions for your organization, these too would be a great thing to backup.  To get the step definition data, you'll use the Get-DeployRMetaData function with the -Type StepDefinition parameter.   You can then Export that with the Export-DeployRStepDefinition function&#x20;

```powershell
Get-DeployRMetadata -Type StepDefinition
```

Here is a sample code block used to backup all Step definitions that aren't built in by default (-notlike '0000\*')

```powershell
#Backup DeployR step definitions
Write-Host "Backing up DeployR step definitions..." -ForegroundColor Yellow
$Steps = (Get-DeployRMetadata -Type StepDefinition | Where-Object {$_.id -notlike '0000*'})
$Steps | ForEach-Object {
    write-host "Backing up step definition: $($_.name) | $($_.id)" -ForegroundColor Cyan
    Export-DeployRStepDefinition -Id $_.id -DestinationFolder "D:\Backups\StepDefinitions\$($_.name)-$($_.id)"
}
```

### Exporting Task Sequences

To export a task sequence, use Get-DeployRMetaData with the -Type TaskSequence parameter, then pipe that to Export-DeployRTaskSequence or run the Export-DeployRTaskSequence function with the Id parameter.

```powershell
Get-DeployRMetadata -Type TaskSequence
```

Example below of taking the ID which can be found via PowerShell or the URL for the task sequence in the dashboard.

```powershell
$TSID = '102d275b-fee3-477b-aa07-a8f63ca2140c'
Export-DeployRTaskSequence -Id $TSID -DestinationFolder D:\TSBackups
```

Here is a sample code block for exporting task sequences that are not built in

```powershell
#Backup DeployR task sequences
Write-Host "Backing up DeployR task sequences..." -ForegroundColor Yellow
(Get-DeployRMetadata -Type TaskSequence | Where-Object {$_.id -notlike '0000*'}) | ForEach-Object {
    write-host "Backing up task sequence: $($_.name) | $($_.id)" -ForegroundColor Cyan
    Export-DeployRTaskSequence -Id $_.id -DestinationFolder "D:\Backups\TaskSequences\$($_.name)-$($_.id)"
}
```

The exported task sequence will be a json file with the name of the GUID.  It will not include the content items referenced, just the metadata of the task sequence.

<figure><img src="/files/mWPwVwevZhaSx7klYWTY" alt=""><figcaption></figcaption></figure>

For a script example, check out this on our GitHub: <https://github.com/2pintsoftware/2Pint-DeployR/blob/main/Samples/Webinars/ExportSpecificTaskSequenceMetaData.ps1>

### Importing Task Sequences

Importing requires you have access to an exported task sequence json file, then running the import function like so:

```powershell
Import-DeployRTaskSequence -SourceFile D:\DeployRCIs\TEMP\102d275b-fee3-477b-aa07-a8f63ca2140c.json
```

### Clone Task Sequence

The clone feature is identical to the importing, but you specify -clone

```powershell
Import-DeployRTaskSequence -SourceFile 102d275b-fee3-477b-aa07-a8f63ca2140c.json -clone
```

For an example script of the clone ability, see this script on our GitHub. <https://github.com/2pintsoftware/2Pint-DeployR/blob/main/Samples/Webinars/CloningTaskSequenceDemo.ps1>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.2pintsoftware.com/deployr/powershell-modules/scripting-for-deployr-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
