I have been trying to automate the reset of a lab environment with PowerCLI. This lab consists of 15 sets of 3 VMs. Each set contains one XP, one Server 2008, and one Linux appliance. The virtual machines are created from templates and are assigned to specific usernames. Automating this process would seriously help out the technicians whose responsibilities
The script (which is listed below) has the following functions created and working:
1) DeleteOldVMs - Stops the existing virtual machines and then deletes them from disk
2) CreateVMSet - Creates each of the 3 virtual machines necessary for each user.
3) StartNewVMs - Starts all newly created virtual machines
4) AssignUserToVM - Assign rights to the virtual machine sets to each user
My final problem is the overlap of the Start-VM task and the New-VM tasks. The New-VM task needs to complete before I assign rights to VMs and start them. I've tried using Wait-Task, but I haven't had any luck. I currently have the Wait-Task sections removed from the script. I'm looking for any suggestions as to how I can properly implement Wait-Task to force the script to wait until the VM sets are created before proceeding.
My only other thought was using Start-Sleep for a predetermined amount of time.
Any thoughts?
Here is the script:
Function AssignUserToVM
{
$xpvm = get-vm WinXP-$n
$winvm = get-vm Win2008-$n
$vistavm = get-vm OpenVista-$n
#$emrvm = get-vm OpenEMR-$n
New-VIPermission -Entity $xpvm -Principal "ehr$n" -Role EHR822VirtualMachineUser -Propagate $false
New-VIPermission -Entity $WinVM -Principal "ehr$n" -Role EHR822VirtualMachineUser -Propagate $false
New-VIPermission -Entity $vistavm -Principal "ehr$n" -Role EHR822VirtualMachineUser -Propagate $false
#New-VIPermission -Entity $emrvm -Principal "ehr$n" -Role EHR822VirtualMachineUser -Propagate $false
}
Function CreateVMSet
{
#Create XP VM
$XPTemplate = Get-Template -Name "Template-WinXPPro"
$XPSpec = Get-OSCustomizationSpec -Name "Windows XP"
$XPVMName = "WinXP-$n"
New-VM -VMHost "$VMhost" -Name "$XPVMName" -Template "$XPTemplate" -OSCustomizationSpec $XPSpec -Location $VMLocation
#Create 2008 VM
$2008Template = Get-Template -Name "Template-Win2008-32"
$2008VMName = "Win2008-$n"
$2008Spec = Get-OSCustomizationSpec -Name "Windows Server 2008"
$VMhost = Get-VMHost
New-VM -VMHost $VMhost -Template $2008Template -Name $2008VMName -OSCustomizationSpec $2008Spec -Location $VMlocation
#Create OpenVistA VM
$OpenVistATemplate = Get-Template -Name "Template-OpenVistA-Appliance"
$OpenVistaVMName = "OpenVista-$n"
New-VM -VMHost $VMhost -Name $OpenVistaVMName -Template $OpenVistATemplate -Location $VMlocation
}
Function StartNewVMs
{
Start-VM -VM "Win2008-*"
Start-VM -VM "WinXP-*"
Start-VM -VM "OpenVista-*"
}
Function DeleteOldVMs
{
Stop-VM -VM "Win2008-*"
Stop-VM -VM "WinXP-*"
Stop-VM -VM "OpenVista-*"
Remove-VM -VM "Win2008-*" -DeletePermanently
Remove-VM -VM "WinXP-*" -DeletePermanently
Remove-VM -VM "OpenVista-*" -DeletePermanently
}
Write-Host "Please enter your AD credentials: "
$vCenterCredential = Get-Credential
$VMhost = "hitecvsphere.ccbcmd.edu"
$vCenter = "hitechvcenter.ccbc.ccbcmd.edu"
$VMlocation = "AHL822"
Connect-VIServer -Server $vCenter -Credential $vCenterCredential
$NumVMsToCreate = Read-Host "Enter the number of virtual machine sets that you wish to create: "
DeleteOldVMs
$n = 1
while ($n -le $NumVMsToCreate)
{
CreateVMSet
$n++
}
StartNewVMs
$n = 1
while ($n -le $NumVMsToCreate)
{
AssignUserToVM
$n++
}