Change session image by PNG disk file using Devolutions.Powershell module
Hi,
I have a lot of country related folders which are named based on the ISO Country Code. For example:
On disk I have .PNG files representing the flag for each country and these files are also named based on the ISO Country code. For example:
To avoid a lot of manual labor, I want to change the image/icon of each folder entry by using the Devolutions.Powershell module, but I don't know how.
This won't work at least:
$PNGFile = Get-ChildItem "C:\Country Flags\*.png" | where name -match ".$($CountryCode).png"
$Session.image = $PNGFile.FullName
Recommended Answer
I found the solution myself and I sincerely hope that I didn't spoiled too much of your time. Just for the record, my PS Code is:
<#
Folder structure syntax:
Sessions
Countries ------> Will be the parent folder in this script
<CC> ------> Is the two letter ISO Country Code. This folder will get the flag as its image/icon
<City>
Folder structure example:
Sessions
Countries
BE
DE
Berlin
Hamburg
ES
Barcelona
Madrid
Sevilla
FR
Paris
NL
IT
Rome
UK
#>
# Reading the parent folder
$ParentFolder = @(Get-RDMSession -ID 4ac1b844-5a96-4252-8da5-54a9e908dc30)
# Getting all folders including subfolders
$AllFolders = @(Get-RDMSession -GroupName $ParentFolder.Group -Type Group -IncludeSubFolders)
# Filtering the results because they include the parent folder too
$AllFolders = @($AllFolders | where-object {$_.ID -ne $ParentFolder.ID })
# Looping through each subfolder representing a country
foreach ($CountryFolder in $AllFolders)
{
if ($CountryFolder.name.Length -eq 2)
{
# Storing the folder name as the country code
$CountryCode = $CountryFolder.name
# Reading PNG file by using a name filter
$PNGFile = Get-ChildItem "C:\Temp\Country Flags\*.png" -ErrorAction Stop | where name -match ".$($CountryCode).png"
# Only do something when the PNG file exists.
if ($PNGFile.count -eq 1)
{
$bytes = [System.IO.File]::ReadAllBytes($PNGFile.fullName)
$CountryFolder.Image = $bytes
set-rdmsession $CountryFolder -ErrorAction Stop
}
}
}
I found the solution myself and I sincerely hope that I didn't spoiled too much of your time. Just for the record, my PS Code is:
<#
Folder structure syntax:
Sessions
Countries ------> Will be the parent folder in this script
<CC> ------> Is the two letter ISO Country Code. This folder will get the flag as its image/icon
<City>
Folder structure example:
Sessions
Countries
BE
DE
Berlin
Hamburg
ES
Barcelona
Madrid
Sevilla
FR
Paris
NL
IT
Rome
UK
#>
# Reading the parent folder
$ParentFolder = @(Get-RDMSession -ID 4ac1b844-5a96-4252-8da5-54a9e908dc30)
# Getting all folders including subfolders
$AllFolders = @(Get-RDMSession -GroupName $ParentFolder.Group -Type Group -IncludeSubFolders)
# Filtering the results because they include the parent folder too
$AllFolders = @($AllFolders | where-object {$_.ID -ne $ParentFolder.ID })
# Looping through each subfolder representing a country
foreach ($CountryFolder in $AllFolders)
{
if ($CountryFolder.name.Length -eq 2)
{
# Storing the folder name as the country code
$CountryCode = $CountryFolder.name
# Reading PNG file by using a name filter
$PNGFile = Get-ChildItem "C:\Temp\Country Flags\*.png" -ErrorAction Stop | where name -match ".$($CountryCode).png"
# Only do something when the PNG file exists.
if ($PNGFile.count -eq 1)
{
$bytes = [System.IO.File]::ReadAllBytes($PNGFile.fullName)
$CountryFolder.Image = $bytes
set-rdmsession $CountryFolder -ErrorAction Stop
}
}
}
Hello Jasper,
Thank you for reaching out to us regarding this,
I'm glad to hear you were able to find the solution. I appreciate you taking the time to share it with others who may have the same issue.
Best regards,
Samuel Dery