Sie sind auf Seite 1von 4

Virtualization: Create Hyper-V Virtual Machines with Windows PowerShell

If you need to create and configure virtual machines on a regular basis, using Windows PowerShell to do so can speed up the process.

Neil Tucker
Whether you need to test new or standardized server setups, Hyper-V gives you an environment to quickly and efficiently run test configurations, which has made it a popular lab environment. The only catch is, you need to create and configure virtual machines (VMs) on a regular basis. If youre looking for an easy way to build Hyper-V images, Windows PowerShell scripts are a viable solution. The extent to which you can customize Windows PowerShell and its flexible command structure makes it suitable for any environment. When combined with an automated or unattended setup strategy, virtually any computer configuration is possible.

Configure Windows PowerShell


This strategy assumes youre working on a Windows Server 2008 R2 system that meets the requirements for the Hyper-V role. After following these steps, the system will be ready for Hyper-V scripting:

do this from the Server Manager or by using the ServerManagerCmd.exe command-line tool (ServerManagerCmd.exe

Shell).

This is disabled by default. You can configure this with the Set-ExecutionPolicy cmdlet (Set-

dule. You need this module to access all the cmdlets designed specifically for Windows PowerShell. From the

ownload the latest stable, non-development release. Make sure the block attribute is removed from the zipped files

out needing digital signatures. If youre doing the install on Server Core, unblock and unzip the module file on

er. Otherwise, youll need additional tools to perform these operations on the server (the stream.exe tool on

enable the Hyper-V cmdlets by importing the newly installed module (Import-Module HyperV). If you get an error

ntend to regularly use Windows PowerShell to manage your Hyper-V environment, you should add the Import-

werShell profile file. This will let you use the cmdlets without having to configure the server each time. You should

ronment to avoid any issues running the cmdlets. To verify youve successfully installed the cmdlets and theyre functioning properly, use the GetCommand cmdlet to see a list of the commands available to you (Get-Command Module Hyperv). As with other modules, each cmdlet comes with helpful information about its functions and examples on how to use it (Get-Help New-VM Detailed and Get-Help New-VM Examples). Theres additional documentation available on CodePlex.

Configure the Hyper-V Windows PowerShell Script

Once your Windows PowerShell environment is ready, you can start building new VMs. Make sure you start with Administrator permissions to use elevated privileges for these commands. The script uses Hyper-V cmdlets to create a new VM based on five variables (see Figure 1) you provide during setup. Each variable has a pre-assigned default value that will be used if one isnt provided. Figure 1 Descriptions of the variables defined during Hyper-V virtual machine (VM) setup.
$SRV1 $SRAM VM name Amount of memory assigned to the VM

$SRV1VHD Size of the virtual hard drive the VM is using $VMLOC Location of where you want to create the VM virtual hard drive

$Network1 VM virtual network connection

After defining those variables, the New-Image.ps1 script (see Figure 2) configures the Hyper-V Virtual Network using the value assigned to the $Network1 variable. Before defining the new private network with the New-VMPrivateSwitchcmdlet, remove it with Remove-VMSwitch, whether or not it already existed. This ensures you dont define duplicate networks with the same name. Figure 2 The New-Image.ps1 script that creates new virtual machines.
# This script creates a new Hyper-V machine with hard drive, memory & network resources configured. # Variables $SRV1 = Read-Host "Enter the Virtual Machine name (Press [Enter] to choose Server01): " if ($SRV1 -eq ""){$SRV1="Server01"} ; if ($SRV1 -eq $NULL){$SRV1="Server01"} $SRAM = Read-Host "Enter the size of the Virtual Machine Memory (Press [Enter] to choose 512MB): " if ($SRAM -eq ""){$SRAM=512MB} ; if ($SRAM -eq $NULL){$SRAM=512MB} $SRV1VHD = Read-Host "Enter the size of the Virtual Machine Hard Drive (Press [Enter] to choose 40GB): " if ($SRV1VHD -eq ""){$SRV1VHD=40GB} ; if ($SRV1VHD -eq $NULL){$SRV1VHD=40GB} $VMLOC = Read-Host "Enter the location of the Virtual Machine file (Press [Enter] to choose C:\HyperV): " if ($VMLOC -eq ""){$VMLOC="C:\HyperV"} ; if ($VMLOC -eq $NULL){$VMLOC="C:\HyperV"} $Network1 = Read-Host "Enter the name of the Virtual Machine Network (Press [Enter] to choose Network1): "

if ($Network1 -eq ""){$Network1="Network1"} ; if ($Network1 -eq $NULL){$Network1="Network1"} # Configure Hyper-V Virtual Network remove-vmswitch $Network1 -force -erroractionsilentlycontinue new-vmprivateswitch $Network1 # Create Virtual Machines MD $VMLoc -erroractionsilentlycontinue new-vm $SRV1 -path $VMLoc new-vhd -vhdpaths $VMLoc\$SRV1 -size $SRV1VHD add-vmdisk -vm $SRV1 -controllerid 0 -lun 0 -path $VMLoc\$SRV1 get-vm $SRV1 | add-vmdrive -controllerid 1 -lun 0 -dvd get-vm $SRV1 | set-vmmemory -memory $SRAM get-vm $SRV1 | add-vmnic -virtualswitch $Network1

The final part of the process where you actually create the VM is simple. Create the directory location for the virtual hard drive (VHD). Then six cmdlets create and configure the new VM. The New-VM command defines the machine and its location. New-VHD creates the VHD file and Add-VMDisk assigns it to the VM. Add-VMDrive adds a DVD drive to the machine, Set-VMMemory defines the amount of RAM and Add-VMNIC configures the network card. Once youve configured the new VM, you can install whichever OS youll be using. You can also script and manage the OS installation process with Windows PowerShell. Using Windows PowerShell to automate these tasks can expedite and streamline the entire process, especially if you have to do this on a regular basis.

Install and Configure Virtual Machines on Hyper-V:


Installing Hyper-V on a server establishes the server as a virtualization server. Each virtual machine you install on the server must be assigned resources to use and then be configured. The number of virtual machines you can run on any individual server depends on the servers hardware configuration and workload. During setup, you specify the amount of memory available to a virtual machine. Although you can change that memory allocation, the amount of memory actively allocated to a virtual machine cannot be used in other ways. To install and configure a virtual machine, complete the following steps: 1. Start Hyper-V Manager by clicking Start, Administrative Tools, Hyper-V Manager. 2. In Hyper-V Manager, right-click the server node in the left pane, point to New, and then select

Virtual Machine. This starts the New Virtual Machine Wizard. Click Next. 3. In the Name text box, enter a name for the virtual machine, such as AppServer02. 4. By default, the virtual machine data is stored on the system disk. To select a different location, select the Store The Virtual Machine In A Different Location check box, click Browse, and then use the Select Folder dialog box to select a save location. Click Next. 5. On the Assign Memory page, specify the amount of memory to allocate to the virtual machine. In most cases, you should reserve at least the minimum amount of memory recommended for the operating system you plan to install. Click Next. 6. On the Configure Networking page, use the Connection list to select a network adapter to use. Each new virtual machine includes a network adapter, and you can configure the adapter to use an available virtual network for communicating with other computers. Click Next. 7. On the Connect Virtual Hard Disk page, use the options provided to name and create a virtual hard disk for the virtual machine. Each virtual machine requires a virtual hard disk so that you can install an operating system and required applications. Click Next. 8. On the Installation Options page, select Install An Operating System From A Boot CD/DVD-ROM. If you have physical distribution media, insert the distribution media, and then specify the CD/DVD drive to use. If you want to install from an .iso image, select Image File, click Browse, and then use the Open dialog box to select the image file to use. 9. Click Next, and then click Finish. 10. In Hyper-V Manager, right-click the name of the virtual machine, and then click Connect. 11. In the Virtual Machine Connection window, click Start. After the virtual machine is initialized, the operating system installation should start automatically. Continue with the operating system installation as you normally would. When the installation is complete, log on to the virtual machine and configure it as you would any other server. From then on, you manage the virtual machine much as you would any other computer, except that you can externally control its state, available resources, and hardware devices using Hyper-V Manager.

Das könnte Ihnen auch gefallen