您可以自訂和使用下列範例指令碼,在多個 Linux 虛擬機器 (VM) 上執行作業。該作業包含開啟電源、關閉電源、關機、重新啟動和刪除虛擬機器。

此指令碼可從 vCenter Server 刪除虛擬機器,但無法從 View 刪除。

若要複製和貼上不含分頁符號的指令碼內容,請使用本主題的 HTML 版本,您可以從 Horizon7 文件頁面取得,網址是:https://www.vmware.com/support/pubs/view_pubs.html

指令碼輸入

此指令碼會讀取一個輸入檔,在用於部署 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 秒來避免開機風暴情況,該情況可能造成某些作業失敗。