Category Archives: Uncategorized

How to generate hundred Windows clients


Task: create one hundred Windows workstations.

Requirements: 1. create a testing lab consisting of several tens, preferable no less than one hundred of Windows client computers.

2. select a virtualization provider with the topmost performance and at no cost.

3. desirably that there be a possibility to expand the lab on demand.

Note to item 2: There were only two providers to choose, VMware with its ESXi and Parallels. Instead of serious investigation into their declared and real performance, ESXi has been chosen as already being the platform used in the office most and for available PowerCLI cmdlets.

Solution: a PowerShell module that emulates certain PowerCLI cmdlets. Direct use of VMware PowerCLI cmdlets are not possible unless you’ve bought something like vSphere or vCenter.

Details: the following examples demonstrate how to generate test machines and perform mass operation with them. The ESXiMgmt module outlined in greater detail in the following post.

The first script is the only in series that needed for lab creation:


#######################################################################################################################
# File:             ESXiMgmt_machines_generation_sample.ps1                                                           #
# Author:           Alexander Petrovskiy                                                                              #
# Publisher:        Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com                                #
# Copyright:        © 2011 Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com. All rights reserved.   #
# Prerequisites:    The module was tested with Vmware ESXi 4.1 U1 on the server side and                              #
#                       Vmware PowerCLI 4.1 U1                                                                        #
#                       plink.exe 0.60.0.0                                                                            #
# Usage:            To load this module run the following instruction:                                                #
#                       Import-Module -Name ESXiMgmt -Force                                                           #
#                   Please provide feedback in the SoftwareTestingUsingPowerShell.WordPress.Com blog.                 #
#######################################################################################################################
param([string]$Server,
	  [string]$User,
	  [string]$Password,
	  [string]$DatastoreName,
	  [string]$Drive,
	  [int]$MachinesNumber,
	  [string]$MachinePrefix,
	  [int]$OperationTimeout
	  )
# USAGE: .\ESXiMgmt_machines_generation_sample.ps1 192.168.1.1 root 123 datastore3 host1ds3 100 XPSP2_ 300

cls
Set-StrictMode -Version Latest
Import-Module ESXiMgmt -Force;

Connect-ESXi -Server $Server -Port 443 `
	-Protocol HTTPS -User $User -Password $Password `
	-DatastoreName $DatastoreName -Drive $Drive;

# This is to test can or can't plink.exe connect to your server
# The answer you need to select in case it questions you is obviously Yes.
Invoke-ESXiCommand -Server $Server `
	 -User $User -Password $Password `
	 -Command 'ls ~; sleep 10s; exit;' -PathToPlink 'C:\VMTests\plink.exe' `
	 -ShowWindow $true -OperationTimeout 10;

New-ESXiVMs -TemplateVMName 'template XP SP2 Sv 2' -Count $MachinesNumber `
		-Logname "C:\VMTests\xpsp2\100.txt" -NewVMName $MachinePrefix `
		-BasePath 'C:\VMTests\xpsp2' -OperationTimeout $OperationTimeout;

The following two scripts power the lab on and off, respectively:


#######################################################################################################################
# File:             ESXiMgmt_machines_poweron_sample.ps1                                                              #
# Author:           Alexander Petrovskiy                                                                              #
# Publisher:        Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com                                #
# Copyright:        © 2011 Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com. All rights reserved.   #
# Prerequisites:    The module was tested with Vmware ESXi 4.1 U1 on the server side and                              #
#                       Vmware PowerCLI 4.1 U1                                                                        #
#                       plink.exe 0.60.0.0                                                                            #
# Usage:            To load this module run the following instruction:                                                #
#                       Import-Module -Name ESXiMgmt -Force                                                           #
#                   Please provide feedback in the SoftwareTestingUsingPowerShell.WordPress.Com blog.                 #
#######################################################################################################################
param([string]$Server,
	  [string]$User,
	  [string]$Password,
	  [string]$DatastoreName,
	  [string]$Drive,
	  [string]$MachinePrefix,
	  [int]$OperationTimeout
	  )
# USAGE: .\ESXiMgmt_machines_poweron_sample.ps1 192.168.1.1 root 123 datastore3 host1ds3 XPSP2 300

cls
Set-StrictMode -Version Latest
Import-Module ESXiMgmt -Force;

Connect-ESXi -Server $Server -Port 443 `
	-Protocol HTTPS -User $User -Password $Password `
	-DatastoreName $DatastoreName -Drive $Drive;

$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue;
$VMs = Get-VM *

[int]$grouper = 5;
foreach($vm in $VMs)
{
	if ($vm.Name -like "$($MachinePrefix)*" -and `
		$vm.PowerState -ne 1)
	# [VMware.VimAutomation.ViCore.Types.V1.Inventory.PowerState]::PoweredOff = 0
	# [VMware.VimAutomation.ViCore.Types.V1.Inventory.PowerState]::PoweredOn = 1
	# [VMware.VimAutomation.ViCore.Types.V1.Inventory.PowerState]::Suspended = 2
	{
		Write-Verbose "$($vm.Name) is starting";
		Start-ESXiVM -Server $Server `
			-User $User -Password $Password `
			-Id (Get-ESXiVMId $vm);
		$grouper--;
		if ($grouper -eq 0){
			Write-Verbose "Sleeping for $($OperationTimeout) seconds";
			sleep -Seconds $OperationTimeout;
			$grouper = 5;
		}
	}
}

Disconnect-ESXi

and

#######################################################################################################################
# File:             ESXiMgmt_machines_poweroff_sample.ps1                                                             #
# Author:           Alexander Petrovskiy                                                                              #
# Publisher:        Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com                                #
# Copyright:        © 2011 Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com. All rights reserved.   #
# Prerequisites:    The module was tested with Vmware ESXi 4.1 U1 on the server side and                              #
#                       Vmware PowerCLI 4.1 U1                                                                        #
#                       plink.exe 0.60.0.0                                                                            #
# Usage:            To load this module run the following instruction:                                                #
#                       Import-Module -Name ESXiMgmt -Force                                                           #
#                   Please provide feedback in the SoftwareTestingUsingPowerShell.WordPress.Com blog.                 #
#######################################################################################################################
param([string]$Server,
	  [string]$User,
	  [string]$Password,
	  [string]$DatastoreName,
	  [string]$MachinePrefix,
	  [int]$OperationTimeout
	  )
# USAGE: .\ESXiMgmt_machines_poweroff_sample.ps1 192.168.1.1 root 123 datastore3 XPSP2 10
cls
Set-StrictMode -Version Latest
Import-Module ESXiMgmt -Force;

Connect-ESXi -Server $Server -Port 443 `
	-Protocol HTTPS -User $User -Password $Password `
	-DatastoreName $DatastoreName;

$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue;
$VMs = Get-VM *

foreach($vm in $VMs)
{
	if ($vm.Name -like "$($MachinePrefix)*" -and `
		$vm.PowerState -eq 1) #PoweredOn
	{
		Write-Verbose "$($vm.Name) is stopping";
		Stop-ESXiVM -Server $Server `
			-User $User -Password $Password `
			-Id (Get-ESXiVMId $vm);
		sleep -Seconds $OperationTimeout;
	}
}

Disconnect-ESXi

# to restart virtual machines instead of shutdown them
# simply call the ESXiMgmt_machines_poweron_sample.ps1 script:
# USAGE: .\ESXiMgmt_machines_poweron_sample.ps1 192.168.1.1 root 123 datastore3 host1ds3 XPSP2 300

The final example demonstrates how to find out names of virtual machines knowing their host names:


#######################################################################################################################
# File:             ESXiMgmt_search_for_guest_hostname_sample.ps1                                                     #
# Author:           Alexander Petrovskiy                                                                              #
# Publisher:        Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com                                #
# Copyright:        © 2011 Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com. All rights reserved.   #
# Prerequisites:    The module was tested with Vmware ESXi 4.1 U1 on the server side and                              #
#                       Vmware PowerCLI 4.1 U1                                                                        #
#                       plink.exe 0.60.0.0                                                                            #
# Usage:            To load this module run the following instruction:                                                #
#                       Import-Module -Name ESXiMgmt -Force                                                           #
#                   Please provide feedback in the SoftwareTestingUsingPowerShell.WordPress.Com blog.                 #
#######################################################################################################################
param([string]$Server,
	  [string]$User,
	  [string]$Password,
	  [string]$DatastoreName,
	  [string]$Drive
	  )
# USAGE: .\ESXiMgmt_search_for_guest_hostname_sample.ps1 192.168.1.1 root 123 datastore3 host1ds3

cls
Set-StrictMode -Version Latest
Import-Module ESXiMgmt -Force;

Connect-ESXi -Server $Server -Port 443 `
	-Protocol HTTPS -User $User -Password $Password `
	-DatastoreName $DatastoreName -Drive $Drive;

$vmname = Get-ESXiVMName -VMHostname 'B45E19A64B5E418'
Write-Host "Guest host $($vmname) corresponds to $($vmname)";

$hostnames = @(
			'1-0028687D9BSP3',
			'1-07B328CA254D4',
			'1-081D88F5DF2D4',
			'1-10BF79C694094',
			'1-15DB4C70F57B4',
			'1-1B154BAE5CD84',
			'1-1D33859002954',
			'1-3502988189C24',
			'1-3A324DA9EBE54',
			'1-9160383452304',
			'1-96B95B980BDE4',
			'1-B3F725FTYE56',
			'1-E49B84B0A4AA4',
			'1-FECEC772CBB74'
			);

$vmnamesFromHostnames = Get-ESXiVMName -VMHostname $hostnames;

for ($private:i = 0; $private:i -lt $vmnamesFromHostnames.Length; $private:i++)
{
	Write-Host "Guest host $($hostnames[$private:i]) corresponds to $($vmnamesFromHostnames[$private:i])";
}

Surprisingly, PowerGUI VMware PowerPack is useful here because the Get-VM cmdlet still works with independent ESXi hosts.

Advertisements
%d bloggers like this: