Forum / Remote Desktop Manager - PowerShell Repository

PowerShell Search for existing Session

  • Create an Issue
  • Cancel

I want to write a script that first searches to see if the session already exists and if it does then it throws an error. I would have it check every part of the path and for one section of the path if it doesn't exist I need to create a path based on a template. I was wondering if there was an easy way to do this?

Example:
Given Path: CompanyABC\NewYork\TechnologySolutions\Servers\RDPSESSION
Existing Path: CompanyABC\SanDiego
Flow:
CompanyABC folder exists, do not recreate
NewYork does not exist, create
TechnologySolutions does not exist, create from template
Servers folder exists, do not recreate
Create RDPSESSION

Clock6 yrs

FYI you don't need to create all folders. If you create an RDP session, with the Group/Folder = "CompanyABC\NewYork\TechnologySolutions\Servers" the system will create "virtual" folders for you. If ever you change a property of a "virtual" folder then it's saved as a group session. In PowerShell it would look something like this:

$session = New-RDM-Session -Group "CompanyABC\NewYork\TechnologySolutions\Servers" -Host "RDPSESSION" -Kind "RDPConfigured" -Name "RDPSESSION"

To get the list of all physical groups:

Get-RDM-Session | where {$_.Session.Kind -eq "Group"}

So for each folder you want/need to create using the template would look something like this:
$session = New-RDM-Session -Name "TechnologySolutions" -Kind "Group" -Group "CompanyABC\NewYork\TechnologySolutions" -TemplateID [GUID]"80cbfce3-5315-4b02-ab65-eaef7afe8a04"

Stefane Lavergne

signaturesignature

Clock6 yrs

So looking at your last example, I would specify -Group "CompanyABC\NewYork\TechnologySolutions" and it would recreate the TechnologySolutions group using the given template?

What are the other "filters" I can use in addition to $_.Session.Kind -eq "Group"?
I would like to see if a group does exist; for example, I don't want to create the TechnologySolutions folder if it already exists.

Logic:
If CompanyABC\NewYork\TechnologySolutions does not exist then {
$session = New-RDM-Session -Name "TechnologySolutions" -Kind "Group" -Group "CompanyABC\NewYork\TechnologySolutions" -TemplateID [GUID]"80cbfce3-5315-4b02-ab65-eaef7afe8a04"
}

Clock6 yrs

If you want them to be created using the Template then you must create it manually.

$folderName = "TechnologySolutions"
$folderPathFull = "CompanyABC\NewYork\" + $folderName;

# get all physically folders
$folders = Get-RDM-Session | where {$_.Session.Kind -eq "Group"}

# find the folder
$group = $folders | Where-Object { $_.Group -eq $folderPathFull };

# does not exist (physically), create it using the template
if (!$group)
{
$session = New-RDM-Session -Name $folderName -Kind "Group" -Group $folderPathFull -TemplateID "8afb9a2c-7cb6-4a7b-831b-ff090ae49d33"
Set-RDM-Session $session;
}

Stefane Lavergne

signaturesignature

Clock6 yrs

Perfect I think that will work for me!

Thanks

Clock6 yrs