更新 PowerCLI

执行以下命令来更新 PowerCLI。

Update-Module -Name VMware.PowerCLI

使用 VMware PowerCLI管理 vSphere

连接到vCenter Server

今天连接VCenter Server 时候,出现错误:

报错Connect-VIServer : 在模块“VMware.VimAutomation.Core”中找到“Connect-VIServer”命令,但无法加载该模块。有关详细信息,请运行“Import-Module VMware.VimAutomation.Core"

原来昨天操作有误,并没有导入模块成功。今天再次尝试:

导入模块

Get-Module VMware* -ListAvailable | Import-Module

出现报错

PowerCLI 学习笔记2——使用PowerCLI连接到vCenter_VMware

PS C:\Program Files\WindowsPowerShell\Modules> Get-Module VMware* -ListAvailable | Import-Module
Import-Module : 无法加载文件 C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Sdk\VMware.VimAutomation.Sdk.psm1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ Get-Module VMware* -ListAvailable | Import-Module
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [Import-Module],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand

更改默认策略:

PS C:\Program Files\WindowsPowerShell\Modules> Set-ExecutionPolicy RemoteSigned

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 http://go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): Y

再次导入模块

PS C:\Program Files\WindowsPowerShell\Modules> Get-Module VMware* -ListAvailable | Import-Module
警告: Please consider joining the VMware Customer Experience Improvement Program, so you can help us make PowerCLI a better product. You can join using the following command:

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true

VMware's Customer Experience Improvement Program ("CEIP") provides VMware with information that enables VMware to improve its products and services, to fix problems, and to advise you on how best to deploy and use our
products.  As part of the CEIP, VMware collects technical information about your organization鈥檚 use of VMware products and services on a regular basis in association with your organization鈥檚 VMware license key(s).  This
 information does not personally identify any individual.

For more details: type "help about_ceip" to see the related help article.

To disable this warning and set your preference use the following command and restart PowerShell:
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true or $false.
          Welcome to VMware PowerCLI!

Log in to a vCenter Server or ESX host:              Connect-VIServer
To find out what commands are available, type:       Get-VICommand
To show searchable help for all PowerCLI commands:   Get-PowerCLIHelp
Once you've connected, display all virtual machines: Get-VM
If you need more help, visit the PowerCLI community: Get-PowerCLICommunity

       Copyright (C) VMware, Inc. All rights reserved.


PS C:\Program Files\WindowsPowerShell\Modules>

再次连接到vCenter Server

Connect-VIServer -Server vcsa7.brkj.com -Protocol http -User 'administrator@vsphere.local' -Password 'Zzbrkm2409@'

 由于是第一次连接沓vcenter,出现报错“无法为 SSL/TLS 安全通道与颁发机构“vcsa7.brkj.com”建立信任关系。”

PowerCLI 学习笔记2——使用PowerCLI连接到vCenter_PowerCLI_02

解决办法是设置忽略证书安全信息。

Get-PowerCLIConfiguration
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

PowerCLI 学习笔记2——使用PowerCLI连接到vCenter_PowerCLI_03

再次尝试:

Connect-VIServer -Server vcsa7.brkj.com -Protocol https -User 'administrator@vsphere.local' -Password 'Zzbrkm2409@'

PowerCLI 学习笔记2——使用PowerCLI连接到vCenter_Server_04

连接成功!