Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Created December 28, 2015 16:28
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 techthoughts2/ad084434d1aea94e2d84 to your computer and use it in GitHub Desktop.
Save techthoughts2/ad084434d1aea94e2d84 to your computer and use it in GitHub Desktop.
Create a New-VHD
#----------------------Variables Used----------------------------------------
[string]$vmName = $null
[string]$vhdName = $null
[string]$vhdType = $null
[string]$path = $null
[string]$vhdPath = $null
[int64]$vhdSize = $null
[string]$dynamic = $null
[string]$confirm = $null
#--------------------END Variables Used--------------------------------------
#----------------------USER PROMPTS------------------------------------------
$vmName = Read-Host "Enter name of VM to create new VHD for"
#check to ensure VM actually exists on the local Hyp
$vms = Get-VM | Select-Object -ExpandProperty Name
if($vmName -like $vms){
#vm was found, we can continue with questions
$vhdName = Read-Host "Enter name of new .vhdx file (Ex VMNameDisk4)"
$path = Read-Host "Enter path where .vhdx will reside (Ex E:\VHD\)"
$vhdPath = $path+$vhdName+".vhdx"
$vhdSize = Read-Host "Enter VHDSize (GB)"
$vhdSize = [math]::round($vhdSize *1Gb, 3) #converts GB to bytes
while("yes","no" -notcontains $dynamic){
$dynamic = Read-Host "Will this new VHD be dynamic? (yes/no)"
}
if($dynamic -eq "yes"){
$vhdType = "Dynamic"
}
else{
$vhdType = "Fixed"
}
#-------------------END USER PROMPTS------------------------------------------
#-----------------CONFIRM CREATE NEW VHD----------------
Write-Host "Creating new VHD:" $vhdPath "(size:" $vhdSize "),"`
"`n VHD Type:" $vhdType `
"`n Attaching this new VHD to:" $vmName `
-ForegroundColor Cyan
while("yes","no" -notcontains $confirm){
$confirm = Read-Host "Proceed? (yes/no)"
}
#---------------END CONFIRM CREATE NEW VHD--------------
if($confirm -eq "yes"){
#-------------NEW VHD CREATION----------------------
#create either a fixed or dynamic vhd depending on user choice
try{
if($dynamic -eq "yes"){
New-VHD -SizeBytes $vhdSize –Path $vhdPath -Dynamic -Confirm:$false
}
else{
Write-Host "Creating fixed .vhdx - this could take some time..." -ForegroundColor Cyan
New-VHD -SizeBytes $vhdSize –Path $vhdPath -Fixed -Confirm:$false
}
}
catch{
Write-Host "There was an error creating the new VHD" -ForegroundColor Red
Write-Error $_
}
#----------END NEW VHD CREATION---------------------
#--------------ATTACH NEW VHD ----------------------
try{
Add-VMHardDiskDrive -VMName $vmName -Path $vhdPath
}
catch{
Write-Host "There was an error attaching the new VHD to" $vmName -ForegroundColor Red
Write-Error $_
}
#------------END ATTACH NEW VHD --------------------
#now show all VHDs that are attached to the specified VM:
Write-Host "Final Results:" -ForegroundColor Cyan
Write-Host "--------------" -ForegroundColor Cyan
$vm = Get-VM -Name $vmName
Get-VHD -VMId $vm.ID | select Path
}
else{
Exit
}
}
else{
#vm was not found
Write-Host "The VM you specified was not located - therefore you cannot add a VHD to it" -ForegroundColor Yellow
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment