Virtual machine copying with PowerShell
Believe it or not, PowerShell is more than just fun and games. You can even do productive things with it! A common scenario I run into is the need to create a new virtual machine. Usually I want to base it on one I’ve already created and I want it to join a domain. There are a couple issues that I run in to:
- Duplicate SID Error
- Too many clicks
The first problem can be solved with a free tool that Microsoft has released called NewSID. Running that will give my virtual a unique SID so that it can be added to the domain.
Now for the second problem..too many clicks! Renaming a machine and adding it to the domain takes almost 2 minutes and a nearly infinite number of clicks and keystrokes…or maybe not. At any rate, it’d be great to simplify this process. Enter PowerShell.
Rename a Computer and Join Domain
$comp = get-wmiobject Win32_ComputerSystem $comp.Rename("newComputerName","password","administrator") $comp.JoinDomainOrWorkGroup("MYDOMAIN","domainPassword","MYDOMAIN\domainAdmin",$null,3)
Add a call to NewSID, wrap those delicious lines in a function and you are well on your way to renaming a virtual and joining a domain in fewer keystrokes. Its so easy you might even find yourself creating new virtuals just to run it again.
5 comments Digg this5 Comments so far
Leave a reply
[...] the following lines of code (code thanks to justaddcode.com) separately in your PowerShell console (PowerShell must first be installed by opening a Command [...]
Better than running NewSID on each new VM as they are brought up would be to run sysprep on the original VM image. sysprep obviates the need to run NewSID and is the Microsoft-supported way to deploy multiple VMs based upon a single image.
It is true that sysprep is the cleanest way to do it, but I was looking for the easiest. My images have SharePoint and SQL, which are both not very sysprep friendly. If the object is to keep it as close to copy/paste as possible, then newsid makes that easier. If you want a clean image, then sysprep is definitely the way to go.
[...] by proceeding with the following steps: Enter the following lines of code (code thanks to justaddcode.com) separately in your PowerShell console (PowerShell must first be installed by opening a Command [...]
[...] by proceeding with the following steps: Enter the following lines of code (code thanks to justaddcode.com) separately in your PowerShell console (PowerShell must first be installed by opening a Command [...]