VMware PowerCLI安装以及使用

VMware PowerCLI 在Ubuntu 系统中安装以及使用说明

VMware PowerCLI安装以及使用

在Ubuntu 18.04安装powershell

PowerShell for Linux is published to package repositories for easy installation and updates. The URL to the package varies by OS version:

  • Ubuntu 20.04 - https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
  • Ubuntu 18.04 - https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

Use the following shell commands to install PowerShell on the target OS. Change the URL to match the version of the target OS.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh

As superuser, register the Microsoft repository once. After registration, you can update PowerShell with sudo apt-get install powershell.

离线安装PowerCLI

在https://developer.vmware.com/web/tool/vmware-powercli下载离线zip包

(Optional) If you need to install PowerCLI offline, download the PowerCLI ZIP file from the PowerCLI home page and transfer the ZIP file to your local machine.

  1. Check the PowerShell Module path by using the command: $env:PSModulePath.
  2. Extract the contents of the ZIP file to one of the listed folders.

You can verify that the PowerCLI module is available by using the command Get-Module -Name VMware.PowerCLI -ListAvailable.

配置忽略证书验证

1
Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCeip $false -InvalidCertificateAction Ignore

常见PowerCLI命令

连接esxi或者vCenter

1
Connect-VIServer  -Server godvcsa.gzhc.local -User administrator@vsphere.local -password 'password'

查看物理机序列号

1
2
Get-VMHost  | Select-Object Name,Manufacturer, Model, @{N="Serial Number";E={(Get-EsxCli -VMHost $_.Name).hardware.platform.get().SerialNumber}}, PowerState | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture ##导出为csv
Get-VMHost  | Select-Object Name,Manufacturer, Model, @{N="Serial Number";E={(Get-EsxCli -VMHost $_.Name).hardware.platform.get().SerialNumber}}, PowerState | Format-Table -AutoSize ## 直接打印

通过vmtools设置虚拟机IP

$datas = Import-CSV C:\11.csv
foreach ($data in $datas){ 
$VM =   Get-VM  $data.vm
$IP = $data.ip 
Invoke-VMScript -VM $VM -ScriptText "echo DEVICE=eth0$'\n'ONBOOT=yes$'\n'IPADDR=$ip$'\n'NETMASK=255.255.255.0$'\n'GATEWAY=192.168.117.1> /etc/sysconfig/network-scripts/ifcfg-eth0;service network restart" -GuestUser root -GuestPassword &jx723j%^Bxxxxd1 -RunAsync  
}

修改ESXi NTP设置

步骤解读:第一个代码块是连接VC,第二个是获取你本机的当前时间设置到ESXi主机,第三个是设置NTP并重启服务与设置开机自启,第四个是查询下各主机时间。

Connect-VIServer  -Server godvcsa.gzhc.local -User administrator@vsphere.local -password 'password'

Foreach($HostIP in Get-VMHost){
	Add-VmHostNtpServer -NtpServer "ntp.aliyun.com" -VMHost $HostIP
	Add-VmHostNtpServer -NtpServer "ntp1.aliyun.com" -VMHost $HostIP
	Get-VMHostService -VMHost $HostIP | where { $_.Key -eq "ntpd" } | Start-VMHostService
	Get-VMHostService -VMHost $HostIP | where { $_.Key -eq "ntpd" } | Set-VMHostService -Policy "on" -Confirm:$false
}
#查询主机时间
Foreach($HostIP in Get-VMHost){
	$esxcli = Get-EsxCli -VMHost $HostIP -V2
	$esxcli.system.time.get.Invoke()
}
updatedupdated2021-12-132021-12-13