Created
February 19, 2015 18:22
-
-
Save jcefoli/6e6419d02d376f90e09c to your computer and use it in GitHub Desktop.
Hyper-V: This script takes a vhdx file and automatically creates a new VM based off it. All permissions are fixed after the VHDX gets copied.
This file contains hidden or 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
<# | |
Script: newvm.ps1 | |
Description: Script to automagically create new VM from a template VM | |
Usage from CMD: powershell -noexit "& "newvm.ps1" | |
#> | |
## Configure Variables Here ## | |
[String]$templateVMName = "Template.vhdx" | |
[String]$templateVMPath = "D:\Hyper-V\VHD\" | |
[String]$setNIC = "Broadcom Netxtreme 57xx Gigabit Controller - Virtual Switch" | |
## ------------------------ ## | |
#Ask for name of New VM instance | |
[string]$newVMName = Read-Host 'New VM Name' | |
#Remove invalid user input (AlphaNumeric only) | |
[System.Text.RegularExpressions.Regex]::Replace($newVMName,"[^1-9a-zA-Z_]",""); | |
#Copy Template VHD to new VHD | |
Copy-Item $templateVMPath\$templateVMName $templateVMPath\$newVMName.vhdx | |
#Create New VM & add 2 cores | |
New-VM -Name $newVMName -MemoryStartupBytes 512MB -Generation 1 -VHDPath $templateVMPath\$newVMName.vhdx -SwitchName $setNIC | |
SET-VMProcessor -VMname $newVMName -count 2 | |
#Fix VHD permissions | |
[String]$newVMGUID = get-vm -Name $newVMName| Select-Object Id | Select -Expand Id #> | |
$filePath = $templateVMPath + $newVMName + ".vhdx" | |
$Username = "`"NT VIRTUAL MACHINE\$newVMGUID`"" | |
icacls ("$filePath") /grant ("$Username" + ':(F)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment