VMware PowerCLI vCenter批量修改esxi主机root密码
测试环境
1、VMware ESXi-7.0.2-17630552
2、VMware vCenter 7.0.2-17630552
3、Windows Server2019
一、VMware PowerCLI安装
通过在线安装或者离线安装方式操作:
在线安装
可以直接从 PowerShell Gallery安装
Install-Module -Name VMware.PowerCLI
离线安装
1、下载PowerCLI Module.zip文件,此次测试环境版本是VMware-PowerCLI-12.2.0-17538434
2、下载地址:https://developer.vmware.com/web/tool/12.6.0/vmware-powercli
3、检查PowerShell模块路径,执行以下命令(本实验在server2019 powershell中输入)
$env:PSModulePath
二、 将下载的.zip文件解压缩到PowerShell模块路径
Powershell执行以下命令以验证PowerCLI模块是否可用
Get-Module -Name VMware.PowerCLI -ListAvailable
使用Powershell导入模块
对于Powershell,默认禁止运行脚本,需要开放
set-ExecutionPolicy RemoteSigned
导入模块
Import-Module VMware.PowerCLI
三、编写批量修改esxi主机root密码脚本
- $vCenter = “vcenter-host” 修改为你的vCenter地址
- $user = “vcenter-user” 修改为你的vCenter登录用户
- $password = “vcenter-passwd” 修改为你的vCenter登录密码
<# vCenter esxi Changing the root Password
+++++++++++++++++++++++++++++++++++++++++++++++++
+______ _____ _ _____ +
+| ___ \ / __ \| | |_ _|+
+| |_/ /____ _____ _ __| / \/| | | | +
+| __/ _ \ \ /\ / / _ \ '__| | | | | | +
+| | | (_) \ V V / __/ | | \__/\| |_____| |_ +
+\_| \___/ \_/\_/ \___|_| \____/\_____/\___/ +
+++++++++++++++++++++++++++++++++++++++++++++++++
#vCenter esxi Changing the root Password
# .\powercli-vCenter-esxi-change-root-password.ps1
#>
# vCenter Server configuration
$vCenter = "vcenter-host"
$user = "vcenter-user"
$password = "vcenter-passwd"
Write-Host "connect esxi & vCenter $VMhost" -ForegroundColor Green
# -User 'vcenter-user' 此处需填写vCenter登录用户
Connect-VIServer -Protocol https -User 'vcenter-user' -Password $password -Server $vCenter
Write-Host "Enter new ESXi root password $VMhost" -ForegroundColor Green
$cred = Get-Credential -UserName "root" -message "Enter new ESXi root password"
Write-Host "Select ESXi hosts for changing the root password $VMhost" -ForegroundColor Green
$VMhosts = Get-VMHost | Out-GridView -PassThru -Title "Select ESXi hosts for changing the root password"
Foreach ($VMhost in $VMhosts) {
Write-Host "Look at the host $VMhost" -ForegroundColor Green
$esxcli = Get-EsxCli -VMhost $VMhost -v2
Write-Host "Select the host whose password you want to reset $VMhost" -ForegroundColor Green
$esxcli.system.account.set.Invoke(@{id=$cred.UserName;password=$cred.GetNetworkCredential().Password;passwordconfirmation=$cred.GetNetworkCredential().Password})
}