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
$config.vRDConfigurationFile
# and drill down into the ConnectionsFolder
$config.vRDConfigurationFile.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
$config.vRDConfigurationFile.ConnectionsFolder.AppendChild($newfolder)

# show the added folder
$config.vRDConfigurationFile.ConnectionsFolder

# 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!")
$newfolder.SetAttribute("Sorted","false")
# Link folder to a credentials GUID (I checked this beforehand)
$newfolder.SetAttribute("Credentials","785dd5ed-c75a-48e1-bb31-0b47e1e75fc3")

# Create a Connection xml element with some attributes
$newconnection = $config.CreateElement("Connection")
$newconnection.SetAttribute("Name","server.test.com")
$newconnection.SetAttribute("ServerName","server.test.com")

# Add it to the new folder
$newfolder.appendChild($newconnection)

# dump folder as XML
$latest=$config.vRDConfigurationFile.ConnectionsFolder.folder.count-1
$config.vRDConfigurationFile.ConnectionsFolder.folder[$latest].get_OuterXML()

# we have to do this several times, so a small function is easier
function addElement($xml,[string] $elementName, $elementText) {
$e=$xml.CreateElement($elementName)
$e.set_innerText($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
$latest=$config.vRDConfigurationFile.ConnectionsFolder.folder.count-1
$config.vRDConfigurationFile.ConnectionsFolder.folder[$latest].get_OuterXML()

# update configuration in registry

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

# folder and connection added!



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

2 comments:

Unknown 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 ;)

Per