PoshCode Logo PowerShell Code Repository

vmrc remote connector by Patrick 26 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1527"></script>download | new post

Connects with VMRC to an ESX Farm. Now with option to allow multiple connections to one vm

  1. #region Global var
  2. $script:viserver = "esx.local"
  3. $script:vmrc = "C:\Program Files\VMwareVMRC\vmware-vmrc.exe"
  4. $script:vm = ""
  5. $script:vmt = ""
  6. $script:vmcon = ""
  7. $script:selectvm = ""
  8. $script:multiple = $FALSE #Set to TRUE for multiple VMRC Instances
  9. #endregion
  10.  
  11. $vmcon = Connect-VIServer -Server $script:viserver -Protocol https -Credential (Get-Credential)
  12. if (!$vmcon) {
  13.         Write-Output "No Connection!"
  14.         break
  15. }
  16.  
  17. $script:vmt = Get-VM
  18.  
  19. function GenerateForm {
  20.  
  21. #region Import the Assembles
  22. [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  23. [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  24. #endregion
  25.  
  26. #region Generated Form Objects
  27. $form1 = New-Object System.Windows.Forms.Form
  28. $btnConnect = New-Object System.Windows.Forms.Button
  29. $txtEntity = New-Object System.Windows.Forms.TextBox
  30. $chkIP = New-Object System.Windows.Forms.CheckBox
  31. $comboBox1 = New-Object System.Windows.Forms.ComboBox
  32. #endregion Generated Form Objects
  33.  
  34. $btnConnect_OnClick=
  35. {
  36.  
  37. #region Check User Inputs
  38. if (!$txtEntity.Text -eq "" -and !$comboBox1.Text -eq "")
  39. {
  40.         Write-Host "Bitte nur eine Option anwählen!"
  41.         break
  42. }
  43. #endregion
  44.  
  45. if ($txtEntity.Text -eq "") {
  46.         $script:selectvm = $comboBox1.Text
  47. } else {
  48.         $script:selectvm = $txtEntity.Text
  49. }
  50.  
  51. if ($chkIP.Checked) {
  52.                 $script:chk = "FALSE"
  53.  
  54.                 foreach ($script:vm in $script:vmt) {
  55.                         $vmg = Get-VMGuest $script:vm
  56.                        
  57.                         if ($vmg.IPAddress -eq $script:selectvm) {
  58.                                 $script:chk = ""
  59.                                 break
  60.                         }
  61.                 }
  62.         }
  63.        
  64. If (!$script:selectvm -eq "" -and !$chkIP.Checked)  {
  65.                 $script:vm = Get-VM $script:selectvm
  66.        
  67.                 if($script:vm.Count) {
  68.                         Write-Output "Define only one host!"
  69.                         break
  70.                 } else {
  71.                         if (!$script:vm.id) {
  72.                                 Write-Output "No VM found"
  73.                                 $script:chk = "FALSE";
  74.                                 break
  75.                         }
  76.  
  77.                 }
  78.         }
  79.        
  80.         if (!$script:chk) {
  81.                 if (!$script:vm -eq "") {
  82.                         if (!($script:gvm = Get-View $script:vm.id) -eq "") {
  83.                                         if (($multiple) -or (!($multiple) -and $script:gvm.Runtime.NumMksConnections -lt 1)) {
  84.                                                 $vmrcargs = '-h ' + $script:vm.Host + ' -m "' + $script:gvm.Config.Files.VmPathName + '"'
  85.                                                 [Diagnostics.Process]::Start($script:vmrc, $vmrcargs)
  86.                                         }
  87.                         }
  88.                 }
  89.         }
  90. }
  91.  
  92. #----------------------------------------------
  93. #region Generated Form Code
  94. $form1.Text = 'VMware Remote Connector'
  95. $form1.Name = 'form1'
  96. $form1.DataBindings.DefaultDataSourceUpdateMode = 0
  97. $System_Drawing_Size = New-Object System.Drawing.Size
  98. $System_Drawing_Size.Height = 193
  99. $System_Drawing_Size.Width = 281
  100. $form1.ClientSize = $System_Drawing_Size
  101. $form1.FormBorderStyle = 1
  102.  
  103. $btnConnect.TabIndex = 2
  104. $System_Drawing_Size = New-Object System.Drawing.Size
  105. $System_Drawing_Size.Height = 23
  106. $System_Drawing_Size.Width = 75
  107. $btnConnect.Size = $System_Drawing_Size
  108. $btnConnect.Name = 'btnConnect'
  109. $btnConnect.UseVisualStyleBackColor = $True
  110.  
  111. $btnConnect.Text = 'Verbinden'
  112. $System_Drawing_Point = New-Object System.Drawing.Point
  113. $System_Drawing_Point.X = 97
  114. $System_Drawing_Point.Y = 115
  115. $btnConnect.Location = $System_Drawing_Point
  116.  
  117. $btnConnect.DataBindings.DefaultDataSourceUpdateMode = 0
  118. $btnConnect.add_Click($btnConnect_OnClick)
  119.  
  120. $form1.Controls.Add($btnConnect)
  121.  
  122. $System_Drawing_Point = New-Object System.Drawing.Point
  123. $System_Drawing_Point.X = 12
  124. $System_Drawing_Point.Y = 17
  125. $txtEntity.Location = $System_Drawing_Point
  126. $System_Drawing_Size = New-Object System.Drawing.Size
  127. $System_Drawing_Size.Height = 20
  128. $System_Drawing_Size.Width = 261
  129. $txtEntity.Size = $System_Drawing_Size
  130. $txtEntity.DataBindings.DefaultDataSourceUpdateMode = 0
  131. $txtEntity.Name = 'txtEntity'
  132. $txtEntity.TabIndex = 1
  133.  
  134. $form1.Controls.Add($txtEntity)
  135.  
  136. $chkIP.TabIndex = 0
  137. $System_Drawing_Size = New-Object System.Drawing.Size
  138. $System_Drawing_Size.Height = 19
  139. $System_Drawing_Size.Width = 160
  140. $chkIP.Size = $System_Drawing_Size
  141. $chkIP.Name = 'chkIP'
  142. $chkIP.UseVisualStyleBackColor = $True
  143.  
  144. $chkIP.Text = 'IP Adresse'
  145. $System_Drawing_Point = New-Object System.Drawing.Point
  146. $System_Drawing_Point.X = 12
  147. $System_Drawing_Point.Y = 43
  148. $chkIP.Location = $System_Drawing_Point
  149.  
  150. $chkIP.DataBindings.DefaultDataSourceUpdateMode = 0
  151.  
  152. $form1.Controls.Add($chkIP)
  153.  
  154. $System_Drawing_Point = New-Object System.Drawing.Point
  155. $System_Drawing_Point.X = 12
  156. $System_Drawing_Point.Y = 73
  157. $comboBox1.Location = $System_Drawing_Point
  158. $comboBox1.DataBindings.DefaultDataSourceUpdateMode = 0
  159. $comboBox1.FormattingEnabled = $True
  160. $comboBox1.Name = 'comboBox1'
  161. $comboBox1.TabIndex = 0
  162. #Get all VM
  163. foreach ($cvm in $script:vmt) {
  164.         $comboBox1.Items.Add($cvm.Name) | Out-Null
  165. }
  166. $System_Drawing_Size = New-Object System.Drawing.Size
  167. $System_Drawing_Size.Height = 20
  168. $System_Drawing_Size.Width = 261
  169. $comboBox1.Size = $System_Drawing_Size
  170.  
  171. $form1.Controls.Add($comboBox1)
  172.  
  173. #endregion Generated Form Code
  174.  
  175. #Show the Form
  176. $form1.ShowDialog()| Out-Null
  177.  
  178. }
  179.  
  180. GenerateForm

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me