A Quick Fling with PowerCLI

I was doing some burn-in and performance testing in the lab and I needed to automate vCenter actions.  I couldn’t find anything out there, so I rolled my own.

To use this, create a folder in vCenter for Gold VMs and another folder for Clone VMs.  Some of these actions are destructive, so the Clone VM folder shouldn’t have anything else in it. The script expects that all templates in the Gold VM folder are candidates for full clones, and all VMs in the Gold VM folder that have a snapshot are candidates for linked clones.

Set $goldFolder and $cloneFolder as above. Set $datastores to the list of datastores eligible for clones.  Set $actions to the list of actions you want to execute. Set $maxWaitMs to the maximum delay in milliseconds that you want the script to wait between tasks.

The script requires vPowerCLI 5.0 or greater (for linked clone creation.) Enjoy!

PowerCLI script: vc-load.ps1

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
$server = "vcenter"
$user = "Administrator"
$password = "Password123!"
$goldFolder = "GoldVMs"
$cloneFolder = "CloneVMs"
$datastores = "Datastore_A", "Datastore_B"
$maxWaitMs = 1
#All Actions
#$actions = "FullClone", "LinkedClone", "Start", "StartAll", "Restart", "RestartAll", "Suspend", "SuspendAll", "Shutdown", "ShutdownAll", "Stop", "StopAll", "Remove", "RemoveAll"
#$actions = "Start", "Restart", "Suspend", "Shutdown", "Stop"
$actions = "FullClone"
Disconnect-VIServer -Server $server -Confirm:$false
Set-PowerCLIConfiguration -InvalidCertificateAction "Ignore" -Confirm:$false
Write-Host "Connecting to server $server"
Connect-VIServer -Server $server -Protocol https -User $user -Password $password
While ($true) {
$sleep = Get-Random -minimum 0 -maximum $maxWaitMs
Write-Host Sleeping $sleep ms
Start-Sleep -m $sleep
$randomAction = Get-Random -input $actions
switch ($randomAction) {
FullClone {
Write-Host " "
Write-Host "Executing $randomAction"
$datastore = Get-Random -input $datastores
Write-Host "Randomly Selected Datastore $datastore"
Write-Host "Gold VMs:"
$goldVMs = Get-Template -Location (get-folder -name $goldFolder)
ForEach ($goldVM in $goldVMs) {Write-Host $goldVM.Name}
Write-Host "VMHosts with access to datastore $datastore"
$vmhosts = Get-Datastore -name $datastore | Get-VMHost
foreach ($vmhost in $vmhosts) {Write-Host $vmhost.Name}
$goldVM = Get-Random -input $goldVMs
Write-Host "Randomly Selected GoldVM $goldVM"
$randomHost = Get-Random -input $vmhosts
Write-Host "Randomly Selected Host $randomHost"
$VMID = Get-Date -UFormat "%s"
$cloneVM = "$goldVM-FullClone-$VMID"
Write-Host "Full-Cloning to $cloneVM"
New-Vm -vmhost $randomHost -Name $cloneVM -Template $goldVM -Datastore $datastore -Location $cloneFolder -RunAsync | Out-Null
}
LinkedClone {
Write-Host " "
Write-Host "Executing $randomAction"
$datastore = Get-Random -input $datastores
Write-Host "Randomly Selected Datastore $datastore"
Write-Host "Gold VMs:"
$goldVMs = Get-VM -Location (get-folder -name $goldFolder) ;
ForEach ($goldVM in $goldVMs) {Write-Host $goldVM.Name}
Write-Host "VMHosts with access to datastore $datastore"
$vmhosts = Get-Datastore -name $datastore | Get-VMHost
foreach ($vmhost in $vmhosts) {Write-Host $vmhost.Name}
$goldVM = Get-Random -input $goldVMs
Write-Host "Randomly Selected GoldVM $goldVM"
$randomHost = Get-Random -input $vmhosts
Write-Host "Randomly Selected Host $randomHost"
$VMID = Get-Date -UFormat "%s"
$cloneVM = "$goldVM-LinkedClone-$VMID"
$sourceSnapshot = Get-Snapshot $goldVM
If ($sourceSnapshot) {
Write-Host "Linked-Cloning to $cloneVM"
New-VM -Name $cloneVM -VM $goldVM -LinkedClone -ReferenceSnapshot $sourceSnapshot -VMHost $randomHost -Datastore $datastore -Location $cloneFolder -RunAsync | Out-Null
}
Else {
Write-Host "Requre snapshot on $goldVM to make linked clone.  Skipping..."
}
}
Start {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {($_.Powerstate -eq "PoweredOff") -or ($_.Powerstate -eq "Suspended")};
If ($targetVMs) {
$targetVM = Get-Random -input $targetVMs
Write-Host "Starting Randomly Selected VM: $targetVM"
}
}
StartAll {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {($_.Powerstate -eq "PoweredOff") -or ($_.Powerstate -eq "Suspended")};
If ($targetVMs) {
ForEach ($targetVM in $targetVMs) {
Write-Host "Starting VM $targetVM"
Start-VM $targetVM -RunAsync -Confirm:$false | Out-Null
}
}
}
Restart {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {$_.Powerstate -eq "PoweredOn"};
If ($targetVMs) {
$targetVM = Get-Random -input $targetVMs
Write-Host "Restarting Randomly Selected VM: $targetVM"
Write-Host Checking VMware Tools...
$vminfo = get-view -Id $targetVM.ID
If ($vminfo.Guest.ToolsStatus -eq "toolsNotRunning") {
Write-Host "VMWare Tools not running.  Skipping."
}
else {
Write-Host "VMWare Tools is running.  Restarting."
Restart-VMGuest $targetVM -Confirm:$false | Out-Null
}
}
}
RestartAll {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {$_.Powerstate -eq "PoweredOn"};
If ($targetVMs) {
ForEach ($targetVM in $targetVMs) {
Write-Host $targetVM.Name
If ($targetVMs) {
Write-Host "Restarting VM: $targetVM"
Write-Host Checking VMware Tools...
$vminfo = get-view -Id $targetVM.ID
If ($vminfo.Guest.ToolsStatus -eq "toolsNotRunning") {
Write-Host "VMWare Tools not running.  Skipping."
}
else {
Write-Host "VMWare Tools is running.  Restarting."
Restart-VMGuest $targetVM -Confirm:$false | Out-Null
}
}
}
}
}
Shutdown {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {$_.Powerstate -eq "PoweredOn"};
If ($targetVMs) {
$targetVM = Get-Random -input $targetVMs
Write-Host "Shutting Down Randomly Selected VM: $targetVM"
Write-Host Checking VMware Tools...
$vminfo = get-view -Id $targetVM.ID
If ($vminfo.Guest.ToolsStatus -eq "toolsNotRunning") {
Write-Host "VMWare Tools not running.  Skipping."
}
else {
Write-Host "VMWare Tools is running.  Shutting down."
Shutdown-VMGuest $targetVM -Confirm:$false | Out-Null
}
}
}
ShutdownAll {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {$_.Powerstate -eq "PoweredOn"};
If ($targetVMs) {
ForEach ($targetVM in $targetVMs) {
Write-Host "Shutting Down VM: $targetVM"
Write-Host Checking VMware Tools...
$vminfo = get-view -Id $targetVM.ID
If ($vminfo.Guest.ToolsStatus -eq "toolsNotRunning") {
Write-Host "VMWare Tools not running.  Skipping."
}
else {
Write-Host "VMWare Tools is running.  Shutting down."
Shutdown-VMGuest $targetVM -Confirm:$false | Out-Null
}
}
}
}
Suspend {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {$_.Powerstate -eq "PoweredOn"};
If ($targetVMs) {
$targetVM = Get-Random -input $targetVMs
Write-Host "Suspending Randomly Selected VM: $targetVM"
Suspend-VM $targetVM -RunAsync -Confirm:$false | Out-Null
}
}
SuspendAll {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {$_.Powerstate -eq "PoweredOn"};
If ($targetVMs) {
ForEach ($targetVM in $targetVMs) {
Write-Host "Suspending VM: $targetVM"
Suspend-VM $targetVM -RunAsync -Confirm:$false | Out-Null
}
}
}
Stop {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {$_.Powerstate -eq "PoweredOn"};
If ($targetVMs) {
$targetVM = Get-Random -input $targetVMs
Write-Host "Stopping Randomly Selected VM: $targetVM"
Stop-VM $targetVM -RunAsync -Confirm:$false | Out-Null
}
}
StopAll {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {$_.Powerstate -eq "PoweredOn"};
If ($targetVMs) {
ForEach ($targetVM in $targetVMs) {
Write-Host "Stopping VM: $targetVM"
Stop-VM $targetVM -RunAsync -Confirm:$false | Out-Null
}
}
}
Remove {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {($_.Powerstate -eq "PoweredOff") -or ($_.Powerstate -eq "Suspended")};
If ($targetVMs) {
$targetVM = Get-Random -input $targetVMs
Write-Host "Removing Randomly Selected VM: $targetVM"
Remove-VM $targetVM -DeletePermanently -RunAsync -Confirm:$false | Out-Null
}
}
RemoveAll {
Write-Host " "
Write-Host "Executing $randomAction"
$targetVMs = Get-VM -Location (get-folder -name $cloneFolder) | where {($_.Powerstate -eq "PoweredOff") -or ($_.Powerstate -eq "Suspended")};
If ($targetVMs) {
ForEach ($targetVM in $targetVMs) {
Write-Host "Removing VM: $targetVM"
Remove-VM $targetVM -DeletePermanently -RunAsync -Confirm:$false | Out-Null
}
}
}
} #switch
} #while
Write-Host "Disconnecting from server"
Disconnect-VIServer -Server $server -Confirm:$false

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.