雲計算技術分享之如何在PowerCLI中使用EXCLI管理ESXi主機

ESXCLI命令用於管理ESXi主機的許多方面。你可以在SSH會話中以vCLI命令的身份運行ESXCLI命令,也可以在故障排除情況下在ESXiShell中運行它們。儘管VMware不斷髮展PowerCLI cmdlet庫,但使用ESXCLI仍然可以做很多事情,而PowerCLI無法做到這一點。值得慶幸的是,PowerCLI模塊包含Get-EsxCLI cmdlet,允許你通過PowerShell運行esxcli命令。

雲計算技術分享之如何在PowerCLI中使用EXCLI管理ESXi主機

要使用它,你需要添加主機作為參數並將結果存儲在變量中。不要忘記添加-V2開關。-V2開關在PowerCLI 6.3R1中引入,使其使用更加容易。

Invoke() method

在開始之前,需要介紹這兩種方法。此方法用於針對主機啟動esxcli命令。它與在esxcli命令上按下ESXi shell中的enter相當。當命令的選項需要強制參數時,有時需要參數。

1. CreateArgs()method

當Invoke()方法需要參數時,對象上可以使用CreateArgs()方法。當使用它創建“參數變量”時,它將創建一個哈希表,其中包含了可以傳遞給命令的所有參數。當你不熟悉它時非常方便。那麼你需要使用Invoke()方法和括號之間的參數變量,如$ esxcli ...... Invoke($ MyArgs)。

如何使用

在本示例中,將檢查主機上是否啟用了IPv6。執行此操作的esxcli命令如下:

esxcli system module parameterslist -m tcpip4


創建EsxCLI變量

首先需要在參數和V2開關中運行帶有主機的Get-EsxCLIcmdlet,並將輸出存儲在變量中。如果查看$esxcli變量的對象類型,將看到它具有自己的類型EsxCliImpl。

$esxcli = Get-EsxCLI -VMhost ESX1-V2

If you display thecontent of the variable you will see the same options you would get in theshell of a host.

PS> $esxcli

==================================

EsxCli: mg-p-esxcs31.mgmtdom.intra

Elements:

———

device

elxfc

elxfcoe

elxnet

esxcli

fcoe

graphics

hardware

iscsi

network

nvme

rdma

sched

software

storage

system

vm

vsan

可以瀏覽對象的屬性。每當在屬性上按Enter鍵時,它將顯示一個信息表。

Method Elements方法元素:你可以去哪裡(property or esxcli option)

PS> $esxcli.system.module.parameters

=========================

EsxCliElement: parameters

Method Elements:

———

copy

list

set

Methods:

——–

string Help()

Methods : 可以做的包括,Invoke()命令是否需要參數。在這種情況下,必須使用CreateArgs()方法創建參數對象。下面你可以看到list元素的invoke方法確實需要和參數hash表。

PS> $esxcli.system.module.parameters.list

===================

EsxCliMethodElement: list

Methods:

——–

Hashtable CreateArgs()

ModuleParameter[] Invoke(Hashtable args)

string Help()

2. Create thearguments variable創建參數變量

複製了前面提到的$ esxcli變量中的esxcli命令。我們還看到list元素的Invoke()方法需要參數。這是我們在這個$ arguments變量中創建的。

$arguments =$esxcli.system.module.parameters.list.CreateArgs()

如果查看其內容,將看到可以設置的所有屬性及其預期的類型。此列表元素僅採用模塊參數。

PS> $arguments

Name Value

—- —–

module Unset, ([string])

3. Configure thearguments variable配置參數變量

現在我們需要根據esxcli命令設置模塊參數(esxcli system module parameterslist -m tcpip4).

$arguments.module = “tcpip4”

4. Invoke thecommand with arguments使用參數調用命令

現在是時候開始真正的命令了。剩下要做的就是使用參數變量作為參數運行invoke方法,它將顯示與esxcli相同的內容。下面我們可以看到ipv6被禁用了。

PS> $esxcli.system.module.parameters.list.Invoke($arguments)

Description

Name

Type Value

# of provisioned H/W iSCSI ports

ipportIscsiReserved

int

Enable/Disable IPv6

ipv6

int 0

結束語

這個例子很簡單,但沒有那麼複雜。使用時可能必須指定更多參數,但這是關於它的。你可以在vSphere命令行界面參考中找到有關esxcli命令的所有信息。


分享到:


相關文章: