Skip to content

Instantly share code, notes, and snippets.

@chaderoth
Created November 12, 2017 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaderoth/3d84ba46ee863d6f065e33be4796af44 to your computer and use it in GitHub Desktop.
Save chaderoth/3d84ba46ee863d6f065e33be4796af44 to your computer and use it in GitHub Desktop.
###############################
# Load VMware PowerCLI Module #
###############################
. 'C:\Program Files\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'
cls
###########################################
# Define variables to "Connect To Server" #
###########################################
Write-Host "Enter The Following Information To Create A New VM" -ForegroundColor DarkYellow
Write-Host ""
Write-Host ""
Write-Host "Enter The Name Of The vCenter Or ESXi Server To Connect To: " -ForegroundColor Yellow -NoNewline
$ServerInstance = Read-Host
Write-Host "Enter The Username Used To Connect To The Server" -ForegroundColor Yellow -NoNewline
Write-Host " (Enter As Domain\User)" -ForegroundColor Green -NoNewline
Write-Host ": " -ForegroundColor Yellow -NoNewline
$ServerUser = Read-Host
Write-Host "Enter The Password Used To Connect To The Server: " -ForegroundColor Yellow -NoNewline
$ServerPass = Read-Host -AsSecureString:$true
$ServerCreds = New-Object System.Management.Automation.PSCredential -ArgumentList $ServerUser,$ServerPass
#############################################################
# Define variables to "Create all VMs specified in $NewVms" #
#############################################################
Write-Host "Enter The FQDN Of The ESXi Server That Will Host The Guest VM: " -ForegroundColor Yellow -NoNewline
$ESXiHost = Read-Host
#Needed To Create An Array From The Comma Seperated List Stored In "$NewVMs"
Write-Host "Enter The Name Of The New Guest VM " -ForegroundColor Yellow -NoNewline
Write-Host "(Enter Multiples As A Comma Separated List)" -ForegroundColor Green -NoNewline
Write-Host ": " -ForegroundColor Yellow -NoNewline
$NewVMs = (Read-Host).split(',') | ForEach-Object {$_.trim()}
Write-Host "Enter The Name Of The VM Template Used To Create The New Guest VM: " -ForegroundColor Yellow -NoNewline
$VMTemplate = Read-Host
Write-Host "Enter The Name Of The Folder To Create The New Guest VM In: " -ForegroundColor Yellow -NoNewline
$VMLocation = Read-Host
Write-Host "Enter The Name Of The Datastore To Create The New Guest VM In: " -ForegroundColor Yellow -NoNewline
$VMDatastore = Read-Host
#Currently Commented Out As Network Is Defined In VM Template But Added Here For Future Use
#Write-Host "Enter The Name Of The Network To Connect The Guest VM To: " -ForegroundColor Yellow -NoNewline
#$VMNetwork = Read-Host
#####################
# Connect To Server #
#####################
Write-Host ""
Write-Host "Opening Connection To Server: " -ForegroundColor Green -NoNewline
Write-Host "$ServerInstance " -ForegroundColor Cyan -NoNewline
Write-Host ""
Connect-VIServer -Server $ServerInstance -Protocol https -Credential $ServerCreds
Write-Host ""
########################################################
# Create Hash Table #
# Create All VMs In $NewVMs #
# Populate Hash Table with Id of Each VM Creation Task #
########################################################
$TaskTable = @{}
foreach ($VM in $NewVMs){
$TaskTable[(New-VM -Name $VM -Template $VMTemplate -VMHost $ESXiHost -Location $VMLocation -Datastore $VMDatastore -RunAsync).Id] = $VM
}
###########################
# Display VM Build Status #
###########################
$RunningTasks = $TaskTable.Count
while($RunningTasks -gt 0){
Get-Task | foreach {
if($TaskTable.ContainsKey($_.Id) -and $_.State -eq "Success"){
Write-Host " VM " -ForegroundColor Green -NoNewline
Write-Host $TaskTable[$_.Id] -ForegroundColor Cyan -NoNewline
Write-Host " Has Been Created Successfully " -ForegroundColor Green
$TaskTable.Remove($_.ID)
$RunningTasks--
}
elseif($TaskTable.ContainsKey($_.Id) -and $_.State -eq "Error"){
Write-Host " Creation of VM " -ForegroundColor Red -NoNewline
Write-Host $TaskTable[$_.Id] -ForegroundColor Cyan -NoNewline
Write-Host " Has Failed " -ForegroundColor Red
$TaskTable.Remove($_.Id)
$RunningTasks--
}
}
Start-Sleep -Seconds 15
}
Write-Host ""
Write-Host "Task Complete" -ForegroundColor Yellow
Write-Host ""
##########################
# Disconnect From Server #
##########################
Write-Host ""
Write-Host "Closing Connection To Server: " -ForegroundColor Green -NoNewline
Write-Host "$ServerInstance " -ForegroundColor Cyan
Disconnect-VIServer -Server $ServerInstance -Force -Confirm:$false
Write-Host ""
Write-Host ""
Write-Host "!!The Connection To The Server Has Been Closed!!" -ForegroundColor Red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment