📜 ⬆️ ⬇️

A simple GUI utility to manage standard vSwitch in VMWare vSphere via PowerCLI

Good day, dear readers of Habr!

I would like to share with you a simple and, in my opinion, a convenient way to manage virtual switches in a VMWare vSphere infrastructure, without using a vSphere (vSphere Distributed Switch).
Someone does not use vDS because they do not see the need for them. Someone because he is uncomfortable. Someone because there is no license - because vDS requires Enterprise Plus vSphere license.

We also use conventional vSwitch so far, despite the presence of more than 70 ESXi hypervisors in the infrastructure. And it so happened that during the time of working with them, I had accumulated a lot of PowerCLI scripts to automatically configure certain functions. And I wanted to combine them into something more, making a convenient tool for other administrators. After a while, I came across the Primal Forms package (now called PowerShell studio, there is a stripped-down Community Edition) and I realized that the next step would be a GUI. Primal Forms allows you to create GUIs using PowerShell itself, however, greatly simplifying this process is as simple as drawing a GUI in the editor, and then simply clicking on a button or other controls to add code.
')
The result is a script utility that allows you to:


Plus some extra buns:
Doing all this manually on each host is not only tedious, but also rather stupid. Of course, you can use host profiles, but for this you need to translate each host in maintance mode. Yes, and those who worked with host profiles can confirm that the process of applying the profile is not at all quick.
Code samples and the utility itself under the cat.

Through Primal Forms I sketched the interface, with buttons for each action. I would like to add firewall management and, for example, view snapshots of all hosts at once, therefore, with tabs, it is a reserve for the future. The default values ​​are pre-filled in the fields, below is the status bar showing information about the connection with vCenter and the completion of the task.
image
Screw the code to connect to the virtual infrastructure:

$buttonConnectToVcenter_Click={ If ($vcenter_address.Text -eq "") {[System.Windows.Forms.MessageBox]::Show("Please specify a VC Server")} Else { $app_statusbar.Text="Connecting" //  [System.Windows.Forms.Application]::DoEvents() //     //     VMWare Snapin'. Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue Add-PSSnapin VMware.VumAutomation -ErrorAction SilentlyContinue $VC = Connect-VIServer $vcenter_address.Text If ($VC.IsConnected -eq $true) { $vc_connected=$TRUE $app_statusbar.Text="Connected" [System.Windows.Forms.MessageBox]::Show("Connected to "+$vcenter_address.Text) } else { [System.Windows.Forms.MessageBox]::Show("Connection FAILED - Please try again.") $vc_connected=$FALSE // .     $vc_connected,         vcenter. $app_statusbar.Text="Connection failed" } } } 


Then everything is by analogy. Button - reaction to the event. For example, add a port group.
 $port_group_add_Click={ if ($vc_connected -eq $FALSE) { [System.Windows.Forms.MessageBox]::Show("I am not connected to vCenter") //,   ? } if ($vc_connected -eq $TRUE) { //,      ? If (($switch_name.Text -eq "") -or ($port_group_name_add.Text -eq "") -or ($port_group_vlan_id_add.Text -eq "")) {[System.Windows.Forms.MessageBox]::Show("Parameters missing")} Else { $app_statusbar.Text="Adding port group..." $esxhosts= Get-VMHost * $progress.Value=0 $progress_tick=((1/$esxhosts.Count)*100) foreach ($esxhost in $esxhosts) { [System.Windows.Forms.Application]::DoEvents() $hypervisor = Get-VMHost $esxhost | Get-VirtualSwitch -Name $switch_name.Text $hypervisor | New-VirtualPortGroup -Name $port_group_name_add.Text -VLanId $port_group_vlan_id_add.Text $progress.Value=$progress_tick $progress_tick=$progress_tick+((1/$esxhosts.Count)*100) // progress bar } $progress.Value=100 $app_statusbar.Text="Done!" } } } 

I would not like to turn the post into a stupid code listing, so only a couple of functions are listed. If you are interested to see it entirely - skalte immediately down, there is a link to pastebin. Unfortunately, I'm not a programmer, it works and it’s okay. Maybe someone will pick up a couple of ideas from it or be able to do much better.
How it works?

After installing VMWare PowerCLI, the administrator has access to mdlets for working with vSphere. If there is a working PowerShell and the installed PowerCLI - the script should already work (by the way, even Primal Forms allows you to create an exe). Then PowerCLI will take care of authorization and interaction with vSphere.
At startup, a window should appear:
image
Log in to vCenter.
image

Let's create a trial port group “test”.
image
And remove it
image
By analogy with the other functions.

The source code is on pastebin .
And collected in Primal Forms exe 's depositfiles .

In general - PowerCLI in conjunction with PowerShell provide tremendous opportunities for automation, management and monitoring. For example, a vCenter can be screwed with a plug-in that displays a html-report on the state of the virtual infrastructure in a separate tab, which is created every morning with PowerCLI and displays information about:


Finally, I would like to thank the vmware communities forums for valuable hints and tips, as well as Mikhail Mikheev ( michigun ), through whose book I began to explore such a popular virtualization platform like VMWare vSphere, and Maxim Moshkov - an excellent teacher from the HP Training Center for informative VMWare courses, thanks to which we managed to pass on VCP410 and VCP510, and indeed, learn a lot of new things.

Source: https://habr.com/ru/post/148052/


All Articles