I did a presentation for the virtual PowerShell chapter of SQLPASS (link HERE) last week.
I talked about using sysprep for creating quick OS images and also talked about the new sysprep support now in SQL Server 2008 R2.
My lab environment uses Hyper-V, so I try to practice using PowerShell as much as possible. One thing I demo’d was a very basic/simple PowerShell script to:
- Create a new VM
- Change the default RAM
- Copy a sysprep’d image to a new name
- Attach a hard disk
- Add a DVD drive
- Attach an ISO to the DVD drive
- Start the VM
Here’s the script:
import-module hyperv
new-vm -name foo
get-vm foo|set-vmmemory -memory 1024
copy-item "C:\Users\Public\Documents\Hyper-V\Virtual hard disks\Server_sysprep_gold.vhd" `
"C:\Users\Public\Documents\Hyper-V\Virtual hard disks\Server_foo.vhd"
get-vm foo|add-vmdisk -controller 0 -lun 0 -path "C:\Users\Public\Documents\Hyper-V\Virtual hard disks\Server_foo.vhd"
get-vm foo|add-vmdrive -dvd -controllerid 1 -lun 0
get-vm foo|add-vmdisk -dvd -controller 1 -lun 0 -path "C:\Users\Administrator\ISOs\sql_server_2008_r2.iso"
get-vm foo|start-vm
(Yes, I could have condensed some stuff, but that’s not the point I was trying to communicate. I don’t see the point in condensing 10 lines of code to 5-6 at the expense of readability, and prefer to ease the PowerShell learning curve for newcomers.)
A few resources from the session:
- PowerShell Hyper-V module: http://pshyperv.codeplex.com/
- My Microsoft connect item (SQL R2 sysprep doesn’t support SQL tools): https://connect.microsoft.com/SQLServer/feedback/details/658584/sysprep-in-sql-2008-r2
Enjoy! I’m not sure when the recording will be put online, but I’ll put a link to it… I don’t talk a whole lot about PowerShell though. Maybe just 10-15 minutes at the end.