SHARE
TWEET

PowerCLI VM Console Viewer

a guest Oct 24th, 2013 297 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
  2. $defaultvCenter = "vcenter"
  3. $viServer = $null
  4.  
  5. #Generated Form Function
  6. function GenerateForm {
  7. ########################################################################
  8. # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
  9. # Generated On: 10/24/2013 11:00 AM
  10. ########################################################################
  11.  
  12. #region Import the Assemblies
  13. [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  14. [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  15. #endregion
  16.  
  17. #region Generated Form Objects
  18. $form1 = New-Object System.Windows.Forms.Form
  19. $hostnameTextbox = New-Object System.Windows.Forms.TextBox
  20. $connectButton = New-Object System.Windows.Forms.Button
  21. $exitButton = New-Object System.Windows.Forms.Button
  22. $openButton = New-Object System.Windows.Forms.Button
  23. $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
  24. #endregion Generated Form Objects
  25.  
  26. #----------------------------------------------
  27. #Generated Event Script Blocks
  28. #----------------------------------------------
  29. #Provide Custom Code for events specified in PrimalForms.
  30. $exitButton_OnClick=
  31. {
  32.     $global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
  33.     $form1.Close()
  34. }
  35.  
  36. $connectButton_OnClick=
  37. {
  38.     $vCenter = $hostnameTextbox.Text
  39.     $global:viServer.IsConnected
  40.  
  41.     if ($viServer.IsConnected) {
  42.         Write-Host "Disconnecting from $vCenter"
  43.         $global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
  44.         $openButton.Enabled = $false
  45.     }
  46.     $global:viServer = Connect-VIServer $vCenter -ErrorAction SilentlyContinue
  47.     if ($global:viServer.IsConnected) {
  48.         Write-Host "Successfully connected to $vCenter"
  49.         $openButton.Enabled = $true
  50.     }
  51.     else {
  52.         [System.Windows.Forms.MessageBox]::Show("Failed to connect to vCenter server '$vCenter'", "Connection Failed")
  53.         $global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
  54.         $openButton.Enabled = $false
  55.     }
  56. }
  57.  
  58. $openButton_OnClick=
  59. {
  60.     $Selection = Get-VM | Out-GridView -OutputMode Multiple
  61.     $Selection | Foreach { Open-VMConsoleWindow $_ }
  62. }
  63.  
  64. $OnLoadForm_StateCorrection=
  65. {#Correct the initial state of the form to prevent the .Net maximized form issue
  66.         $form1.WindowState = $InitialFormWindowState
  67. }
  68.  
  69. #----------------------------------------------
  70. #region Generated Form Code
  71. $System_Drawing_Size = New-Object System.Drawing.Size
  72. $System_Drawing_Size.Height = 108
  73. $System_Drawing_Size.Width = 284
  74. $form1.ClientSize = $System_Drawing_Size
  75. $form1.DataBindings.DefaultDataSourceUpdateMode = 0
  76. $form1.Name = "form1"
  77. $form1.Text = "VM Console Viewer"
  78.  
  79. $hostnameTextbox.DataBindings.DefaultDataSourceUpdateMode = 0
  80. $System_Drawing_Point = New-Object System.Drawing.Point
  81. $System_Drawing_Point.X = 12
  82. $System_Drawing_Point.Y = 16
  83. $hostnameTextbox.Location = $System_Drawing_Point
  84. $hostnameTextbox.Name = "hostnameTextbox"
  85. $System_Drawing_Size = New-Object System.Drawing.Size
  86. $System_Drawing_Size.Height = 20
  87. $System_Drawing_Size.Width = 156
  88. $hostnameTextbox.Size = $System_Drawing_Size
  89. $hostnameTextbox.TabIndex = 3
  90. $hostnameTextbox.Text = $defaultvCenter
  91.  
  92. $form1.Controls.Add($hostnameTextbox)
  93.  
  94.  
  95. $connectButton.DataBindings.DefaultDataSourceUpdateMode = 0
  96.  
  97. $System_Drawing_Point = New-Object System.Drawing.Point
  98. $System_Drawing_Point.X = 174
  99. $System_Drawing_Point.Y = 14
  100. $connectButton.Location = $System_Drawing_Point
  101. $connectButton.Name = "connectButton"
  102. $System_Drawing_Size = New-Object System.Drawing.Size
  103. $System_Drawing_Size.Height = 23
  104. $System_Drawing_Size.Width = 98
  105. $connectButton.Size = $System_Drawing_Size
  106. $connectButton.TabIndex = 2
  107. $connectButton.Text = "Connect"
  108. $connectButton.UseVisualStyleBackColor = $True
  109. $connectButton.add_Click($connectButton_OnClick)
  110.  
  111. $form1.Controls.Add($connectButton)
  112.  
  113.  
  114. $exitButton.DataBindings.DefaultDataSourceUpdateMode = 0
  115.  
  116. $System_Drawing_Point = New-Object System.Drawing.Point
  117. $System_Drawing_Point.X = 12
  118. $System_Drawing_Point.Y = 72
  119. $exitButton.Location = $System_Drawing_Point
  120. $exitButton.Name = "exitButton"
  121. $System_Drawing_Size = New-Object System.Drawing.Size
  122. $System_Drawing_Size.Height = 23
  123. $System_Drawing_Size.Width = 260
  124. $exitButton.Size = $System_Drawing_Size
  125. $exitButton.TabIndex = 1
  126. $exitButton.Text = "Quit"
  127. $exitButton.UseVisualStyleBackColor = $True
  128. $exitButton.add_Click($exitButton_OnClick)
  129.  
  130. $form1.Controls.Add($exitButton)
  131.  
  132.  
  133. $openButton.DataBindings.DefaultDataSourceUpdateMode = 0
  134. $openButton.Enabled = $False
  135.  
  136. $System_Drawing_Point = New-Object System.Drawing.Point
  137. $System_Drawing_Point.X = 12
  138. $System_Drawing_Point.Y = 43
  139. $openButton.Location = $System_Drawing_Point
  140. $openButton.Name = "openButton"
  141. $System_Drawing_Size = New-Object System.Drawing.Size
  142. $System_Drawing_Size.Height = 23
  143. $System_Drawing_Size.Width = 260
  144. $openButton.Size = $System_Drawing_Size
  145. $openButton.TabIndex = 0
  146. $openButton.Text = "Open VM Consoles"
  147. $openButton.UseVisualStyleBackColor = $True
  148. $openButton.add_Click($openButton_OnClick)
  149.  
  150. $form1.Controls.Add($openButton)
  151.  
  152. #endregion Generated Form Code
  153.  
  154. #Save the initial state of the form
  155. $InitialFormWindowState = $form1.WindowState
  156. #Init the OnLoad event to correct the initial state of the form
  157. $form1.add_Load($OnLoadForm_StateCorrection)
  158. #Show the Form
  159. $form1.ShowDialog()| Out-Null
  160.  
  161. } #End Function
  162.  
  163. #Call the Function
  164. GenerateForm
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top