XenCenter Plugins - Hello World Example
Here is a short example which will guide you through the creation of a XenCenter plugin.
Objective: We would like a new menu item in XenCenter which says "hello from ..." where "..." denotes the name of the object selected in the treeview.
In this example we shall achieve this by using the XenServerPSSnapIn PowerShell bindings.
The first step is to install the following requirements:
|| Download Location
| .Net Framework v2.0
| .Net Framework v2.0 SDK
|Microsoft PowerShell v1.0||http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx|
| Citrix XenCenter v5.5
| Citrix XenServerPSSnapIn v5.5
Now we need to create a plugin descriptor file.
We start with the XenCenterPlugin node with the following attributes:
- xmlns - Specifies the schema of the XML file.
- version - The XenCenter Plugins version, we are using version 1.
- plugin_version - The version number of this plugin.
To create a menu item we add a MenuItem node under the XenCenterPlugin node. We give this three attributes:
- name - For identfying the menu item, used to reference the label in the resources file.
- menu - The name of the menu under which the menu item should appear. Let's use the 'View' menu.
- serialized - Decides if copies of the plugin running simutaneously are allowed for the same object. Here we do not limit this so we set to "none".
Next we add the XenServerPowerShell tag which points to a PowerShell script to run:
- filename - The filepath relative to the install directory of the XenCenter executable of a PowerShell script to run (or an absolute filepath).
- window - Show the console window which runs the script. We don't want this.
Close off all the tags and save as HelloWorld.xcplugin.xml. The name of this file must be the same name as the name of the plugin directory in which it resides.
The next step is to add some resources which provide strings and images for the plugin. These are stored in DLLs so that the correct language (if other cultures are provided) can be loaded at run-time.
First we need to create the resx file. This can be done using Visual Studio. Add strings for the menu-item labels, copyright statements, filepaths to icons etc. The names of the resources strings should be <name>.<property>, where <name> is the name given to the tag and <property> is one of the properties found in the specification. Here is the resources table for this plugin.
|HelloWorld.description|| XenServer PowerShell plugin example.
|HelloWorld.copyright||© Citrix Systems Inc. 2009|
|hello-menu-item.description||Displays 'Hello World' from the selected object.|
The final step is to convert this .resx into a DLL. We do this with two tools:
- ResGen.exe - Creates a .resources file from the resx. This can be found in the .NET framework SDK.
- Al.exe - Embeds the .resources file into a DLL. We specify we want to create a library, the file we wish to embed, that we need the invariant culture and the name of the DLL file. Al.exe is in the .NET framework directory.
We can put any PowerShell in the script. This example makes use of the context sensitive data which is passed to the PowerShell script. XenCenter sets up the following variables before executing the script:
- $url - The URL of the server which owns the object selected in the tree view
- $uuid - An authenticated session ID for the server (allows the script to log into the server)
- $class - The type of the object selected: Server, VM etc
- $objUuid - The UUID for the selected object.
Before any server commands can be used create a new connection with the session. We can do this in PowerShell.
We can then use XenServer commands without having to provide credentials for each PowerShell statement.
Finally we can also make .NET Windows Forms to create an alert box. We have to load the correct DLL before we request the MessageBox.
The full PowerShell script is HelloWorld.ps1
The best way to distribute your XenCenter plugin is to package your plugin into a single MSI (Windows Installer) file.
Using a Windows Installer allows you to make sure the plugin is being installed into the correct place (by checking the XenCenter InstallDir registry key) and it gives versioning. A newer version will automatically uninstall the old version and then install the new one.