Computer >> Hướng Dẫn Máy Tính >  >> Phần Mềm >> Máy Ảo

VMware PowerCLI: Cách cài đặt và quản lý vSphere và ESXi

VMware vSphere PowerCLI là một tập hợp module PowerShell để quản lý hạ tầng VMware từ dòng lệnh. Bạn có thể dùng PowerCLI để lấy trạng thái máy ảo, datastore, mạng, hoặc người dùng, quản lý các cài đặt khác nhau, tạo đối tượng mới, và dùng PowerShell trong tác vụ tự động hóa cho ESXi, vCenter Server, vSphere, vSAN, và các sản phẩm VMware khác.

Hiện tại, VMware đang phát triển phiên bản PowerCLI cho PowerShell Core (7.0+) chỉ. Phiên bản PowerShell này đa nền tảng và có thể dùng trong Windows, Linux, hoặc macOS. Chỉ bản vá bảo mật được phát hành cho các module PowerCLI trước cho Windows PowerShell 5.x. Khuyến khích dùng phiên bản PowerShell Core cập nhật.

Cách cài đặt module VMware PowerCLI trên Windows?

Bạn có thể cài đặt VMware PowerCLI trên Windows online hoặc offline.

Để cài PowerCLI từ PowerShell Gallery online, chạy lệnh dưới:

Install-Module -Name VMware.PowerCLI

PowerCLI vSphere phiên bản 6.5 trở về trước được phân phối dưới dạng installer MSI.

Nếu bạn muốn cài module cho user hiện tại:

Install-Module VMware.PowerCLI -Scope CurrentUser

Mặc định, phiên bản PowerCLI mới nhất được cài từ PowerShell Gallery. Nếu muốn cài phiên bản PowerCLI cụ thể, bạn có thể hiển thị danh sách đầy đủ các phiên bản có sẵn và cài phiên bản bạn muốn:

Find-Module -Name VMware.PowerCLI -AllVersions|select version
Install-Module -Name VMware.PowerCLI -RequiredVersion 12.4.0.17860403

VMware PowerCLI: Cách cài đặt và quản lý vSphere và ESXi

Trong quá trình cài đặt, bạn sẽ được nhắc cập nhật NuGet provider. Cũng xác nhận cài đặt từ repository PowerShell Gallery không tin cậy. Bạn có thể làm nó thành trusted dùng cmdlet Set_PSRepository.

Kiểm tra module PowerCLI đã cài và hiển thị phiên bản:

Get-Module -ListAvailable VMware* | Select Name,version

VMware PowerCLI: Cách cài đặt và quản lý vSphere và ESXi

Để cập nhật phiên bản module PowerCLI, chỉ chạy lệnh:

Update-Module -Name VMware.PowerCLI

Nếu bạn cài module quản lý gói Choco trên Windows, bạn có thể tìm module PowerCLI trong repository:
choco search vmware
và cài nó:
choco install vmware-powercli-psmodule

Module PowerCLI chưa có trong winget.

Nếu bạn muốn cài PowerCLI trên máy cách ly (môi trường disconnected), bạn có thể dùng offline installer.

  1. Tải file ZIP chứa module PowerCLI https://code.vmware.com/web/tool/vmware-powercli (kích thước ZIP với VMware-PowerCLI 12.4.1 hiện tại khoảng 80 MB);
  2. Giải nén archive vào thư mục module PowerShell trên máy (có thể lấy danh sách thư mục module PoSh dùng $env:PSModulePath). Thường dùng C:\Program Files\WindowsPowerShell\Modules.
  3. Unblock file tải từ Internet:
    cd “c:\Program Files\WindowsPowerShell\Modules”
    Get-ChildItem * -Recurse | Unblock-File
  4. Mở console PowerShell và đảm bảo module PowerCLI khả dụng: Get-Module -Name VMware.PowerCLI –ListAvailable
Đọc thêm về cài đặt offline module PowerShell.

Để import module PowerCLI vào session PowerShell ISE hoặc Visual Studio Code hiện tại, chạy lệnh:

Import-Module VMware.VimAutomation.Core

Bạn có thể thấy thông báo lỗi khi nạp module:

Import-Module: File C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Sdk\12.4.0.18627054\VMware.VimAutomation.Sdk.psm1 cannot be loaded because running scripts is disabled on this system.

Lúc đó bạn phải cấu hình chính sách thực thi script PowerShell. Để cho phép dùng module trong session hiện tại:

Set-ExecutionPolicy Unrestricted -Scope Process

Bạn có thể dùng module PowerCLI cài trên máy từ xa. Để làm vậy, import nó từ xa dùng PSRemoting:

$session = New-PSSession -ComputerName mun-svr01

Import module PowerCLI vào máy của bạn:

Import-Module -PSsession $session -Name VMware.VimAutomation.Core

Đừng quên đóng session khi xong:
Remove-PSSession $session

Cài đặt module PowerCLI trên Linux

Bạn có thể cài và dùng PowerCLI cả trong Windows và Linux. Trước hết, cài PowerShell Core trên máy chủ Linux. Sau đó chạy các lệnh sau trong console để cài VMware PowerCLI:

pwsh
Install-Module -Name VMware.PowerCLI

VMware PowerCLI: Cách cài đặt và quản lý vSphere và ESXi

Import module vào session:

Import-Module VMware.PowerCLI

Lệnh PowerCLI cơ bản quản lý VMware vSphere và VM

Bạn có thể lấy danh sách cmdlet trong module PowerCLI như sau (hơn 2200 lệnh có sẵn):

Get-Command –Module *vmware*

VMware PowerCLI: Cách cài đặt và quản lý vSphere và ESXi

Để tắt thông báo VMware Customer Experience Improvement Program (CEIP):

Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCeip $false

Để kết nối với vCenter Server hoặc ESXi host:

Connect-VIServer <vCenter_or_ESXi_FQDN>

Nếu bạn dùng self-signed SSL certificate cho vCenter Server, PowerCLI sẽ chặn kết nối:
Connect-VIServer Error: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction option to Prompt if you'd like to connect once or to add a permanent exception for this server. Additional Information: Could not establish trust relationship for the SSL/TLS secure channel with authority.

Để bỏ qua self-signed certificate:

Set-PowerCLIConfiguration -Scope AllUsers -InvalidCertificateAction Warn

Để hiển thị danh sách máy ảo đăng ký trên ESXi (hoặc vCenter) server:

Get–VM

Để chỉ hiện VM tắt:

Get-VM | Where {$_.Powerstate -ne “PoweredOn”} | Select Name, VMHost, NumCPU, MemoryMB, Version|Format-Table

VMware PowerCLI: Cách cài đặt và quản lý vSphere và ESXi

Để bật máy ảo:

Start-VM -VM MUNTestVM1

Để khởi động lại VM đúng cách (dùng VMware tools agent):

Restart-VMGuest -VM MunTestVM1 -Confirm:$False

Để tắt VM:

Shutdown-VMGuest -VM MunTestVM1 -Confirm:$False

Để hiển thị danh sách snapshot cho VM:

Get-VM -VM MunTestVM1 | Get-Snapshot| Format-List

Để di chuyển VM đang chạy sang host khác bằng vMotion, lệnh Move-VM dùng. Ví dụ, bạn muốn di chuyển tất cả VM từ mun-esxi1 sang mun-esxi2:

Get-VMHost mun-esxi1|Get-Vm| Move-VM –Destination (Get-VMHost mun-esxi2)

Để tạo máy ảo mới, dùng cmdlet New-VM:

New-VM –Name MunTestVM1 -VMHost mun-esxi1 –ResourcePool Production –DiskGB 20 –DiskStorageFormat Thin –Datastore MUN_MSA2000_Prod1

Dùng cmdlet Set-VM để thay đổi cài đặt máy ảo.

Dùng cmdlet PowerCLI, bạn có thể tương tác với hệ điều hành khách của máy ảo. Để làm vậy, VMware Tools phải cài trong VM. Bạn có thể cập nhật VMware Tools như dưới:

Get-VMGuest MunTestVM1 | Update-Tools

Dùng Invoke-VMScript, bạn có thể chạy script hoặc chương trình trong guest Windows OS:

$script = '"%programfiles%\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /report "%tmp%\inforeport"'
Invoke-VMScript -ScriptText $script -VM MunTestVM1 -HostCredential $hostCred -GuestCredential $VMCred -ScriptType Bat

Để copy file đến tất cả VM, chạy lệnh:
Get-VM | Copy-VMGuestFile -Source C:\PS\get-size.ps1 -Destination C:\PS\ - LocalToGuest -GuestUser administrator -GuestPassword P@ssdr0w2

Module chứa cmdlet quản lý cụm, datacenter, datastore, và ESXi host:

Get-VMHost
Get-Datacenter
Get-Cluster
Get-Datastore
Get-VirtualPortGroup

Để hiển thị danh sách ESXi host trong cụm:

Get-Cluster munprod1 | Get-VMHost | sort name

Đưa ESXi host cụ thể vào maintenance mode:
Set-VMhost -VMHost mun-esxi1 -State Maintenance

Để kết nối iSCSI LUN với ESXi host và tạo VMFS datastore trên nó:
Get-ScsiLun -VmHost mun-esxi1 | ft
New-Datastore -Name 'mun_iscsi_datastore' -VMHost mun-esxi1 -Path naa.6000xxxxxxxxxxxxxxxx -Vmfs

Mẹo. PowerCLI hoạt động ở chế độ read-only với VMware vSphere Hypervisor miễn phí. Điều này có nghĩa là bạn có thể dùng lệnh xem cài đặt host và VM, nhưng không thể thay đổi chúng.

Ví dụ script PowerCLI

Có một số ví dụ dùng PowerCLI để lấy thông tin về mục hạ tầng VMware và tự động hóa một số hành động trên trang web. Bạn có thể dùng ví dụ từ các bài viết sau để viết script PowerCLI của mình:

  • Kiểm tra không gian ổ đĩa trống trên VMFS Datastore
  • Map VMware Virtual Disks (VMDK Files) và Windows Drive Volumes
  • Cách tìm VMware VMs bằng IP hoặc MAC Address?
  • Cấu hình SNMP trên VMware ESXi Hosts
  • Cách Force Restart VMware VM bị kẹt?
  • Cách tự động hóa Windows Updates trong VMware Templates?
  • Tiêm driver vào ESXi Installation Image
  • VMware VM Consolidation Needed Status Alarm
  • Nâng cấp VM Hardware Version

vSphere PowerCLI là công cụ mạnh để quản lý và tự động hóa hạ tầng VMware. PowerCLI bao gồm hơn 2000 cmdlet quản lý hạ tầng VMware cloud và on-premises (vSphere,vSAN, vRealize Operations Manager, vCloud Director, Site Recovery Manager, Horizon, NSX-T, VMware Cloud Services, VMware Cloud on AWS, v.v.).