«

»

Setting VMware View Desktop State Remotely

Automation has been always the best friend of good System Administrators. Automating repetitive tasks is a key enabler in most IT environments. However, VMware View has some known limitations when talking about automation and remote management.

Besides the Graphical User Interface, local PowerCLI is the only supported management methodology. I say local PowerCLI because none of the VMware View PowerCLI cmdlets allow remote management today. I have previously demonstrated how to use VMware.View.Broker PSSnap-In to configure WinRM sessions and execute remote View PowerCLI (here). It is possible to remotely execute PowerCLI cmdlets using New-PSSession as I demonstrated in my previous post and in the example below.

The PowerShell function below will change the state of a View desktop via ADAM/LDAP database. If pae-VmState is set to READY the VM is accessible, if set to ERROR the VM is not accessible, if set to MAINTENANCE the VM goes into maintenance mode, and if set to DELETING the VM will be deleted.

pae-VmState = “READY”
pae-VmState = “ERROR”
pae-VmState = “MAINTENANCE”
pae-VmState = “DELETING”

The function uses the ADSI adapter to receive an object representation of an Active Directory object and set the new VM state in the ADAM database. This method does not require Remote PowerShell. However to update the ADAM object you will need to find the $_.machine_id via Get-DesktopVM, and this cmdlet will require the remote PowerShell session.

 

Function ViewVM-SetState {
    [cmdletBinding()]
    Param (
        [parameter(Mandatory=$true)]
        [string]$vm,                    #VM name
        [parameter(Mandatory=$true)]
        [ValidateSet("MAINTENANCE","READY","DELETING","ERROR")] 
        [string]$state,                    #State to set the VM to
        [parameter(Mandatory=$true)]
        [string]$ViewIPAddress,            #Connection Server IP Address
        $credential                        #Connection Server Credential
    )

    $ldapBaseURL = 'LDAP://'+$ViewIPAddress + ':389/'
    $script = [scriptblock]::Create("(Get-DesktopVM -Name $vm).machine_id")
    $session = New-PSSession -ComputerName $ViewIPAddress -Credential $credential

    $Machine_Id = Invoke-Command -Session $session -ScriptBlock $script

    $objMachine_Id = [ADSI]($ldapBaseURL + "cn=" + $Machine_Id + ",ou=Servers,dc=vdi,dc=vmware,dc=int")
    $objMachine_Id.PSBase.UserName = "administrator"
    $objMachine_Id.PSBase.Password = "password"

    switch ($state) {
        "MAINTENANCE" { 
            $objMachine_Id.put("pae-vmstate", "MAINTENANCE")
        }
        "READY" {
            $objMachine_Id.put("pae-vmstate", "READY")
        }
        "DELETING" {
            $objMachine_Id.put("pae-vmstate", "DELETING")
        }
        "ERROR" {
            $objMachine_Id.put("pae-vmstate", "ERROR")
        }
        default {
            break
        }
    }
    $objMachine_Id.setinfo()

    Return ""
}

 

This article was first published by Andre Leibovici (@andreleibovici) at myvirtualcloud.net.

 

 

Similar Posts:

Permanent link to this article: http://myvirtualcloud.net/?p=3925

2 comments

  1. Mike Gibson

    This information is fantastic. Programmatically placing a VM into Maintenance mode was the last step I required in automating my fornesic data aquisition process in my View environment. I couldn’t believe they didn’t have a cmdlet for this already.

  2. Andre Leibovici

    Mike, I am glad it has helped you.

Leave a Reply