Just Add Code

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:

  1. Duplicate SID Error
  2. 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 this

5 Comments so far

  1. [...] the following lines of code (code thanks to justaddcode.com) separately in your PowerShell console (PowerShell must first be installed by opening a Command [...]

  2. Another Reader August 3rd, 2009 10:40 pm

    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.

  3. neil August 3rd, 2009 11:14 pm

    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.

  4. [...] 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 [...]

  5. [...] 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 [...]

Leave a reply