Skip to content

Instantly share code, notes, and snippets.

@fenneh
Created May 24, 2012 16:44
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 fenneh/2782653 to your computer and use it in GitHub Desktop.
Save fenneh/2782653 to your computer and use it in GitHub Desktop.
Sample of Auto VM
param (
[string]$vmName = (Read-Host "Provide VM Name"),
[string]$Template = (Read-Host "What Template do you want Vanilla/Web/Gravis?")
[string]$Template = (Read-Host "Disk size 40/80?")
)
# Import Configuration
. (Join-Path -Path (Split-Path -parent $MyInvocation.MyCommand.Definition) -ChildPath "configuration.ps1")
# Load VMware Module
if ((Get-PSSnapin | Where-Object { $_.Name -eq "VMware.VimAutomation.Core" }) -eq $null) { Add-PSSnapin VMware.VimAutomation.Core }
# Connect to vCenter
Connect-VMHost
### Variables ###
$domainName = "hihihih.justfen.co.uk"
$fqdn = $vmName.$domainName
# Highest amount of disk space required for VM
$RequiredDisk = 80000
$Pool = Get-ResourcePool "TRON"
# Will calculate Datastore with most free space, useful until Storage DRS setup
$DataStore = Get-Datastore | Where { $_.FreespaceMB -gt $RequiredDisk } | Sort-Object FreeSpaceMB -Descending | Select -First 1
if (!$Datastore) { throw "No Datastore could be found with $RequiredDisk MB free space." }
# Will chose ESX host with least CPU utilization, maybe RAM would be a better choice
$ESXHost = Get-VMHost | Sort $_.CPuUsageMhz | Select -First 1
$OSCustomizationSpec = get-OSCustomizationSpec "Windows"
if (!$OSCustomizationSpec) { throw "OSCustomizationSpec Windows was not found"}
New-VM -vmhost $ESXHost `
-Name $vmName `
-Template $Template `
-ResourcePool $Pool `
-Datastore $DataStore `
-OSCustomizationSpec $OSCustomizationSpec
Start-VM -VM $vmName
@fenneh
Copy link
Author

fenneh commented May 24, 2012

Work in progress ;P!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment