Thursday, September 07, 2006

Adding entries to visionapp Remote Desktop with PowerShell

Now that you have visionapp Remote Desktop as I wrote about, how do you populate it easy with all the servers you want? Easy! The configuration is stored as XML as one long string in the registry value hkcu\software\visionapp\vRD\Configuration. And as the registry and xml is native to PowerShell the choice was easy.

So here is my PowerShell scripts adding a test folder and a test connection -

# The registry key

$regkey = get-item 'HKCU:\software\visionapp\vRD'

# read the configuration value
$config = $regkey | get-itemproperty -name configuration

# save it to a file in %temp% - just in case (you can also use backup from the app)
$savedConfig = join-path $env:temp vRd.saved
$config.configuration > $savedConfig

# convert string to xml - cool huh!
$config = [xml] $config.configuration

# show part of the xml file
# and drill down into the ConnectionsFolder

# Create a Folder xml element
$newfolder = $config.CreateElement("Folder")

# Set its name
$newfolder.SetAttribute("Name","Test folder")

# and add it as a child to the ConnectionsFolder

# show the added folder

# Add some attributes - note that you can do this before or after
# adding the xml element - $newfolder is a pointer to the live value
$newfolder.SetAttribute("Description","Testing dude!")
# Link folder to a credentials GUID (I checked this beforehand)

# Create a Connection xml element with some attributes
$newconnection = $config.CreateElement("Connection")

# Add it to the new folder

# dump folder as XML

# we have to do this several times, so a small function is easier
function addElement($xml,[string] $elementName, $elementText) {
return $e

# these values must be child elements - not attributes - to the connection element

# note the double-parenthesis - the inner is used to call the addElement function
# and return the value to the appendChild method
$newconnection.appendChild((addElement $config "SmartCard" "false"))
$newconnection.appendChild((addElement $config "ResolutionX" "1024"))
$newconnection.appendChild((addElement $config "ResolutionY" "768"))
$newconnection.appendChild((addElement $config "AutoSize" "true"))
$newconnection.appendChild((addElement $config "Description" "Test server"))
$newconnection.appendChild((addElement $config "Printer" "false"))
# Take credentials from parent folder
$newconnection.appendChild((addElement $config "InheritCredentials" "true"))
$newconnection.appendChild((addElement $config "Credentials" "00000000-0000-0000-0000-000000000000"))
$newconnection.appendChild((addElement $config "SeparateWindow" "false"))
$newconnection.appendChild((addElement $config "Console" "false"))
$newconnection.appendChild((addElement $config "Serial" "false"))
$newconnection.appendChild((addElement $config "LocalDrives" "false"))
$newconnection.appendChild((addElement $config "Audio" "false"))
$newconnection.appendChild((addElement $config "Port" "3389"))

# dump again

# update configuration in registry

$regkey | set-itemproperty -name configuration -value $config.get_innerxml()

# folder and connection added!

PowerShell rocks! - did I mention that earlier...


Kiran Reddy said...

Hi i dont know too much about power shell;was looking for a script that would add the server names and ip's from a text file.Thought this would do it but cant get it to work.get an error "Unexpected token 'get-itemproperty' in expression or statement
At C:\Scripts\s.ps1:7 char:35
+ $config = $regkey get-itemproperty <<<< -name configuration"

Anonymous said...

Hi Kiran.

The is a vertical bar (|) missing between $regkey and get-itemproperty. I have had this problem easier when my vertical bars were eaten during posting. I have fixed the script, but not tested it ;)