«

»

Sep 20 2012

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

6 comments

Skip to comment form

  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.

  3. Nathan Manzi

    I’ve modified this function so that it works when run on a View Connection Server directly, if people don’t want to do the work to get remote View PowerCLI going. Tested against View 5.2. https://gist.github.com/nmanzi/6198708

  4. Andre Leibovici

    Nathan, good job and thanks for sharing with us.

    -Andre

  5. Kurt

    I was just looking into this and came across your article. Nice approach. As an alternative to doing a remote powercli command to look up the machine id, I use a directoryEntry object and set a filter on the pae-DisplayName property. For example:

    $vmName = “DESKTOP23″
    $vdiinfo = new-object DirectoryServices.DirectoryEntry( ($ldapServer + “OU=Servers,DC=vdi,DC=vmware,DC=int”), ($netCred.Domain +”\” + $netCred.Username), $netCred.password)
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($vdiinfo)
    searcher.Filter = “(pae-DisplayName=$vmName)”
    $desktops = $searcher.FindAll()

  6. Andre Leibovici

    Kurt, thanks for sharing.

    -Andre

Leave a Reply