26 Sep2014
Hyper-V 2012: Créer une ou plusieurs VM en PowerShell
Bonjour à tous,
Un petit post pour vous simplifier la création rapide d'une VM en PowerShell.
J'en avais marre de faire cela via l'interface qui me prenait trop de temps pour monter mon lab.
Je pars donc avec les hypothèses suivantes :
- Nom de la VM : VM2012R2-1
- VirtualSwitch : Internal
- Taille du VHDX : 60Go
- RAM dynamique : Oui
- Nombre de CPU : 4
New-VM -Name VM2012R2-1 -SwitchName Internal -MemoryStartupBytes 512MB -NewVHDSizeBytes 60GB -Generation 2-NewVHDPath C:\VM\INET.vhdx | Set-VMMemory -DynamicMemoryEnabled $trueOn peut aussi passer par un fichier CSV pour en faire plusieurs en masse :
Get-VM -Name VM2012R2-1 | Set-VMProcessor -Count 4
$csv = Import-CSV Create-BulkVM.csv -Delimiter ";"
$vhdPath = "C:\HYPER-V\Virtual Hard Disks"
$csv | % {
Write-Host "Deploying new VM: $($_.Name)" -NoNewLine
try {
if([bool]((Get-VM).Name -eq $_.Name) -eq $false) {
if($_.DynamicRAM -eq "TRUE") {
if($_.VSwitch1 -AND !($_.VSwitch2)) {
New-VM -Name $_.Name -SwitchName $_.VSwitch1 -MemoryStartupBytes 512MB -NewVHDSizeBytes (1GB*$_.HDDSize_GB) -Generation $_.VMGeneration -NewVHDPath "$vhdPath\$($_.Name).vhdx" | Out-Null
}
if($_.VSwitch1 -AND $_.VSwitch2) {
New-VM -Name $_.Name -SwitchName $_.VSwitch1,$_.VSwitch2 -MemoryStartupBytes 512MB -NewVHDSizeBytes (1GB*$_.HDDSize_GB) -Generation $_.VMGeneration -NewVHDPath "$vhdPath\$($_.Name).vhdx" | Out-Null
}
Get-VM -Name $_.Name | Set-VM -DynamicMemory | Out-Null
} elseif($_.DynamicRAM -eq "FALSE") {
New-VM -Name $_.Name -SwitchName $_.VSwitch -NewVHDSizeBytes (1GB*$_.HDDSize_GB) -Generation $_.VMGeneration -NewVHDPath "$vhdPath\$($_.Name).vhdx" | Out-Null
Get-VM -Name $_.Name | Set-VM -MemoryStartupBytes (1GB*$_.nbRAM_GB) | Out-Null
}
$vmToUpdate = Get-VM -Name $_.Name
$vmToUpdate | Set-VM -ProcessorCount $_.nbCPU | Out-Null
switch($_.VMGeneration) {
"1" {$vmToUpdate | Set-VMBios -StartupOrder @("IDE","CD","LegacyNetworkAdapter")}
"2" {$vmToUpdate | Set-VMFirmware -EnableSecureBoot On -BootOrder $vmToUpdate.DVDDrives[0],$vmToUpdate.HardDrives[0],$vmToUpdate.NetworkAdapters[0]}
default {}
}
Write-Host " done" -foregroundcolor:Green
} else {
Write-Host " already exists" -foregroundcolor:Cyan
}
} catch [Exception] {
Write-Host " fail" -foregroundcolor:Red
Write-Host $error[0].Exception.Message -foregroundcolor:Red
}
}
Avec comme fichier d'entrée un CSV comme suit :
Name | nbCPU | nbRAM_GB | DynamicRAM | HDDSize_GB | VSwitch1 | VSwitch2 | VMGeneration |
---|---|---|---|---|---|---|---|
VM2012R2-1 | 4 | TRUE | 60 | Internal | 2 | ||
VM2012R2-2 | 4 | TRUE | 60 | Internal | 2 | ||
VM2012R2-3 | 4 | TRUE | 60 | Internal | 2 | ||
VM2012R2-4 | 4 | TRUE | 60 | Internal | External | 2 | |
VM2012R2-5 | 4 | TRUE | 60 | Internal | External | 2 | |
VM2012R2-6 | 4 | 2 | FALSE | 60 | Internal | 2 | |
VM2012R2-7 | 4 | 2 | FALSE | 60 | Internal | 2 | |
VM2012R2-8 | 4 | 2 | FALSE | 60 | Internal | 2 | |
VM2012R2-9 | 4 | 2 | FALSE | 60 | Internal | 2 | |
VM2012R2-10 | 4 | 2 | FALSE | 60 | Internal | 2 |
Et comme résultat :
Résultat PowerShell pour la création complète | Résultat PowerShell si des VM existent déjà |
![]() |
![]() |
Les fichiers utilisés pour la démo :
Mots-clés: powershell,, hyper-v,, virtual machine,, vm,
Comments powered by CComment