Skip to content

Instantly share code, notes, and snippets.

@PCfromDC
Last active November 25, 2015 20:37
Show Gist options
  • Save PCfromDC/6170d76896066df415ce to your computer and use it in GitHub Desktop.
Save PCfromDC/6170d76896066df415ce to your computer and use it in GitHub Desktop.
Ignite 2015 session Using Desired State Configuration to Deploy SQL
$serverName = "SQL03"
$vmName = "3- SQL03"
$vmDest= "G:\VMs\$serverName\"
$isoName = "z_Master-Server2012R2-Standard.vhdx"
$isoPath = "D:\_Images\"
# VM Host Name
$computerName = "Host-HP-01"
# Network
$switchName = "pcDemo-1"
$isoExtension = [System.IO.Path]::GetExtension($isoName)
Function validate($dest){
# Verify Folder Exists else Create It
if ((Test-Path $dest) -eq $false) {
[IO.Directory]::CreateDirectory($dest)
}
}
Function createVM {
$startTime = Get-Date
$isoDest= $vmDest + "OS\"
validate($isoDest)
$path = "$isoPath" + "$isoName"
$vhdPath = "$isoDest" + "$serverName" + "$isoExtension"
if ((Test-Path $vhdPath) -eq $false) {
Copy-Item -Path $path -Destination $vhdPath
}
# ***** Create Drive Information *****
$dataDrive = "userData"
$logDrive = "userLogs"
$tempData = "tempData"
$tempLogs = "tempLogs"
$buDrive = "backups"
$driveArray = @($dataDrive,$logDrive,$tempData,$tempLogs,$buDrive)
foreach ($drive in $driveArray) {
$drive = "$vmDest" + "$drive" + "\"
validate($drive)
}
# Startup Memory
$memStartUp = 24GB # Not a String... no Quotes!
# CPU Cores
$cores = "8"
# Data Disk Size
$dataDiskSize = 80GB # Not a String... no Quotes!
# Create the VM
New-VM -VHDPath $vhdPath -ComputerName $computerName -Generation 1 -MemoryStartupBytes $memStartUp -Name $vmName -Path $isoDest -SwitchName $switchName
Set-VMProcessor -VMName $vmName -ComputerName $computerName -Count $cores
# Create Drives
foreach ($drive in $driveArray) {
$path = "$vmDest" + "$drive" + "\$drive.vhdx"
New-VHD -Path $path -SizeBytes $dataDiskSize
Add-VMHardDiskDrive -VMName $vmName -Path $path -ControllerType SCSI
}
# Remove DVD Drive
Get-VMDvdDrive -VMName $vmName | Remove-VMDvdDrive # -VMName $vmName -ControllerNumber 1 -ControllerLocation
Start-VM -Name $vmName
$stopTime = Get-Date
$finishTime = $stopTime.TimeOfDay - $startTime.TimeOfDay
Write-Host("Server created and started in: " + $finishTime.TotalSeconds.ToString() + " seconds...")
}
Function deleteVM {
Stop-VM -ComputerName $computerName -Name $vmName -TurnOff -Force
Remove-VM -ComputerName $computerName -Name $vmName -Force
Remove-Item -Path $vmDest -Force -Recurse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment