7

Setting VMware View Desktop State Remotely

 2 years ago
source link: https://myvirtualcloud.net/setting-vmware-view-desktop-state-remotely/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Setting VMware View Desktop State Remotely

  • 09/20/2012

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK