Develop for XenServer

Getting Started with PowerShell

The XenServer-SDK directory contains the following folders that are relevant to PowerShell users:

  • XenServerPowerShell
    • PowerShell_7: The XenServer SDK for PowerShell 7.
      • XenServerPSModule: The XenServer PowerShell module for PowerShell 7.
      • src: C# source code for the XenServer PowerShell cmdlets.
    • PowerShell_51: The XenServer SDK for PowerShell 5.1.
      • XenServerPSModule: The XenServer PowerShell module for PowerShell 5.1.
      • src: C# source code for the XenServer PowerShell cmdlets.

RPC protocol

The PowerShell SDK supports the JSON-RPC v2.0 protocol.

Platform supported

XenServer SDK for PowerShell 7:

  • Windows
  • Linux

This library requires .NET 6.0 and PowerShell 7.2 or greater.

XenServer SDK for PowerShell 5.1:

  • Windows

This library requires .NET 4.5 and PowerShell 5.1.

Library

  • XenServerPSModule

Note:

This module is generally, but not fully, backwards compatible. To interact with hosts running older versions of XenServer or Citrix Hypervisor, it is advisable to use the module of the same version as the host.

Dependencies

Installation

First, extract the contents of the SDK ZIP file.

Note:

Some web browsers may mark the SDK ZIP file as “blocked” during the download. To import the module successfully you will need to unblock the SDK ZIP file before extracting its contents. To unblock the SDK ZIP file, right-click on it and launch the Properties dialog. Click the Unblock button, then the Apply or OK button.

XenServer SDK for PowerShell 7:

  1. Navigate to the extracted XenServer-SDK\XenServerPowerShell\PowerShell_7 directory and copy the whole folder XenServerPSModule into your PowerShell modules directory.

    • On Windows this will normally be $env:UserProfile\Documents\PowerShell\Modules for per-user configuration, or $env:ProgramFiles\PowerShell\7\Modules for system-wide configuration.
    • On Linux this will be ~/.local/share/powershell/Modules for per-user configuration, or /usr/local/share/powershell/Modules for system-wide configuration.

    For more information see PowerShell’s documentation on module paths:

    Get-Help about_PSModulePath
    <!--NeedCopy-->
    
  2. Open a PowerShell 7 prompt as administrator.

    To do this, open the Windows Start menu by clicking the Start icon, find the item PowerShell 7, right-click it and select Run as administrator.

  3. Determine the current execution policy:

    Get-ExecutionPolicy
    <!--NeedCopy-->
    

    If the current policy is Restricted, you need to set it to RemoteSigned:

    Set-ExecutionPolicy RemoteSigned
    <!--NeedCopy-->
    

    You should understand the security implications of this change. If you are unsure, see PowerShell’s documentation on execution policies:

    Get-Help about_Execution_Policies
    <!--NeedCopy-->
    

    If the current policy is AllSigned, you will be able to use the XenServer PowerShell module, but it will be inconvenient because this policy requires even scripts that you write on the local computer to be signed. You may want to change it to RemoteSigned, as above.

    If the current policy is RemoteSigned, ByPass, or Unrestricted there is nothing to do.

  4. Exit the privileged instance of PowerShell.

  5. Open a PowerShell 7 prompt as a regular user (click Start > PowerShell 7) and import the XenServer PowerShell module:

    Import-Module XenServerPSModule
    <!--NeedCopy-->
    
  6. If you wish to load specific environment settings when the XenServer PowerShell module is loaded, create the file XenServerProfile.ps1 and put it in the folder containing your $PROFILE file for per-user configuration, or in $PSHOME for system-wide configuration.

    • On Windows these will normally be $env:UserProfile\Documents\PowerShellfor per-user configuration, or $env:ProgramFiles\PowerShell\7 for system-wide configuration.

    • On Linux these will be ~/.config/powershell for per-user configuration, or /opt/microsoft/powershell/7 for system-wide configuration.

XenServer SDK for PowerShell 5.1:

  1. Navigate to the extracted XenServer-SDK\XenServerPowerShell\PowerShell_51 directory and copy the whole folder XenServerPSModule into your PowerShell modules directory.

    • On Windows this will normally be $env:UserProfile\Documents\WindowsPowerShell\Modules for per-user configuration, or $env:windir\system32\WindowsPowerShell\v1.0\Modules for system-wide configuration.

    For more information see PowerShell’s documentation on module paths:

    PS> Get-Help about_PSModulePath
    
  2. Open a PowerShell 5.1 prompt as administrator.

    To do this, open the Windows Start menu by clicking the Start icon, find the item PowerShell 5.1, right click it and select Run as administrator.

  3. Determine the current execution policy:

    PS> Get-ExecutionPolicy
    <!--NeedCopy-->
    

    If the current policy is Restricted, you need to set it to RemoteSigned:

    PS> Set-ExecutionPolicy RemoteSigned
    <!--NeedCopy-->
    

    You should understand the security implications of this change. If you are unsure, see PowerShell’s documentation on execution policies:

    PS> Get-Help about_Execution_Policies
    <!--NeedCopy-->
    

    If the current policy is AllSigned, you will be able to use the XenServer PowerShell module, but it will be inconvenient because this policy requires even scripts that you write on the local computer to be signed. You may want to change it to RemoteSigned, as above.

    If the current policy is RemoteSigned, ByPass, or Unrestricted there is nothing to do.

  4. Exit the privileged instance of PowerShell.

  5. Open a PowerShell 5.1 prompt as a regular user (click Start > PowerShell) and import the XenServer PowerShell Module:

    PS> Import-Module XenServerPSModule
    <!--NeedCopy-->
    
  6. If you wish to load specific environment settings when the XenServer PowerShell module is loaded, create the file XenServerProfile.ps1 and put it in the folder containing your $PROFILE file for per-user configuration, or in $PSHOME for system-wide configuration.

    • On Windows these will normally be $env:UserProfile\Documents\WindowsPowerShell for per-user configuration, or $env:windir\system32\WindowsPowerShell\v1.0 for system-wide configuration.

Getting help

For an overview of the XenServer PowerShell module, type:

Get-Help about_XenServer
<!--NeedCopy-->

You can obtain a list of all available cmdlets by running:

Get-Command -Module XenServerPSModule
<!--NeedCopy-->

For help with a specific command use:

Get-Help [CommandName]
<!--NeedCopy-->

See Using the XenServer PowerShell module for an overview of the module cmdlets and their structure, and a number of walk-throughs on how to perform certain specialized tasks.

Building and debugging the source code

Open the project XenServerPowerShell.csproj in Visual Studio 2022. You should now be ready to build the source code.

If in Debug mode, clicking Start will launch a PowerShell 7 prompt as an external process and import the compiled XenServerPowerShell.dll as a module (without, however, processing the scripts, types, and formats shipped within the XenServer PowerShell module). You should now be ready to debug the cmdlets.

Examples

Examples on the usage of the XenServer Powershell module can be found at XenServer PowerShell Module usage examples on GitHub.

Getting Started with PowerShell