PoshCode Archive  Artifact Content

Artifact c8eb4b5d00a285b09f71208d837b209dc014976be223c8fb9b951c643751ecd8:

  • File vmrc-remote-connector.ps1 — part of check-in [fbe9c4b6dc] at 2018-06-10 12:58:11 on branch trunk — Connects with VMRC to an ESX Farm. Now with option to allow multiple connections to one vm (user: Patrick size: 5295)

# encoding: utf-8
# api: powershell
# title: vmrc remote connector
# description: Connects with VMRC to an ESX Farm. Now with option to allow multiple connections to one vm
# version: 0.1
# type: script
# author: Patrick
# license: CC0
# x-poshcode-id: 1527
# x-archived: 2016-05-20T09:58:30
# x-published: 2010-12-13T23:45:00
#
#
#region Global var
$script:viserver = "esx.local"
$script:vmrc = "C:\Program Files\VMwareVMRC\vmware-vmrc.exe"
$script:vm = ""
$script:vmt = ""
$script:vmcon = ""
$script:selectvm = ""
$script:multiple = $FALSE #Set to TRUE for multiple VMRC Instances
#endregion

$vmcon = Connect-VIServer -Server $script:viserver -Protocol https -Credential (Get-Credential)
if (!$vmcon) {
	Write-Output "No Connection!"
	break
}

$script:vmt = Get-VM 

function GenerateForm {

#region Import the Assembles
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$btnConnect = New-Object System.Windows.Forms.Button
$txtEntity = New-Object System.Windows.Forms.TextBox
$chkIP = New-Object System.Windows.Forms.CheckBox
$comboBox1 = New-Object System.Windows.Forms.ComboBox
#endregion Generated Form Objects

$btnConnect_OnClick= 
{

#region Check User Inputs
if (!$txtEntity.Text -eq "" -and !$comboBox1.Text -eq "") 
{
	Write-Host "Bitte nur eine Option anwählen!"
	break
} 
#endregion

if ($txtEntity.Text -eq "") {
	$script:selectvm = $comboBox1.Text
} else { 
	$script:selectvm = $txtEntity.Text
}

if ($chkIP.Checked) { 
		$script:chk = "FALSE"

		foreach ($script:vm in $script:vmt) {
			$vmg = Get-VMGuest $script:vm
			
			if ($vmg.IPAddress -eq $script:selectvm) {
				$script:chk = ""
				break
			} 
		}
	}
	
If (!$script:selectvm -eq "" -and !$chkIP.Checked)  {
		$script:vm = Get-VM $script:selectvm
	
		if($script:vm.Count) {
			Write-Output "Define only one host!"
			break
		} else {
			if (!$script:vm.id) { 
				Write-Output "No VM found"
				$script:chk = "FALSE";
				break
			}

		}
	}
	
	if (!$script:chk) {
		if (!$script:vm -eq "") {
			if (!($script:gvm = Get-View $script:vm.id) -eq "") {
					if (($multiple) -or (!($multiple) -and $script:gvm.Runtime.NumMksConnections -lt 1)) {
						$vmrcargs = '-h ' + $script:vm.Host + ' -m "' + $script:gvm.Config.Files.VmPathName + '"'
						[Diagnostics.Process]::Start($script:vmrc, $vmrcargs)
					}
 			}
		}
	}
}

#----------------------------------------------
#region Generated Form Code
$form1.Text = 'VMware Remote Connector'
$form1.Name = 'form1'
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 193
$System_Drawing_Size.Width = 281
$form1.ClientSize = $System_Drawing_Size
$form1.FormBorderStyle = 1

$btnConnect.TabIndex = 2
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnConnect.Size = $System_Drawing_Size
$btnConnect.Name = 'btnConnect'
$btnConnect.UseVisualStyleBackColor = $True

$btnConnect.Text = 'Verbinden'
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 97
$System_Drawing_Point.Y = 115
$btnConnect.Location = $System_Drawing_Point

$btnConnect.DataBindings.DefaultDataSourceUpdateMode = 0
$btnConnect.add_Click($btnConnect_OnClick)

$form1.Controls.Add($btnConnect)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 17
$txtEntity.Location = $System_Drawing_Point
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 261
$txtEntity.Size = $System_Drawing_Size
$txtEntity.DataBindings.DefaultDataSourceUpdateMode = 0
$txtEntity.Name = 'txtEntity'
$txtEntity.TabIndex = 1

$form1.Controls.Add($txtEntity)

$chkIP.TabIndex = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 19
$System_Drawing_Size.Width = 160
$chkIP.Size = $System_Drawing_Size
$chkIP.Name = 'chkIP'
$chkIP.UseVisualStyleBackColor = $True

$chkIP.Text = 'IP Adresse'
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 43
$chkIP.Location = $System_Drawing_Point

$chkIP.DataBindings.DefaultDataSourceUpdateMode = 0

$form1.Controls.Add($chkIP)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 73
$comboBox1.Location = $System_Drawing_Point
$comboBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBox1.FormattingEnabled = $True
$comboBox1.Name = 'comboBox1'
$comboBox1.TabIndex = 0
#Get all VM
foreach ($cvm in $script:vmt) {
	$comboBox1.Items.Add($cvm.Name) | Out-Null
}
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 261
$comboBox1.Size = $System_Drawing_Size

$form1.Controls.Add($comboBox1)

#endregion Generated Form Code

#Show the Form
$form1.ShowDialog()| Out-Null

}

GenerateForm