您可以自定义和使用以下示例脚本来在多个虚拟机 (VM) 上执行操作。这些操作包括打开电源、关闭电源、关闭、重新启动和删除虚拟机。
该脚本可以从 vCenter Server 中删除虚拟机,但不能从 View 中删除虚拟机。
要复制和粘贴不包含分页符的脚本内容,请使用此主题的 HTML 版本,您可以从位于 https://www.vmware.com/support/pubs/view_pubs.html 的 Horizon 7 文档页面找到该版本。
脚本输入
此脚本将读取一个输入文件,用于部署 Linux 桌面的示例 PowerCLI 脚本的输入文件中对此做了介绍。此脚本还会以交互方式要求提供以下信息:
- vCenter Server 的 IP 地址
- vCenter Server 的管理员登录名称
- vCenter Server 的管理员密码
- 要执行的操作,可以是打开电源、关闭电源、关闭客户机、重新启动虚拟机、重新启动虚拟机客户机或删除虚拟机。
- 虚拟机上的操作之间的等待时间(以秒为单位)。
脚本内容
<#
.DESCRIPTION
The Tool supports:
1. Power off VMs
2. Power on VMs
3. Shutdown VMs
4. Restart VMs
5. Restart VM guest
6. Delete VMs from Disk
.NOTES
#>
#--------------------- Functions --------------------
function GetInput
{
Param($prompt, $IsPassword = $false)
$prompt = $prompt + ": "
Write-Host $prompt -NoNewLine
[Console]::ForegroundColor = "Blue"
if ($IsPassword)
{
$input = Read-Host -AsSecureString
$input = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($input))
}
else
{
$input = Read-Host
}
[Console]::ResetColor()
return $input
}
function IsVMExists ($VMExists)
{
Write-Host "Checking if the VM $VMExists Exists"
[bool]$Exists = $false
#Get all VMS and check if the VMs is already present in VC
$listvm = Get-vm
foreach ($lvm in $listvm)
{
if($VMExists -eq $lvm.Name )
{
$Exists = $true
Write-Host "$VMExists is Exist"
}
}
return $Exists
}
function Delete_VM($VMToDelete)
{
Write-Host "Deleting VM $VMToDelete"
Get-VM $VMToDelete | where { $_.PowerState –eq "PoweredOn" } | Stop-VM –confirm:$false
Get-VM $VMToDelete | Remove-VM –DeleteFromDisk –confirm:$false
}
#------------------ Handle input ---------------------
"-----------------------------------------------------"
$vcAddress = GetInput -prompt "Your vCenter address" -IsPassword $false
$vcAdmin = GetInput -prompt "Your vCenter admin user name" -IsPassword $false
$vcPassword = GetInput -prompt "Your vCenter admin user password" -IsPassword $true
"-----------------------------------------------------"
$action = GetInput -prompt 'Select action: 1). Power On 2). Power Off 3) Shutdown VM Guest 4). Restart VM 5). Restart VM Guest 6). Delete VM' -IsPassword $false
$sleepTime = GetInput -prompt 'Wait time (seconds) between each VM' -IsPassword $false
"-----------------------------------------------------"
[Console]::ForegroundColor = "Yellow"
switch ($action)
{
1
{
"Your selection is 1). Power On"
}
2
{
"Your selection is 2). Power Off"
}
3
{
"Your selection is 3) Shutdown"
}
4
{
"Your selection is 4). Restart VM"
}
5
{
"Your selection is 5). Restart VM Guest"
}
6
{
"Your selection is 6). Delete VM"
}
default
{
"Invalid selection for action: $action"
exit
}
}
[Console]::ResetColor()
$csvFile = '.\CloneVMs.csv'
#check if file exists
if (!(Test-Path $csvFile))
{
write-host -ForeGroundColor Red "CSV File not found"
exit
}
"-----------------------------------------------------"
#--------------------- Main --------------------------
#Read input CSV file
Disconnect-VIServer $vcAddress -Confirm:$false
#Connect-VIServer $vcAddress -ErrorAction Stop -user $vcAdmin -password $vcPassword
Connect-VIServer $vcAddress -user $vcAdmin -password $vcPassword
$csvData = Import-CSV $csvFile
foreach ($line in $csvData)
{
$VMName = $line.VMName
switch ($action)
{
1
{
Get-VM $VMName | Start-VM -Confirm:$false
}
2
{
Get-VM $VMName | Stop-VM -Confirm:$false
}
3
{
Get-VM $VMName | Shutdown-VMGuest -Confirm:$false
}
4
{
Get-VM $VMName | Restart-VM -Confirm:$false
}
5
{
Get-VM $VMName | Restart-VMGuest -Confirm:$false
}
6
{
if (IsVMExists ($VMName))
{
Delete_VM ($VMName)
}
}
default{}
}
Start-Sleep -s $sleepTime
}
Disconnect-VIServer $vcAddress -Confirm:$false
exit
脚本执行
下面是执行脚本时显示的消息:
PowerCLI C:\scripts> .\VMOperations.ps1 Your vCenter address: 10.117.44.17 Your vCenter admin user name: administrator Your vCenter admin user password: ******* -------------------------------------------------- Select action: 1). Power On 2). Power Off 3) Shutdown VM Guest 4). Restart VM 5). Restart VM Guest 6). Delete VM: 1 Wait time (seconds) between each VM: 20 -------------------------------------------------- Your selection is 6). Delete VM
对于打开电源、重新启动虚拟机和重新启动虚拟机客户机操作,将虚拟机之间的等待时间指定为至少 20 秒可避免出现引导风暴的情况,否则可能导致某些操作失败。