Script to automate entry creation in DVLS

Script to automate entry creation in DVLS

avatar

Hi,

I created a script to create entries, for our automation process.

The goal is to be able to call the script with all required parameters, from our Gitlab CI runners.

I tried to create a pull request on your github but I couldn't.

So I'm pasting it here instead, feel free to add it to your collection.

Best regards,

Damien Daco

<#

.SYNOPSIS

Adds a single machine entry to Devolutions Server.

.DESCRIPTION

This script connects to a Devolutions Server instance and adds a new machine entry (RDP, SSH, Web, or VNC)

to a specified vault and folder. Credentials and connection details can be provided via parameters or environment variables.

.PARAMETER ServerUrl

The URL of the Devolutions Server instance.

.PARAMETER VaultId

The ID of the vault where the entry will be added.

.PARAMETER Name

The display name of the machine entry.

.PARAMETER HostName

The hostname or IP address of the machine.

.PARAMETER ConnectionType

The type of connection (RDP, SSH, Web, VNC).

.PARAMETER Folder

The folder name where the entry will be placed.

.PARAMETER Description

(Optional) Description for the entry.

.PARAMETER Username

(Optional) Username for the connection.

.PARAMETER Password

(Optional) Password for the connection.

.PARAMETER Domain

(Optional) Domain for the connection.

.PARAMETER Group

The group or parent folder for the entry.

.EXAMPLE

pwsh Add-Entries.ps1 -Name "tsttotoweb01" -HostName "127.0.0.1" -ConnectionType "RDP" -Folder "TOTO2" -Description "TST TOTO WEB 01" -UserName "DTA01" -Password "Password1234!" -Domain "MY_DOMAIN" -Group "TESTING"

.NOTES

Author: Damien Daco

Version: 1.0

Date: July 10, 2025

#>

[CmdletBinding()]

param (

[ValidateNotNullOrEmpty()]

[string]$ServerUrl = $DVLSUrl,

[ValidateNotNullOrEmpty()]

[string]$VaultId = "00000000-0000-0000-0000-000000000000",

[ValidateNotNullOrEmpty()]

[string]$Name,

[ValidateNotNullOrEmpty()]

[string]$HostName,

[ValidateNotNullOrEmpty()]

[string]$ConnectionType,

[ValidateNotNullOrEmpty()]

[string]$Folder,

[string]$Description,

[string]$Username,

[string]$Password,

[ValidateNotNullOrEmpty()]

[string]$Domain,

[ValidateNotNullOrEmpty()]

[string]$Group

)

# Import the Devolutions Server module if not already imported

try {

if(-not (Get-Module -Name Devolutions.PowerShell -ErrorAction Stop)) {

Write-Host "Importing Devolutions.PowerShell module..." -ForegroundColor Cyan

Import-Module -Name Devolutions.PowerShell -Force -ErrorAction Stop

}

}

catch {

Write-Error "Failed to import Devolutions.PowerShell module: $_"

Write-Host "Troubleshooting steps:" -ForegroundColor Yellow

Write-Host "1. Verify you have permission to install modules" -ForegroundColor Yellow

Write-Host "2. Run 'Install-Module Devolutions.PowerShell -Scope CurrentUser -Force' manually" -ForegroundColor Yellow

Write-Host "3. Check for any PowerShell execution policy restrictions" -ForegroundColor Yellow

exit 1

}

[string]$ApplicationID = $env:DS_USER

[string]$ApplicationSecretKey = $env:DS_PASSWORD

[string]$DVLSUrl = $env:DS_URL

[securestring]$SecApplicationSecretKey = ConvertTo-SecureString $ApplicationSecretKey -AsPlainText -Force

[pscredential]$Creds = New-Object System.Management.Automation.PSCredential ($ApplicationID, $SecApplicationSecretKey)

# Add a new machine entry to Devolutions Server

function Add-MachineEntry {

param (

[string]$Name,

[string]$HostName,

[string]$ConnectionType,

[string]$Folder,

[string]$Description,

[string]$Username,

[string]$Password,

[string]$Domain,

[int]$Port,

[string]$Group,

[string]$VaultId

)

try {

# Check if entry already exists

Write-Host "Checking if entry '$Name' already exists ..." -ForegroundColor Cyan

$ExistingEntry = Get-DSEntry -VaultId $VaultId -FilterValue $Name -ErrorAction SilentlyContinue

if ($ExistingEntry) {

Write-Host "Entry '$Name' already exists. Skipping creation." -ForegroundColor Yellow

exit 0

} else {

Write-Host "Entry '$Name' does not exists. Creating..." -ForegroundColor Green

# Create entry parameters based on connection type

$EntryParams = @{

Name = $Name

HostName = $HostName

Description = $Description

VaultId = $VaultId

Group = $Group

}

if (-not [string]::IsNullOrEmpty($Username) -and -not [string]::IsNullOrEmpty($Password)) {

$EntryParams.Add('Username', $Username)

$EntryParams.Add('Password', $Password)

}

# Add port if specified

if ($Port -gt 0) {

$EntryParams.Add('Port', $Port)

}

# Create entry based on connection type

switch ($ConnectionType.ToLower()) {

"rdp" {

New-DSRDPEntry @EntryParams

}

"ssh" {

New-DSSSHShellEntry @EntryParams

}

"web" {

New-DSWebEntry @EntryParams

}

"vnc" {

New-DSVNCEntry @EntryParams

}

default {

Write-Warning "Unsupported connection type: $ConnectionType. Using generic connection."

New-DSConnection @EntryParams

}

}

Write-Host "Added $ConnectionType entry for $Name" -ForegroundColor Green

}

}

catch {

Write-Error "Failed to add entry for $Name`: $_"

exit 1

}

}

# Main script execution

try {

# Connect to Devolutions Server

$Response = New-DSSession -Credential $Creds -BaseURI $DVLSUrl -AsApplication

Add-MachineEntry -Name $Name -HostName $HostName -ConnectionType $ConnectionType `

-Description $Description -Username $Username `

-Password $Password -Domain $Domain `

-Group "$Group\$Folder" -VaultId $VaultId

# -Port ([int]::TryParse($Machine.Port, [ref]$null) )

Write-Host "Script completed successfully!" -ForegroundColor Green

}

catch {

Write-Error "An error occurred: $_"

exit 1

}

finally {

# Close the connection to Devolutions Server

Close-DSSession

Write-Host "Disconnected from Devolutions Server" -ForegroundColor Cyan

}

All Comments (1)

avatar

Hello Damien,

Thank you for this script.

We will add it to our GitHub library and post the link here when it's done

Best regards,

Patrick Ouimet