Deploy a new VM with PowerCLI
A PowerCLI function that prompts you to select from your Clusters, Hosts, Datastores & Networks. It will then deploy a VM according to the sizes you input
#Select Cluster
function vpf-Select-Cluster
{
$clusters = Get-Cluster
Write-Host
Write-Host " ================ Select $DCChoice Cluster ================" -ForegroundColor green
Write-Host
$i = 1
$clusters | ForEach-Object -Process {
Write-Host " $i $($_.Name)"
$i++
}
Write-Host
Write-Host " A Select All Clusters"
Write-Host
$selection = Read-Host " Please make a selection (Q to Quit)"
if($selection -eq 'Q'){
clear
vpf-NewVM
}
else{
$global:CLUChoice = $clusters[$selection -1].Name
Write-Host " You chose cluster $CLUChoice"
Start-Sleep -Second 1
}
}
#####################################################
function vpf-Select-Datastore
{
$global:DiskSize = Read-Host " Please Enter total disk size"
$global:RAMSize = Read-Host " Please Enter total RAM size"
$VMSize = [int]$global:DiskSize + [int]$global:RAMSize
$Available = {([int]$_.ProvisionedGB + [Int]$VMSize) -lt [Int]$_.CapacityGB -and $_.name -notlike "*-Local*"}
$datastores = Get-Cluster $Global:CLUChoice | get-vmhost $Global:NewVMHost | Get-Datastore | Select Datacenter, Name, Type, CapacityGB,
@{N='AfterProvisionedGB';E={[math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted + $VMSize)/1GB)}},
@{N='FreespaceGB';E={[math]::Round($_.FreespaceGB - $VMSize)}},
@{N='OverProvisioned';E={If ((($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted + $VMSize)/1GB) -gt $_.CapacityGB) {"OverProvisioned"} Else {"Capacity Available"} }}, state |
Sort FreespaceGB | Where {([int]$_.AfterProvisionedGB + [Int]$VMSize) -lt [Int]$_.CapacityGB -and $_.name -notlike "*-Local*"}
Write-Host
Write-Host " ================ Select Datastore ================" -ForegroundColor green
Write-Host
Write-Host " # Name Free Space After Deploy"
$i = 1
$datastores | ForEach-Object -Process {
Write-Host " $i $($_.Name) $($_.FreespaceGB)"
$i++
}
Write-Host
Write-Host
$selection = Read-Host " Please make a selection (Q to Quit)"
if($selection -eq 'Q'){
vpf-NewVM
}
else{
$Global:NewVMDatastore = $datastores[$selection -1].Name
Write-Host
Write-Host " You chose Datastore $Global:NewVMDatastore"
write-host
Write-Host -NoNewLine " Press any key to continue...";
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Clear
}
}
####################################################
function vpf-Select-Host
{
Get-Cluster $global:CLUChoice | get-VMHost
write-host
$global:hostchoice = Get-Datacenter $global:DCChoice | Get-Cluster $global:CLUChoice | get-vmhost
Write-Host
Write-Host " ================ Select Host ================" -ForegroundColor green
Write-Host
$i = 1
$global:hostchoice | ForEach-Object -Process {
Write-Host " $i $($_.Name)"
$i++
}
Write-Host
$selection = Read-Host " Please make a selection (Q to Quit)"
if($selection -eq 'Q'){
clear
vpf-NewVM
}
else{
$Global:NewVMHost = $global:hostchoice[$selection -1].Name
Write-Host " You chose Host $NewVMHost"
Clear
}
}
######################################################
function vpf-Select-Network
{
$Global:networkschoice = get-virtualportgroup -VMhost $Global:NewVMHost | Select Name
Write-Host
Write-Host " ================ Select Network ================" -ForegroundColor green
Write-Host
$i = 1
$Global:networkschoice | ForEach-Object -Process {
Write-Host " $i $($_.Name)"
$i++
}
Write-Host
$selection = Read-Host " Please make a selection (Q to Quit)"
if($selection -eq 'Q'){
clear
vpf-NewVM
}
else{
$Global:NewVMNetwork = $Global:networkschoice[$selection -1].Name
Write-Host " You chose Host $NewVMHost"
Clear
}
}
function vpf-NewVM
{
write-host
$NewVMName = Read-Host " Please Enter New VM Name"
$NewVMMem = Read-Host " Please Enter number of vCPU"
vpf-Select-Cluster
clear
vpf-Select-Host
clear
vpf-Select-Datastore
clear
vpf-Select-Network
clear
New-VM -Name $NewVMName –VMHost $Global:NewVMHost -Datastore $Global:NewVMDatastore -DiskGB $global:DiskSize -MemoryGB $global:RAMSize -NumCpu $NewVMCPU -NetworkName $Global:NewVMNetwork
Write-Host -NoNewLine 'VM Container Deploying. Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
clear
}
########################################################################################################################################################################
clear