Functions/Open-VMwareVMConsole.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<#
    .SYNOPSIS
        Open a VMware Console session to the target VM.
 
    .DESCRIPTION
        Use the VMware.PowerCLI module to aquire a session ticket to the VM and
        use this to open the VMware Remte Console.
 
    .PARAMETER VM
        The target VM.
#>

function Open-VMwareVMConsole
{
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [VMware.VimAutomation.Types.VirtualMachine]
        $VM
    )

    begin
    {
        $viServer = Connect-VMware

        $viServiceInstance = Get-View -Id 'ServiceInstance'
        $viSessionManager = Get-View -Id $viServiceInstance.Content.SessionManager
    }

    process
    {
        # Open VMRC console
        $vmrcUri = 'vmrc://clone:{0}@{1}/?moid={2}' -f $viSessionManager.AcquireCloneTicket(), $viServer.Name, $vm.ExtensionData.MoRef.Value
        Start-Process -FilePath $vmrcUri
    }
}