Created
May 24, 2012 16:44
-
-
Save fenneh/2782653 to your computer and use it in GitHub Desktop.
Sample of Auto VM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Work in progress ;P!