📜 ⬆️ ⬇️

VMware PowerCLI: We manage the clouds and everything from the command line:

advbg
image

About the routine and struggle with it


We all love VMware products! In addition to all of our beloved Workstation, WMware offers the most powerful virtualization tools at the corporate and multi-corporate level.

Having several ESXi hypervisors using vCenter Server installed, for example, on one of the virtual machines living on one of the same hypervisors, we can quite easily manage all connected hypervisors, manage all resources, create, delete, edit virtual machines, etc. d. We can connect, for example, using the vSphere Client and get a pretty nice view of our entire virtual infrastructure.
')
True, at one moment, this infrastructure can become very large and very heavy. Any kind of administration at one point may take too much time and take too much effort. Some tasks are performed periodically, which is a little annoying.

A rather reasonable question arises, and what of all this can be scripted and how? This is where PowerCLI comes to the rescue.

PowerCLI is an extension for Windows Powershell that adds over 400 new cmdlets to manage Virtual Infrastructure, including the Cloud. You can download the product from here communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli . There are also links to documentation, blog and community.

Something about Powershell


After installing the distribution, run the PowerCLi shortcut. A console opens, which is essentially a full-featured Powershell, but with a loaded SnapIn to manage the Virtual Infrastructure. You can simply open Powershell and load snapins from the folder where PowerCLI is installed.

Briefly recall the syntax of Powershell. Commands in it are formed as follows:

Verb-Noun –Param1 String [[-Param2] int ] [-Param3 short ] [-SwitchParam] [-EnumParam { Val1 | .. | ValN ]} [ CommonParameters ]
Verb - as follows from the definition, this is an action that needs to be performed on Noun by submitting parameters by name or by position. The result of the execution will be a .Net object. Further, these objects are recorded in variables or you can submit them via pipeline to some cmdlet. For example:
Get-Process -name notepad | Stop-Process

Here we take all the processes named notepad using the Get-Process cmdlet and pass it along the pipeline to the Stop-Process cmdlet , which stops them.

How it works in PowerCli


We apply this philosophy to manage our infrastructure:

#
PS> $vm = Get-VM -Name DemoMachine
#
PS> $vm
Name PowerState Num CPUs Memory (MB)
---- ---------- -------- -----------
DemoMachine PoweredOff 1 256


We can complicate the command and show other information. As in the example with notepad, we take a virtual machine, transfer it along the pipeline to the Select-Object, which takes some of the fields of the virtual machine object and transfer the result to the Format-Table, which formats the output.

PS> $vm | Select-Object Name,VMHost,Version | Format-Table -AutoSize
Name VMHost Version
---- ------ -------
DemoMachine 10.23.83.210 v7


Now let's try to change something. Suppose we have added memory on the host machines. One line we can upgrade all virtual machines, for example, in the marketing department:

PS> Get-VM –Location MarketingPool | Set-VM –MemoryMB 512 –Confirm:$false
Name PowerState Num CPUs Memory (MB)
---- ---------- -------- -----------
MarketingVM05 PoweredOff 1 512
MarketingVM04 PoweredOff 1 512
MarketingVM03 PoweredOff 1 512
MarketingVM01 PoweredOff 1 512
MarketingVM02 PoweredOff 1 512


Consider a few more eloquent examples:
#
PS> Get-VM -Location DevelopmentPool | New-Snapshot -Name "ClearState"

#
PS> Get-VM -Location DevelopmentPool | Set-VM -Snapshot "ClearState"

#
PS> Get-VM -Location DevelopmentPool | Get-Snapshot -Name "ClearState" | Remove-Snapshot

# datastore-
PS> Get-Datastore | Select Name,CapacityGB,FreespaceGB | Format-Table -AutoSize

#
PS> Get-VM -Location DevelopmentPool | Get-HardDisk | Select Parent,Name,CapacityKB,StorageFormat

# ESX(i)
PS> Get-VMHost | Select Name,Manufacturer,Model,NumCpu,MemoryTotalMB,CpuTotalMhz,ApiVersion | ft -AutoSize


Now fashionable word cloud ...


These commands were executed on vSphere, but as I said in a similar way, we can manage the Cloud infrastructure. For this, there is a set of commandlets.

Having connected to the cloud, we are interested in what virtual machines we have. We carry out:

PS> Get-CIVM | Select Name
Name
----
CloudVM03
CloudVM04
CloudVM01
CloudVM02
CloudVM05


And finally, we can execute commands on both the vSphere and the Cloud at the same time.

In one line, we can move virtual machines to the Cloud:

PS> Get-VM –Location AccountingPool | Import-CIVapp –OrgVdc AccountingVdc
Name Enabled InMaintenanceMode Owner
---- ------- ----------------- -----
AccountingVM03 True False system
AccountingVM04 True False system
AccountingVM01 True False system
AccountingVM02 True False system
AccountingVM05 True False system

Like this! Everything is very simple!

Finally, a bit of magic


Using cmdlets, you can interact with the guest operating system. To do this, it must be installed VMware Tools. You can upgrade them as follows:

PS> Get-VMGuest VM | Update-Tools

What does this interaction include? For example, we want to rewrite a certain file on all our virtual machines (in the case of Windows) to a specific place:

PS> Get-VM | Copy-VMGuestFile -Source c:\text.txt -Destination c:\temp\ -GuestToLocal -HostUser root -HostPassword pass1 -GuestUser user -GuestPassword pass2

And the most interesting - we execute the script on the guest OS:

PS>$script = '"%programfiles%\Common Files\Microsoft Shared\MSInfo\msinfo32.e
xe" /report "%tmp%\inforeport"'
Invoke-VMScript -ScriptText $script -VM MyVM -HostCredential $hostCredential -GuestCredential $guestCredential -ScriptType Bat


The cmdlet allows the execution of bat / bash / powershell scripts.

I’m sure most of you have already felt the full power of this interaction! With their help, you can create, for example, powerful post-provisioning configurations.

Conclusion


So, PowerCLI is a fairly powerful tool for administering the entire Virtual Infrastructure. You can create complex scripts for all sorts of manipulations on it. These scenarios can, for example, run periodically or be used to lift the environment. You can also create scripts for disaster recovery. The properties and approach of Powershell make it quite easy - a small number of commands to manipulate a large number of objects.

Under the above link you can get full instructions on the use of this product. You can also ask about the problems you are interested in in the community. It is one of the largest for powershell at all.

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


All Articles