XenApp and XenDesktop PowerShell – VM Operations


Here’s a few XenApp and XenDesktop PowerShell functions that I find useful when automating certain tasks

  • Start VM
  • Stop VM
  • Wait for VM to power off
  • Wait for VM to register with delivery controller
  • Get hypervisor host for VM

For each of these functions, I assume the script is executing from a delivery controller.  If not, the following commands must include the following parameter.

-AdminAddress “FQDN of delivery controller”

  • Set-BrokerMachineMaintenanceMode
  • Net-BrokerHostingPowerAction
  • Get-BrokerMachine

If -AdminAddress isn’t included, PowerShell uses localhost.

Start VM

  • Take the VM out of maintenance mode
  • Start VM
function Start-VM {
    param(
    [string] $VM,
    [string] $DomainName
    )

    #Turn off maintenance mode
    Write-host (date -Format hh:mm:ss)   -   Taking VM $VM out of maintenance mode -ForegroundColor Yellow
    Set-BrokerMachineMaintenanceMode -InputObject $DomainName\$VM $false
    # Starting  target VM
    Write-host (date -Format hh:mm:ss)   -   Starting target VM $VM -ForegroundColor Yellow -Verbose
    New-BrokerHostingPowerAction -Action Turnon -MachineName $VM
}

Stop VM

  • Put the VM into maintenance mode
  • Stop the VM
function Stop-VM {
    Param(
    [string] $VM,
    [string] $DomainName
    )

    #Turn on maintenance mode
    Write-host (date -Format hh:mm:ss)   -   Placing VM $VM into maintenance mode -ForegroundColor Yellow
    Set-BrokerMachineMaintenanceMode -InputObject $DomainName\$VM $true
    # Stopping target VM
    Write-host (date -Format hh:mm:ss)   -   Shutting down target VM $VM -ForegroundColor Yellow -Verbose
    New-BrokerHostingPowerAction -Action Turnoff -MachineName $VM

}

Wait for VM to power off

function Wait-PowerOff {
    Param(
    [string] $VM

    )

   do
        {
        Write-host (date -Format hh:mm:ss)   -   Waiting for $VM to power off -ForegroundColor Yellow -Verbose
        $powerstate = Get-BrokerMachine -HostedMachineName $VM | foreach { $_.PowerState}
        If ($powerstate -eq "on"){
            start-sleep -s 10
        }
    } until($powerstate -eq "Off")
}

Wait for VM to register with delivery controller

function Wait-VMRegistration {
    param(
    [string] $VM
    )

        do
        {
            Write-host (date -Format hh:mm:ss)   -   Waiting for $VM to register with controller -ForegroundColor Yellow -Verbose
            $registration = Get-BrokerMachine -HostedMachineName $VM | foreach { $_.RegistrationState}
            start-sleep -s 10
        } until($registration -eq "Registered")
}

Get hypervisor host for VM

  • Wait for VM to power on
  • Query VM for hypervisor host
function Get-CurrentVMHost  {
    param(
    [string] $VM
    )

    # Identify hosts for targets and start metric gathering on XenServer host
    #Identify host. Must wait for host to power on. Only do this once

        do
        {
            $powerstate = get-brokermachine -HostedMachineName $VM  | foreach { $_.PowerState }
            start-sleep -s 1
        } until($powerstate -eq "On")

        #ID hostserver
        $hostserver = get-brokermachine -HostedMachineName $VM  | foreach { $_.hostingservername }
        return $hostserver
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: