Skip to content

Instantly share code, notes, and snippets.

@jimfdavies
Created February 11, 2013 14:39
Show Gist options
  • Save jimfdavies/4754761 to your computer and use it in GitHub Desktop.
Save jimfdavies/4754761 to your computer and use it in GitHub Desktop.
Get-OSTypeFromTemplate returns the VSphere API 'ostype' from a defined template. This can be used to build out an OS Customisation Specification when deploying a new VM from template. Easily extensible for other OSes or more specific flavors. If the template does not give up its secrets, the script stops and waits for you to type it in.
Function Get-OSTypeFromTemplate {
param($templatename)
$templateview=get-view -id (get-template -Name $templatename).id
switch -wildcard ($templateview.Guest.GuestFullName)
{
"*Windows*" { $ostype = "Windows" }
"*Linux*" { $ostype = "Linux" }
default { $ostype = Read-Host "Unable to determine OS from template $templatename. Enter the word Linux or Windows here" }
}
return $ostype
}
#Add-PSsnapin VMware.VimAutomation.Core
#Connect-VIServer $vcserver
$templatename = "RHEL 6.2 x64"
$ostype = Get-OSTypeFromTemplate -templatename $templatename
$ostype # "Linux"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment