VMware vSphere PowerCLI 是一款功能强大的命令行工具,可自动执行 vSphere 的各方面管理,包括主机、网络、存储、虚拟机、客户操作系统等等

打开打开的方法

第一种 

1.直接在ESXi控制台界面上按住ALT+F1也能进入

2.把ESXi的SSH功能开开,然后用工具连接行了,root/password。

  选择esxi主机----配置--(软件)安全配置-----(安全配置文件)属性---(ssh)选项     打开后远程root  密码 链接即可。

常用命令  

主机信息查询

vmware -v 查询系统版本

esxcli system version get   查询系统信息

esxcli system time get  查询时间

esxcli system time  set  -y=2016 -M=9 -d=13 -H=10 -m=9

esxcli system maintenanceMode set --enable   true/false     进入/退出维护模式

esxcli system maintenanceMode get  查询维护模式

esxcli system shutdown reboot/poweroff     关机/重启/断电

网络信息查询

esxcli network ip interface ipv4 get   查询ip

esxcli network ip route    ipv4 list      查询路由表

esxcli network nic list   查询网卡 up/down 信息

esxcli network nic down/up -n=vmnic1    关闭/打开一个网卡  -n=指定网卡

 

磁盘信息

esxcli storage core device list   查看磁盘列表

 

                                                               利用vshpere powercli 管理vcenter

安装powe rcli ( windows)

安装过程:

1、下载VMWare.PowerCLI  

Save-Module -Name VMware.PowerCLI    #执行后,会提示要输入path,它的目的是询问你将powercli软件下载到哪

 

虚拟机查看java版本 虚拟机查看版本命令_服务器

2、安装

install-module VMware.PowerCLI 

3、导入模块
Import-Module VMware.VimAutomation.Core

4  设置powercli的证书机制为ignore。

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

5 连接服务器,-user和-password参数不追加,会弹出对话框询问。

Connect-VIServer $vcenterip -User $user -Password $passwd -SaveCredentials

通过vsphere powercli 连接到vcenter server

connect-viserver -server "tw0dai.net" -username "administrator@vsphere.local" -password "3!@#"

6断开链接 Disconnect-VIServer $ip

第二种安装方法   安装程序需要Microsoft PowerShell 3.0的支持,安装过程中自动下载并安装PowerShell

下载VMware-PowerCLI-6.5.0-4624819.exe后将其复制到需要安装的服务器上。PowerCLI可以安装在vCenter上,也可以是其它服务器

双击安装文件后开始安装,会先安装VIX和相关插件

接着开始安装PowerCLI

安装完成后,桌面上会多出两个图标,运行VMware vSphere PowerCLI

虚拟机查看java版本 虚拟机查看版本命令_虚拟机查看java版本_02

首次运行可能会报错

虚拟机查看java版本 虚拟机查看版本命令_虚拟机查看java版本_03

get-executionpolicy           查看状态确实为Restricted

 

set-executionpolicy  remotesigned    将属性从Restricted更改为 remotesigned   y 确认  关闭窗口重新启动就正常了

 

使用   

 通过相关命令来管理虚拟化平台,在管理之前需添加服务器,命令如下

connect-viserver -server "tw0dai.net" -username "administrator@vsphere.local" -password "3!@#"

虚拟机查看java版本 虚拟机查看版本命令_服务器_04

 

三.常用命令列表
 1.connect-viserver  :连接虚拟化平台
 2.get-vmhost         :显示esx主机列表
 3.get-culster         :显示集群
 4.get-datastore     :显示存储列表
 5.get-resourcepool     :显示资源池
 6.get-vm             :显示虚拟机列表
 7.get-virtualswitch :显示虚拟交换机列表
 8.start-vm \stop-vm :启动或者关闭虚拟机
 9.new-vm              :新建虚拟机
 10.get-template     :显示模板
 11.get-oscustomizationspec    :显示自定义规范列表
 12.get-vapp         :显示vapp应用列表
 13.get-folder         :显示文件夹列表
 14.get-vmcommand     :显示所有命令列表

命令示范

1get-vmhost – 显示ESXi主机列表

PowerCLI C:\> get-vmhost

虚拟机查看java版本 虚拟机查看版本命令_VMware_05

2get-cluster – 显示群集列表

PowerCLI C:\> get-cluster

虚拟机查看java版本 虚拟机查看版本命令_VMware_06

3 get-datastore – 显示存储列表

PowerCLI C:\> get-datastore

虚拟机查看java版本 虚拟机查看版本命令_PowerCLI_07

4 get-resourcepool – 显示资源池

PowerCLI C:\> get-resourcepool

虚拟机查看java版本 虚拟机查看版本命令_虚拟机查看java版本_08

new-vm – 创建虚拟机

PowerCLI C:\> New-VM -Name test03 -ResourcePool Resources -VMHost 192.168.232.101 -Datastore datastore2 -NumCPU 2 -MemoryGB 4 -DiskGB 20 -NetworkName "VM Network" -Floppy -CD -DiskStorageFormat Thin -GuestID winNetDatacenterGuest

 

powercli 常用命令

help *vm*  查询vm相关的命令

help *virtualswitch*

help Get-VirtualSwitch

help *firewall*

查询

Get-Datacenter
Get-VMHost
Get-Cluster
Get-VM

Get-Cluster mycluster | Get-VMHost

Get-VM vm1 | Get-Datastore  识别虚拟机在哪个存储上

Get-Datastore 20storage | Get-VM  查询存储上有哪些虚拟机

显示更多的内容

Get-Datastore | Format-Table *  表格形式显示
Get-Datastore | Format-List *   列表形式显示

Get-VM | Format-List *

Get-VM AD-25 | Format-List *  某一个虚拟机AD-25的详细信息

Get-VM  | Select Name, Host, NumCpu, MemoryMB, HARestartPriority | Format-Table   筛选出关心的信息显示 

 

 

快照命令

Get-VM | Get-Snapshot  查询虚拟机快照

Get-VM | Get-Snapshot | Select Name, VM, Created 可以看出快照属于那个虚拟机  名字 创建时间等

 

查看群集或者主机上的虚拟机,方便接下来的快照创建
Get-Cluster "Cluster 01" | Get-VM
Get-VMHost 10.132.97.x | Get-VM

为ESXi主机10.132.X.X上每一台虚拟机创建一个快照,批量创建快照功能
Get-VMHost 10.132.97.19 | Get-VM | New-Snapshot -Name "Snap1“

help get-snapshot

get-vm | get-snapshot  | Where { $_.Created -lt (Get-Date).addDays(-1) } 筛选出某个日期之前的快照

Get-Snapshot AD-25 | Where { $_.Created -lt (Get-Date).addDays(-0) }  查询某个虚拟机此前的快照

get-vm | get-snapshot -name "Snap1" | remove-snapshot –confirm Y 批量删除快照

Get-Snapshot | Remove-Snapshot  删除所有快照

为Cluster里的所有VM升级VMware Tools:

Get-Cluster "Cluster_Name" | Get-VM | Update-Tools -NoReboot

 虚拟机迁移

move-vm

功能:把虚拟机移动到另一个位置

Get-VM -Name winxp | Move-VM -Destination 10.23.112.235 将虚拟机winxp从目前的位置移动到IP地址为10.23.112.235的虚拟机主机上。

Move-VM -VM winxp -Destination ResourcePool 移动虚拟机winxp到ResourcePool。 ESX主机不会改变。

remove-vm

功能:从vSphere删除服务器上指定的虚拟机。

Remove-VM winxp -DeletePermanently

set-vm

功能:更改虚拟机的配置

 

restart-vm

功能:重启虚拟机

Restart-VM -VM winxp -RunAsync -Confirm

关闭虚拟机

(1)Stop-VM -VM VM –Confirm

(2)Stop-VM -VM VM -Kill -Confirm:$false

启动虚拟机

Start-VM -VM winxp -Confirm -RunAsync

删除虚拟机

Remove-VM winxp -DeletePermanently