Hyper-V : PowerShell création de machine en lot

Le script ci après permet la création de machine virtuelle en lot :

<#
    .SYNOPSIS
        Create Hyper-V VM
    .DESCRIPTION
        Create Hyper-V VM with internal and external vSwitch
    .PARAMETER CSVPath
        Path of the CSV file containing all the VM to create
    .EXAMPLE
        .\HYPERV_New-VMFromCSV -CSVPath "C:\_\VmToCreate.csv"
    .NOTES
        Script tested on : Windows 10 Pro & Enterprise
        Author : Sylver SCHORGEN
        Blog : http://microsofttouch.fr/default/b/sylver
        Created : 22 dec. 2015
        @sylver_schorgen
#>
#
#	RDR Script de creation de VM.
#	Les VHDs sont cree en taille FIXE !
#	Les VM et VHDs sont dans le dossier de la VHDPATH
#	exemple D:\XXXX-ROLE1\..
#
Param(
    [Parameter(Mandatory=$false)]
    [string]$CSVPath
)

#region Variable initialization

Write-Host
Write-Host
Write-Host "Setting up initial variables ... " -NoNewLine

$csvFile = Import-Csv -Path $CSVPath

Write-Host "Ok !" -ForeGroundColor Green
Write-Host

#endregion

#region Modules import

Import-Module Hyper-V

#endregion

#region VM creation

foreach($VM in $csvFile) {
    Write-Host "VM $($VM.VMName) CONFIGURATION"
    Write-Host " -- Creating Virtual Machine $($VM.VMName) ... "
    
    #VM existance verification
    $VMExists = Get-VM -Name $VM.VMName -ErrorAction SilentlyContinue

    if($VMExists -eq $Null) {
        try {
            $VMMemory = "$($VM.VMMemoryMB)" + "MB"
            $VMMemory = $(Invoke-Expression -Command $VMMemory)
            
            #Size modification to be able to create the VMs
            $VHDSize = "$($VM.VHDSizeGB)" + "GB"
            $VHDSize = $(Invoke-Expression -Command $VHDSize)

            #VHD existance verification
            $VHDExists = Get-VHD -Path $VM.VHDPath -ErrorAction SilentlyContinue

            if($VHDExists -eq $Null) {
                
                #VM creation
                # New-VM -Name $VM.VMName -MemoryStartupBytes $VMMemory -NewVHDPath $VM.VHDPath -NewVHDSizeBytes $VHDSize -Generation $VM.VMGeneration
                New-VM -Name $VM.VMName -MemoryStartupBytes $VMMemory -Path $VM.VHDPath -Generation $VM.VMGeneration
                Set-VM -Name $VM.VMName -StaticMemory -ProcessorCount $VM.Processors
				
				# Creation du VHDx
				$vhdname = $VM.VHDName
				$vhdpath = $VM.VHDPath
				$vmname = $VM.VMName
				Write-Host " -- Creation du VHD $($vhdname)"
				New-VHD -Fixed -Path $vhdpath\$vmname\$vhdname.vhdx -SizeBytes $VHDSize
				
				# Attach VHDx
				Add-VMHardDiskDrive -VMName $VM.VMName -Path $vhdpath\$vmname\$vhdname.vhdx
				
				if($VM.VHDName2){
					$vhdname = $VM.VHDName2
					Write-Host " -- Creation du VHD 2 $($vhdname)"
					$vhdname = $VM.VHDName2
					$vhdpath = $VM.VHDPath
					$vmname = $VM.VMName
					
					#Size modification to be able to create the VMs
					$VHDSize = "$($VM.VHDSizeGB2)" + "GB"
					$VHDSize = $(Invoke-Expression -Command $VHDSize)
					
					New-VHD -Fixed -Path $vhdpath\$vmname\$vhdname.vhdx -SizeBytes $VHDSize
				
					# Attach VHDx
					Add-VMHardDiskDrive -VMName $VM.VMName -Path $vhdpath\$vmname\$vhdname.vhdx
				}
				
				
                
                if(($VM.ISOPath -ne $null) -and ($VM.ISOPath -ne "") -and ($VM.ISOPath -ne "")) {
                    Add-VMDvdDrive -VMName $VM.VMName -Path $VM.IsoPath
                }
                
                Write-Host "Ok !" -ForeGroundColor Green
           
                #Removal of the default network adapater                
                Get-VMNetworkAdapter -VMName $VM.VMName | Remove-VMNetworkAdapter

                #Adding external vSwitch if it exists
                if(($VM.ExternalvSwitchName -ne "") -and ($VM.ExternalvSwitchName -ne $null) -and ($VM.ExternalvSwitchName -ne " ")) {
                    Write-Host " -- Configuring Virtual Machine $($VM.VMName) external vSwitch ... " -NoNewLine
                    
                    if((Get-VMSwitch -Name $VM.ExternalvSwitchName -ErrorAction SilentlyContinue) -ne $Null) {
                        Add-VMNetworkAdapter –VMName $VM.VMName –Name "Internet"
                        Connect-VMNetworkAdapter -VMName $VM.VMName -Name "Internet" -SwitchName $VM.ExternalvSwitchName
                        
                        Write-Host "Ok !" -ForeGroundColor Green
                    } else {
                        Write-Host "The external vSwitch $($VM.ExternalvSwitchName) does not exist !" -ForegroundColor Yellow
                    }
                }
                
                #Adding internal vSwitch if it exists
                if(($VM.InternalvSwitchName -ne "") -and ($VM.InternalvSwitchName -ne $null) -and ($VM.InternalvSwitchName -ne " ")) {
                    Write-Host " -- Configuring Virtual Machine $($VM.VMName) internal vSwitch ... " -NoNewLine

                    if((Get-VMSwitch -Name $VM.InternalvSwitchName -ErrorAction SilentlyContinue) -ne $Null) {
                        Add-VMNetworkAdapter –VMName $VM.VMName –Name "Internal"
                        Connect-VMNetworkAdapter -VMName $VM.VMName -Name "Internal" -SwitchName $VM.InternalvSwitchName

                        Write-Host "Ok !" -ForegroundColor Green
                    } else {
                        Write-Host "The internal vSwitch $($VM.InternalvSwitchName) does not exist !" -ForegroundColor Yellow
                    }                                       
                }

                #Adding private vSwitch if it exists
                if(($VM.PrivatevSwitchName -ne "") -and ($VM.PrivatevSwitchName -ne $null) -and ($VM.PrivatevSwitchName -ne " ")) {
                    Write-Host " -- Configuring Virtual Machine $($VM.VMName) private vSwitch ... " -NoNewLine
                    
                    if((Get-VMSwitch -Name $VM.PrivatevSwitchName -ErrorAction SilentlyContinue) -ne $Null) {
                        Add-VMNetworkAdapter –VMName $VM.VMName –Name "Private"
                        Connect-VMNetworkAdapter -VMName $VM.VMName -Name "Private" -SwitchName $VM.PrivatevSwitchName

                        Write-Host "Ok !" -ForegroundColor Green
                    } else {
                        Write-Host "The private vSwitch $($VM.PrivatevSwitchName) does not exist !" -ForegroundColor Yellow
                    } 
                }

                Write-Host

            } else {
                Write-Host "VM does not exist but the VHD does ! Remove or rename the existing VHD first !" -ForeGroundColor Yellow
                Write-Host
            }

            

        } catch {
            Write-Host "An error occured ! Verify that the VM Name, VHD Path, VHD Size, VM Memory and VM Genaration parameters are correctly set !" -ForeGroundColor Red
            Write-Host
        }
    } else {
        Write-Host "Already exists !" -ForeGroundColor Yellow
        Write-Host
    }
}

#endregion

Déclarer les machines virtuelles dans le fichier csv :

IsoPath,VMName,Processors,VMMemoryMB,VHDSizeGB,VMGeneration,InternalvSwitchName,ExternalvSwitchName,PrivatevSwitchName,VHDPath,VHDName,VHDName2,VHDSizeGB2
"","XXXX-ROLE1",4,4096,100,2,"","PROD_VM","","C:\ClusterStorage\Volume1","XXXX-ROLE1-C","",""
"","XXXX-ROLE2",4,4096,60,2,"","PROD_VM","","C:\ClusterStorage\Volume1","XXXX-ROLE2-C","XXXX-ROLE2-D","1000"

L’utilisation se fait directement depuis un hôte Hyper-V avec une fenêtre PowerShell en administrateur

cd <Go-To-Script-Folder>
.\HYPERV_New-VMFromCSV.ps1 -CSVPath "C:\<Folder>\csv-file.csv"

 

 



Related Posts


Altaro backup : installation et utilisation avec Hyper-V

Dans ce tutoriel, je vais vous expliquer comment mettre en place Altaro Backup avec Hyper-V. Présentation Altaro Backup est une solution de sauvegarde pour les environnements virtuels Hyper-V et VMwar

Hyper-V : créer un commutateur virtuel

Présentation Un commutateur virtuel dans Hyper-V est un switch virtuel qui permet la connexion des cartes réseaux créer à l'aide d'Hyper-V (Ordinateur virtuel ou hôte). Sous Hyper-V, il existe 3 types

Hyper-V deplacer les fichiers d’une machine virtuelle

Dans cet vidéo, vous allez voir comment déplacer les fichiers d'une machines virtuelle sous Hyper-V (2012/2012R2/2016). Le déplacement des fichiers peut être fait à chaud, c'est à dire machine virtuel