PoshCode Logo PowerShell Code Repository

vProfiles (modification of post by AlanRenouf view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1184"></script>download | new post

vProfiles V1.1 By Alan Renouf http://virtu-al.net, copies vSwitches and PortGroups from one host to another.

  1. #
  2. # vProfiles V1
  3. # By Alan Renouf
  4. # http://virtu-al.net
  5. #
  6.  
  7. #Generated Form Function
  8. function GenerateForm {
  9. ########################################################################
  10. # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.3.0
  11. # Generated On: 27/06/2009 9:17 PM
  12. # Generated By: Alan
  13. ########################################################################
  14.  
  15. #region Import the Assemblies
  16. [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  17. [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  18. #endregion
  19.  
  20. #region Generated Form Objects
  21. $vProfiles = New-Object System.Windows.Forms.Form
  22. $label3 = New-Object System.Windows.Forms.Label
  23. $label2 = New-Object System.Windows.Forms.Label
  24. $Disconnect = New-Object System.Windows.Forms.Button
  25. $Connect = New-Object System.Windows.Forms.Button
  26. $OutputWin = New-Object System.Windows.Forms.TextBox
  27. $CopyNetwork = New-Object System.Windows.Forms.Button
  28. $NewHost = New-Object System.Windows.Forms.ListBox
  29. $BaseHost = New-Object System.Windows.Forms.ListBox
  30. $label1 = New-Object System.Windows.Forms.Label
  31. $VCSRV = New-Object System.Windows.Forms.TextBox
  32. $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
  33. #endregion Generated Form Objects
  34.  
  35. #----------------------------------------------
  36. #Generated Event Script Blocks
  37. #----------------------------------------------
  38. #Provide Custom Code for events specified in PrimalForms.
  39. $Disconnect_OnClick=
  40. {
  41.         Disconnect-VIServer $ConVISRV -Confirm:$false
  42.         $Disconnect.Enabled = $false
  43.         $CopyNetwork.Enabled = $false
  44.         $OutputWin.Text += "`r`nDisconnect Successful"
  45.         $vProfiles.Refresh()
  46.        
  47. }
  48.  
  49. $handler_NewHost_SelectedIndexChanged=
  50. {
  51. #TODO: Place custom script here
  52.  
  53. }
  54.  
  55. $handler_button3_Click=
  56. {
  57.         $OutputWin.Text += "`r`nConnecting to $($Basehost.SelectedItem)"
  58.         $vProfiles.Refresh()
  59.         $CBASEHost = Get-VMHost -Name ($Basehost.SelectedItem)
  60.         $OutputWin.Text += "`r`nConnecting to $($Newhost.SelectedItem)"
  61.         $vProfiles.Refresh()
  62.         $CNEWHost = Get-VMHost -Name ($NewHost.SelectedItem)
  63.         $OutputWin.Text += "`r`nRetrieving Virtual Switches"
  64.         $vProfiles.Refresh()
  65.         $CBASEHost | Get-VirtualSwitch | Foreach {
  66.                 If (($CNEWHost | Get-VirtualSwitch -Name $_.Name -ErrorAction SilentlyContinue) -eq $null){
  67.                         $vSwitch = $_
  68.                         $OutputWin.Text += "`r`nCreating Virtual Switch $($_.Name)"
  69.                         $vProfiles.Refresh()
  70.                         $NewSwitch = $CNEWHost | New-VirtualSwitch -Name $_.Name -NumPorts $_.NumPorts -Mtu $_.Mtu
  71.                 }
  72.                 $_ | Get-VirtualPortGroup | Foreach {
  73.                         If (($CNEWHost | Get-VirtualPortGroup -Name $_.Name -ErrorAction SilentlyContinue) -eq $null){
  74.                                 $OutputWin.Text += "`r`n..Creating Portgroup $($_.Name) on $($vSwitch.Name)"
  75.                                 $vProfiles.Refresh()
  76.                                 $NewPortGroup = $CNEWHost | Get-VirtualSwitch -Name $vSwitch | New-VirtualPortGroup -Name $_.Name -VLanId $_.VLanID
  77.                         }
  78.                 }
  79.         }
  80.         $OutputWin.Text += "`r`n-- Configuration Completed --"
  81.         $vProfiles.Refresh()
  82.  
  83. }
  84.  
  85. $handler_label1_Click=
  86. {
  87. #TODO: Place custom script here
  88.  
  89. }
  90.  
  91. $handler_listBox1_SelectedIndexChanged=
  92. {
  93. #TODO: Place custom script here
  94.  
  95. }
  96.  
  97. $handler_button1_Click=
  98. {
  99.         If ($VCSRV.Text -eq "") {$OutputWin.Text = "Please Specify a VC Server above"}
  100.         Else
  101.         {
  102.                 $OutputWin.Text = "Connecting to $($VCSRV.Text)"
  103.                 $vProfiles.Refresh()
  104.                 $ConVISRV = Connect-VIServer $VCSRV.Text
  105.                 If ($ConVISRV.IsConnected -eq $true) {
  106.                         $OutputWin.Text += "`r`nConnection Successful"
  107.                         $vProfiles.Refresh()
  108.                         $Disconnect.Enabled = $true
  109.                         $CopyNetwork.Enabled = $true
  110.                         Foreach ($EsxHost in (Get-VMHost))
  111.                         {      
  112.                                 $BaseHost.Items.Add("$($EsxHost.Name)")|Out-Null
  113.                                 $NewHost.Items.Add("$($EsxHost.Name)")|Out-Null
  114.                         }              
  115.                 }
  116.                 Else {
  117.                         $OutputWin.Text += "`r`nConnection Un-Successful - Please try again."
  118.                         $vProfiles.Refresh()
  119.                 }
  120.         }
  121.  
  122. }
  123.  
  124. $OnLoadForm_StateCorrection=
  125. {#Correct the initial state of the form to prevent the .Net maximized form issue
  126.         $vProfiles.WindowState = $InitialFormWindowState
  127. }
  128.  
  129. #----------------------------------------------
  130. #region Generated Form Code
  131. $System_Drawing_Size = New-Object System.Drawing.Size
  132. $System_Drawing_Size.Width = 563
  133. $System_Drawing_Size.Height = 503
  134. $vProfiles.MaximumSize = $System_Drawing_Size
  135. $vProfiles.AutoSize = $True
  136. $vProfiles.Text = 'vProfiles'
  137. $vProfiles.ShowInTaskbar = $False
  138. $vProfiles.MaximizeBox = $False
  139. $vProfiles.Name = 'vProfiles'
  140. $vProfiles.SizeGripStyle = 2
  141. $vProfiles.ShowIcon = $False
  142. $System_Drawing_Size = New-Object System.Drawing.Size
  143. $System_Drawing_Size.Width = 563
  144. $System_Drawing_Size.Height = 503
  145. $vProfiles.MinimumSize = $System_Drawing_Size
  146. $vProfiles.StartPosition = 1
  147. $vProfiles.DataBindings.DefaultDataSourceUpdateMode = 0
  148. $System_Drawing_Size = New-Object System.Drawing.Size
  149. $System_Drawing_Size.Width = 547
  150. $System_Drawing_Size.Height = 465
  151. $vProfiles.ClientSize = $System_Drawing_Size
  152.  
  153. $label3.TabIndex = 11
  154. $System_Drawing_Size = New-Object System.Drawing.Size
  155. $System_Drawing_Size.Width = 100
  156. $System_Drawing_Size.Height = 13
  157. $label3.Size = $System_Drawing_Size
  158. $label3.Text = 'Destination:'
  159.  
  160. $System_Drawing_Point = New-Object System.Drawing.Point
  161. $System_Drawing_Point.X = 271
  162. $System_Drawing_Point.Y = 43
  163. $label3.Location = $System_Drawing_Point
  164. $label3.DataBindings.DefaultDataSourceUpdateMode = 0
  165. $label3.Name = 'label3'
  166. $label3.add_Click($handler_label3_Click)
  167.  
  168. $vProfiles.Controls.Add($label3)
  169.  
  170. $label2.TabIndex = 10
  171. $System_Drawing_Size = New-Object System.Drawing.Size
  172. $System_Drawing_Size.Width = 100
  173. $System_Drawing_Size.Height = 13
  174. $label2.Size = $System_Drawing_Size
  175. $label2.Text = 'Source:'
  176.  
  177. $System_Drawing_Point = New-Object System.Drawing.Point
  178. $System_Drawing_Point.X = 24
  179. $System_Drawing_Point.Y = 43
  180. $label2.Location = $System_Drawing_Point
  181. $label2.DataBindings.DefaultDataSourceUpdateMode = 0
  182. $label2.Name = 'label2'
  183.  
  184. $vProfiles.Controls.Add($label2)
  185.  
  186. $Disconnect.TabIndex = 9
  187. $Disconnect.Name = 'Disconnect'
  188. $Disconnect.Enabled = $False
  189. $System_Drawing_Size = New-Object System.Drawing.Size
  190. $System_Drawing_Size.Width = 75
  191. $System_Drawing_Size.Height = 23
  192. $Disconnect.Size = $System_Drawing_Size
  193. $Disconnect.UseVisualStyleBackColor = $True
  194.  
  195. $Disconnect.Text = 'Disconnect'
  196.  
  197. $System_Drawing_Point = New-Object System.Drawing.Point
  198. $System_Drawing_Point.X = 442
  199. $System_Drawing_Point.Y = 10
  200. $Disconnect.Location = $System_Drawing_Point
  201. $Disconnect.DataBindings.DefaultDataSourceUpdateMode = 0
  202. $Disconnect.add_Click($Disconnect_OnClick)
  203.  
  204. $vProfiles.Controls.Add($Disconnect)
  205.  
  206. $Connect.TabIndex = 8
  207. $Connect.Name = 'Connect'
  208. $System_Drawing_Size = New-Object System.Drawing.Size
  209. $System_Drawing_Size.Width = 75
  210. $System_Drawing_Size.Height = 23
  211. $Connect.Size = $System_Drawing_Size
  212. $Connect.UseVisualStyleBackColor = $True
  213.  
  214. $Connect.Text = 'Connect'
  215.  
  216. $System_Drawing_Point = New-Object System.Drawing.Point
  217. $System_Drawing_Point.X = 360
  218. $System_Drawing_Point.Y = 10
  219. $Connect.Location = $System_Drawing_Point
  220. $Connect.DataBindings.DefaultDataSourceUpdateMode = 0
  221. $Connect.add_Click($handler_button1_Click)
  222.  
  223. $vProfiles.Controls.Add($Connect)
  224.  
  225. $OutputWin.Multiline = $True
  226. $System_Drawing_Size = New-Object System.Drawing.Size
  227. $System_Drawing_Size.Width = 491
  228. $System_Drawing_Size.Height = 264
  229. $OutputWin.Size = $System_Drawing_Size
  230. $OutputWin.DataBindings.DefaultDataSourceUpdateMode = 0
  231. $OutputWin.AcceptsReturn = $True
  232. $OutputWin.ScrollBars = 2
  233. $OutputWin.Name = 'OutputWin'
  234. $System_Drawing_Point = New-Object System.Drawing.Point
  235. $System_Drawing_Point.X = 24
  236. $System_Drawing_Point.Y = 189
  237. $OutputWin.Location = $System_Drawing_Point
  238. $OutputWin.Enabled = $False
  239. $OutputWin.TabIndex = 7
  240.  
  241. $vProfiles.Controls.Add($OutputWin)
  242.  
  243. $CopyNetwork.TabIndex = 6
  244. $CopyNetwork.Name = 'CopyNetwork'
  245. $System_Drawing_Size = New-Object System.Drawing.Size
  246. $System_Drawing_Size.Width = 179
  247. $System_Drawing_Size.Height = 23
  248. $CopyNetwork.Size = $System_Drawing_Size
  249. $CopyNetwork.UseVisualStyleBackColor = $True
  250. $CopyNetwork.Enabled = $false
  251.  
  252. $CopyNetwork.Text = 'Copy vSwitches and PortGroups'
  253.  
  254. $System_Drawing_Point = New-Object System.Drawing.Point
  255. $System_Drawing_Point.X = 175
  256. $System_Drawing_Point.Y = 160
  257. $CopyNetwork.Location = $System_Drawing_Point
  258. $CopyNetwork.DataBindings.DefaultDataSourceUpdateMode = 0
  259. $CopyNetwork.add_Click($handler_button3_Click)
  260.  
  261. $vProfiles.Controls.Add($CopyNetwork)
  262.  
  263. $NewHost.FormattingEnabled = $True
  264. $System_Drawing_Size = New-Object System.Drawing.Size
  265. $System_Drawing_Size.Width = 244
  266. $System_Drawing_Size.Height = 95
  267. $NewHost.Size = $System_Drawing_Size
  268. $NewHost.DataBindings.DefaultDataSourceUpdateMode = 0
  269. $NewHost.ScrollAlwaysVisible = $True
  270. $NewHost.Name = 'NewHost'
  271. $System_Drawing_Point = New-Object System.Drawing.Point
  272. $System_Drawing_Point.X = 271
  273. $System_Drawing_Point.Y = 59
  274. $NewHost.Location = $System_Drawing_Point
  275. $NewHost.TabIndex = 5
  276. $NewHost.add_SelectedIndexChanged($handler_NewHost_SelectedIndexChanged)
  277.  
  278. $vProfiles.Controls.Add($NewHost)
  279.  
  280. $BaseHost.FormattingEnabled = $True
  281. $System_Drawing_Size = New-Object System.Drawing.Size
  282. $System_Drawing_Size.Width = 241
  283. $System_Drawing_Size.Height = 95
  284. $BaseHost.Size = $System_Drawing_Size
  285. $BaseHost.DataBindings.DefaultDataSourceUpdateMode = 0
  286. $BaseHost.ScrollAlwaysVisible = $True
  287. $BaseHost.Name = 'BaseHost'
  288. $System_Drawing_Point = New-Object System.Drawing.Point
  289. $System_Drawing_Point.X = 24
  290. $System_Drawing_Point.Y = 59
  291. $BaseHost.Location = $System_Drawing_Point
  292. $BaseHost.TabIndex = 4
  293. $BaseHost.add_SelectedIndexChanged($handler_listBox1_SelectedIndexChanged)
  294.  
  295. $vProfiles.Controls.Add($BaseHost)
  296.  
  297. $label1.TabIndex = 1
  298. $System_Drawing_Size = New-Object System.Drawing.Size
  299. $System_Drawing_Size.Width = 86
  300. $System_Drawing_Size.Height = 23
  301. $label1.Size = $System_Drawing_Size
  302. $label1.Text = 'Virtual Center:'
  303.  
  304. $System_Drawing_Point = New-Object System.Drawing.Point
  305. $System_Drawing_Point.X = 34
  306. $System_Drawing_Point.Y = 15
  307. $label1.Location = $System_Drawing_Point
  308. $label1.DataBindings.DefaultDataSourceUpdateMode = 0
  309. $label1.Name = 'label1'
  310. $label1.add_Click($handler_label1_Click)
  311.  
  312. $vProfiles.Controls.Add($label1)
  313.  
  314. $System_Drawing_Size = New-Object System.Drawing.Size
  315. $System_Drawing_Size.Width = 228
  316. $System_Drawing_Size.Height = 20
  317. $VCSRV.Size = $System_Drawing_Size
  318. $VCSRV.DataBindings.DefaultDataSourceUpdateMode = 0
  319. $VCSRV.Name = 'VCSRV'
  320. $System_Drawing_Point = New-Object System.Drawing.Point
  321. $System_Drawing_Point.X = 126
  322. $System_Drawing_Point.Y = 12
  323. $VCSRV.Location = $System_Drawing_Point
  324. $VCSRV.TabIndex = 0
  325.  
  326. $vProfiles.Controls.Add($VCSRV)
  327.  
  328. #endregion Generated Form Code
  329.  
  330. #Save the initial state of the form
  331. $InitialFormWindowState = $vProfiles.WindowState
  332. #Init the OnLoad event to correct the initial state of the form
  333. $vProfiles.add_Load($OnLoadForm_StateCorrection)
  334. #Show the Form
  335. $vProfiles.ShowDialog()| Out-Null
  336.  
  337. } #End Function
  338.  
  339. #Call the Function
  340. 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