Creating Credentials keys under one folder

Resolved

Creating Credentials keys under one folder

avatar

Devolution server version 2024.3.11.0
Powershell version 7.6
Remote Desktop manager entreprise endition 2024.3.28.0

I need to create Credentials keys under one new folder called database which should be under City\AAAA\

# Import the Devolutions PowerShell module
Import-Module -Name "C:\Program Files\WindowsPowerShell\Modules\Devolutions.PowerShell\2024.3.11\Devolutions.PowerShell.psd1"

# Connect to the RDM data source
$ds = Get-RDMDataSource -Name "db"
Set-RDMCurrentDataSource $ds

# Define the environment path
$ENV = 'City\AAA\'
$database='database'
# Check if the folder already exists
$existingFolder = Get-RDMSession -Type Group -GroupName '$ENV$database'

if (-not $existingFolder) {
Write-Host "Creating folder: $ENV$database"

# Create the database folder
$databaseFolder = New-RDMSession -Name "database" -Type group -Group $ENV -SetSession
Set-RDMSession -Session $databaseFolder -Refresh

# Define credentials in an array
$credentials = @(
@{ Name = "aa"; User = "test"; Pass = "bbb" }
@{ Name = "ccc" = "test1"; Pass = "dd" }
@{ Name = "eee"; User = "test2"; Pass = "fff" }
)
# Loop through credentials and create sessions
foreach ($cred in $credentials) {
$database='database'
$password = ConvertTo-SecureString $cred.Pass -AsPlainText -Force
$session = New-RDMSession -Type Credential -Name $cred.Name -Group "$ENV$database" -SetSession
$mysession = New-RDMSession -Name $cred.Name -Type Credential -SetSession
$session.ConnectionSubType = "Default"
Set-RDMSessionUsername -Session $session -UserName $cred.User -Refresh
Set-RDMSessionPassword -Session $session -Password $password -Refresh
Set-RDMSession -Session $session -Refresh
Write-Host "Created credential: $($cred.Name)"
}

} else {
Write-Host "Folder already exists. Skipping creation."
}


The folder database under City\AAA\ is created and the first Credential "aa" is created under it
but the second and third Credential failed with error

Set-RDMSession: C:\Users\Desktop\RDM\Key.ps1:43
Line |38 | Set-RDMSession -Session $session -Refresh
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Connection has invalid group specified.
Created credential: bbb

All Comments (6)

avatar

Hello hm,

Thank you for reaching out to the Devolutions support team.

I have reviewed your script, and some adjustments can be made.
The must obvious one is the @{ Name = "ccc" = "test1"; Pass = "dd" } that will be @{ Name = "ccc"; User = "test1"; Pass = "dd" }

Also, since you are using the Variable $ENV and $database, you should use the "$" instead of '$'.

After a quick adjustment, it should look like this:

# Connect to the RDM data source
$ds = Get-RDMDataSource -Name "db"
Set-RDMCurrentDataSource $ds

# Define the environment path
$ENV = 'City\AAA\'
$database='database'
# Check if the folder already exists
$existingFolder = Get-RDMSession -Type Group -GroupName "$ENV""$database"

if (-not $existingFolder) {
    Write-Host "Creating folder: $ENV""$database"

    # Create the database folder
    $databaseFolder = New-RDMSession -Name "database" -Type group -Group $ENV -SetSession
    Set-RDMSession -Session $databaseFolder -Refresh
    
    # Define credentials in an array
    $credentials = @(
@{ Name = "aa"; User = "test"; Pass = "bbb" }
@{ Name = "ccc"; User = "test1"; Pass = "dd" }
@{ Name = "eee"; User = "test2"; Pass = "fff" }
    )
    # Loop through credentials and create sessions
    foreach ($cred in $credentials) {
$database='database'
$password = ConvertTo-SecureString $cred.Pass -AsPlainText -Force
$session = New-RDMSession -Type Credential -Name $cred.Name -Group "$ENV$database" -SetSession
$mysession = New-RDMSession -Name $cred.Name -Type Credential  -SetSession
$session.ConnectionSubType = "Default"
Set-RDMSessionUsername -Session $session -UserName $cred.User -Refresh
Set-RDMSessionPassword -Session $session -Password $password -Refresh
Set-RDMSession -Session $session -Refresh
Write-Host "Created credential: $($cred.Name)"
    }
	
} else {
    Write-Host "Folder already exists. Skipping creation."
}


I have tested it, and all the credentials entries are created.

If you still get the group error, simply refresh the connection in RDM by a CTRL + F5.
The group error issue should not appear.

Note that this is a known issue, and it should be fixed in 2025.1.

Best regards,

Patrick Ouimet

avatar

Hello Patrick Ouimet

First of all, thanks for your assistance
Please note that i used the adjusted code from your side and still getting the group error even after refreshing the connection in RDM

Can you please advise ?
should remove the loop and switch the code to another steps?

Thanks

avatar

Hello hm,

Could you add Update-RDMEntries after creating your entries/folders?
This should refresh the cache of RDM and also avoid this error message.

Let us know if this fixes this issue.

Best regards,

Patrick Ouimet

avatar

Hello Patrick Ouimet

Worked now .Appreciate your assistance .
May you advise on another issue?
The script executes correctly, creating the folders and entries as expected. However, when I run the script immediately after the first execution, the folders and entries are created again instead of entering the condition:
Write-Host "Folder already exists. Skipping creation."
Sometimes, the condition is met as expected, but other times, the script proceeds to create the folders and entries again. Could you assist in identifying why this inconsistency occurs?
Thank you for your help.
Best regards,

avatar

Hello hm,

Could you try this for the database folder?

$existingFolder = Get-RDMSession -Type Group -GroupName "$database"

if ([string]::IsNullOrEmpty($existingFolder)) {
$NewEntry = New-RDMSession -Name $database -Group $ENV -Type Group
Set-RDMSession $NewEntry
}

Patrick Ouimet

avatar

Hello Patrick Ouimet
I tried this condition and worked

if ([string]::IsNullOrEmpty($existingFolder)) {

Thanks