Dies sind maschinell übersetzte Inhalte, die von den Mitgliedern der Community bearbeitet werden können. Sie können die Übersetzung verbessern, indem Sie auf den jeweils zum Satz gehörenden Link "Bearbeiten" klicken.Mithilfe des Dropdown-Steuerelements "Inhalt anzeigen" links oben auf der Seite können Sie zudem bestimmen, ob nur der englische Originaltext, nur die deutsche Übersetzung oder beides nebeneinander angezeigt werden.
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 you’re 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 you’re 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:
-
Install the Hyper-V role and Windows PowerShell.
You can do this from the Server Manager or by using the ServerManagerCmd.exe command-line tool (ServerManagerCmd.exe –install Hyper-V and ServerManagerCmd.exe –install PowerShell).
-
Configure Windows PowerShell to allow the use of scripts.
This is disabled by default.
You can configure this with the Set-ExecutionPolicy cmdlet (Set-ExecutionPolicyRemoteSigned).
-
Download and install the Windows PowerShell Hyper-V module.
You need this module to access all the cmdlets designed specifically for Windows PowerShell.
From the CodePlex PowerShell Management Library for Hyper-V page, download the latest stable, non-development release.
Make sure the “block” attribute is removed from the zipped files before you install them.
This will execute setup scripts without needing digital signatures.
If you’re doing the install on Server Core, unblock and unzip the module file on another computer before copying the setup files to the server.
Otherwise, you’ll need additional tools to perform these operations on the server (the stream.exe tool on SysinternalsSuite and 7-Zip).
-
From an elevated Windows PowerShell command prompt, enable the Hyper-V cmdlets by importing the newly installed module (Import-Module HyperV).
If you get an error message, verify that you didn’t miss a previous step.
If you intend to regularly use Windows PowerShell to manage your Hyper-V environment, you should add the Import-Module and Set-ExecutionPolicy cmdlets to a Windows PowerShell profile file.
This will let you use the cmdlets without having to configure the server each time.
You should always use an elevated Windows PowerShell command environment to avoid any issues running the cmdlets.
To verify you’ve successfully installed the cmdlets and they’re functioning properly, use the Get-Command 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).
There’s 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 isn’t provided.
Figure 1 Descriptions of the variables defined during Hyper-V virtual machine (VM) setup.
|
$SRV1
|
VM name
|
|
$SRAM
|
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 don’t 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 you’ve configured the new VM, you can install whichever OS you’ll 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.
Neil R.Tucker
, MCT, MCITP, MCDBA, MCSE, MCDST, has more than 17 years of experience as a technician, trainer and technical writer. He consults and teaches classes on SQL Server, Windows Server, Windows 7 and Windows PowerShell. He is also the author of the Microsoft Learning 50331 Course for Windows 7 Desktop Support Technicians.
Related Content
|
Virtualisierung: Erstellen Sie virtuelle Hyper-V-Computer mit Windows PowerShell
Wenn Sie in regelmäßigen Abständen virtuelle Computer erstellen und konfigurieren müssen, können Sie diesen Prozess mit Windows PowerShell beschleunigen.
Neil Tucker
Ob neue oder standardisierten Server-Konfigurationen testen müssen, bietet Hyper-V Ihnen eine Umgebung schnell und effizient ausführen Testkonfigurationen, die es eine beliebten Testumgebung gemacht hat.
Der einzige Haken ist, müssen Sie zum Erstellen und Konfigurieren von virtuellen Maschinen (VMs) auf einer regelmäßigen Basis.
Wenn Sie eine einfache Möglichkeit suchen, Hyper-V-Abbilder erstellen, werden die Windows PowerShell-Skripts eine praktikable Lösung.
Das Ausmaß, das Sie Windows PowerShell und seiner flexiblen Kommandostruktur anpassen können, macht es geeignet für jede Umgebung.
In Kombination mit einer Strategie für automatische oder unbeaufsichtigte Installation ist praktisch jede Computerkonfiguration möglich.
Konfigurieren von Windows PowerShell
Diese Strategie setzt voraus, dass Sie auf einem Windows Server 2008 R2-System arbeiten, die die Anforderungen für die Hyper-V-Rolle.
Nach Ausführen dieser Schritte, wird das System für die Skripterstellung von Hyper-V bereit sein:
-
Installieren Sie die Hyper-V-Rolle und Windows PowerShell.
Sie können dies tun, vom Server-Manager oder das Befehlszeilentool ServerManagerCmd.exe verwenden (ServerManagerCmd.exe –install Hyper-V und ServerManagerCmd.exe –install PowerShell).
-
Konfigurieren Sie Windows PowerShell ermöglicht die Verwendung von Skripts.
Dies ist standardmäßig deaktiviert.
Sie können dies mit dem Set-ExecutionPolicy-Cmdlet (Set-ExecutionPolicyRemoteSigned) konfigurieren.
-
Downloaden Sie und installieren Sie das Windows PowerShell Hyper-V-Modul.
Sie benötigen dieses Modul alle speziell für die Windows PowerShell Cmdlets zugreifen.
Von der CodePlex PowerShell Management Library für Hyper-V-Seite, laden Sie die letzte stabile, nicht-Entwicklung Version.
Stellen Sie sicher, dass die gezippten Dateien das Attribut "Block" entfernt wird, bevor Sie sie installieren.
Ohne digitale Signaturen wird diese Setup-Skripten auszuführen.
Wenn Sie die Server Core-Installation durchführen, entsperren Sie und entpacken Sie die Moduldatei auf einem anderen Computer vor die Setup-Dateien auf den Server kopiert.
Andernfalls benötigen Sie zusätzliche Tools zum Ausführen dieser Vorgänge auf dem Server (das stream.exe-Tool auf SysinternalsSuite und 7-Zip).
-
Aktivieren Sie aus einer Eingabeaufforderung von Windows PowerShell die Hyper-V-Cmdlets importieren (Import-Module HyperV) das neu installierte Modul.
Wenn Sie eine Fehlermeldung erhalten, stellen Sie sicher, dass Sie einen vorherigen Schritt nicht vermissen.
Wenn Sie regelmäßig mithilfe von Windows PowerShell zum Verwalten der Hyper-V Umgebung möchten, sollten Sie das Import-Module und -Cmdlets Set-ExecutionPolicy in eine Windows PowerShell-Profil-Datei hinzufügen.
Auf diese Weise können Sie mithilfe der Cmdlets zum Konfigurieren des Servers jedes Mal ohne.
Sie sollten immer eine erhöhte Windows PowerShell-Befehl-Umgebung verwenden, zum Ausführen der Cmdlets Probleme zu vermeiden.
Um zu überprüfen Sie die Cmdlets erfolgreich installiert haben und sie sind richtig funktioniert, verwenden Sie das Cmdlet Get-Command finden Sie die Liste der Befehle (Get-Command –Module Hyper-V) zur Verfügung.
Wie bei anderen Modulen kommt jedes Cmdlet mit hilfreiche Informationen über die Funktionen und Beispielen fort wie zu Verwendung es (Get-Help New-VM –Detailed und Get-Help New-VM –Examples).
Gibt es zusätzlicher Dokumentation auf CodePlex.
Konfigurieren Sie die Hyper-V-Windows PowerShell-Skript
Wenn Ihre Windows PowerShell-Umgebung bereit ist, können Sie beginnen, neue VMs zu erstellen.
Stellen Sie sicher, dass Sie mit Administratorberechtigungen für diese Befehle mit erhöhte rechten starten.
Hyper-V-Cmdlets verwendet das Skript zum Erstellen einer neuen VM basierend auf fünf Variablen (siehe Abbildung 1) geben Sie während des Setups.
Jede Variable hat einen zugeordnete Standardwert, der verwendet wird, wenn eine angegeben ist nicht.
Abbildung 1 Beschreibungen der Variablen während des Hyper-V Virtual Machine (VM) Setups definiert.
|
$SRV1
|
VM-name
|
|
$SRAM
|
Größe des Arbeitsspeichers der VM zugewiesen
|
|
$SRV1VHD
|
Größe der virtuellen Festplatte der VM verwendet
|
|
$VMLOC
|
Lage von wo Sie die VM virtuelle Festplatte erstellen
|
|
$Network1
|
VM-Verbindung virtuelles Netzwerk
|
Nach dem Definieren von Variablen, das Skript New-Image.ps1 (siehe Abbildung 2) konfiguriert das virtuelle Hyper-V-Netzwerk mit dem Wert der Variablen $Network1 zugewiesen.
Vor dem Definieren des neuen privaten Netzwerks mit dem New-VMPrivateSwitchcmdlet, entfernen Sie es mit Remove-VMSwitch, ob es bereits vorhanden war oder nicht.
Dadurch wird sichergestellt, dass Sie keine doppelten Netzwerke mit demselben Namen definieren.
Abbildung 2 The New-Image.ps1-Skript, das neuen virtuelle Maschinen erstellt.
# 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
Der letzte Teil des Prozesses, in dem Sie tatsächlich die VM erstellen, ist einfach.
Erstellen Sie den Verzeichnispfad für die virtuelle Festplatte (VHD).
Dann sechs Cmdlets erstellen und konfigurieren die neue VM.
Der Befehl New-VM definiert die Maschine und seiner Lage.
Neue VHD erstellt die VHD-Datei und hinzufügen-VMDisk weist sie die VM.
Add-VMDrive fügt ein DVD-Laufwerk auf dem Computer, Set-VMMemory definiert die Größe des Arbeitsspeichers und Add-VMNIC-konfiguriert die Netzwerkkarte.
Sobald Sie den neuen virtuellen Computer konfiguriert haben, können Sie welche OS installieren, die Sie verwenden möchten.
Sie können auch ein Skript und verwalten die OS-Installation mit Windows PowerShell.
Mithilfe von Windows PowerShell zur Automatisierung dieser Aufgaben kann beschleunigen und optimieren den gesamten Prozess, vor allem, wenn Sie dies regelmäßig tun.
Neil R.Tucker
, MCT, MCITP, MCDBA, MCSE, MCDST, hat mehr als 17 Jahre Erfahrung als Techniker, Trainer und Fachautor. Er berät und unterrichtet auf SQL Server, Windows Server, Windows 7 und Windows PowerShell. Er ist auch Autor von die Microsoft Learning 50331 Kurs für Windows 7 Desktop Support-Technikern.
Verwandter Inhalt
|
|
TechNet Features
TechNet SubscriptionsSie sind kein TechNet-Abonnent? Evaluieren Sie Microsoft-Software und planen Sie Bereitstellungen mit einem Microsoft TechNet-Abonnement.
|