New-TVM, Återskapa VM’s i Hyper-V om och om igen för lab/lek/test

En liten julklapp från mig till alla er som skapar många VM’s om och om igen för lab/lek/test osv.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Re-Create VM in Hyper-V over and over again..
# By Toft 2013/12/25
$VmVhdLocation = "D:\VMs\Virtual Hard Disks"
 
Function New-TVM
    {
        Param(
        [parameter(mandatory=$true)][ValidateNotNullOrEmpty()]$VMName,
        [parameter(mandatory=$true)][ValidateNotNullOrEmpty()]$VMRam,
        [parameter(mandatory=$true)][ValidateNotNullOrEmpty()]$VMNetwork,
        [parameter(mandatory=$true)][ValidateNotNullOrEmpty()]$VMMacAddress,
        [parameter(mandatory=$true)][ValidateNotNullOrEmpty()]$VMCpu
        ) 
 
        $VM = Get-VM $VMName -ErrorAction SilentlyContinue
 
            if ($VM) {
                if (Get-Process vmconnect -ErrorAction SilentlyContinue | ? MainWindowTitle -match $VmName) {Get-Process vmconnect | ? MainWindowTitle -match $VmName | Stop-Process -Verbose}
                    else {Write-Verbose  "No vmconnect window for $VmName was found" -Verbose}
                for ($a=1; $a -lt 100; $a++) {
                    Write-Progress -Activity "$VMName already exist..." -PercentComplete $a -CurrentOperation "$a% complete" -Status "last chance to abort..."
                    Start-Sleep -Milliseconds 40
                    }
                if ($VM.State -eq "Running") {
                    Stop-VM -Name $VMName -TurnOff -Verbose
                    }
                $VM.Harddrives | Remove-Item -Verbose
                Remove-VM -Name $VMName -Force -Verbose  
                    }
        New-VHD –Path "$VmVhdLocation\$($VMName).vhdx" -SizeBytes 100GB
        Sleep -Milliseconds 300
        New-VM –Name $VMName –MemoryStartupBytes $VMRam –VHDPath "$VmVhdLocation\$($VMName).vhdx" -SwitchName $VMNetwork -Generation 2
        Set-VM $VMName -DynamicMemory -ProcessorCount $VMCpu
        Set-VMNetworkAdapter -VMName $VMName -StaticMacAddress $VMMacAddress
        Start-VM -Name $VMName
        Write-Verbose "VM $VMname was created" -Verbose
 
        # Connect vm
        vmconnect.exe localhost $VMName
}
 
New-TVM -VMName LAB-DC01 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:01" -VMCpu 1
New-TVM -VMName LAB-DC02 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:02" -VMCpu 1
 
New-TVM -VMName LAB-RDS01 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:03" -VMCpu 1
New-TVM -VMName LAB-FS01 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:04" -VMCpu 1
New-TVM -VMName LAB-WEB01 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:05" -VMCpu 1
New-TVM -VMName LAB-RRAS01 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:06" -VMCpu 1
 
New-TVM -VMName LAB-CLIENT01 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:11" -VMCpu 1
New-TVM -VMName LAB-CLIENT02 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:12" -VMCpu 1
New-TVM -VMName LAB-CLIENT03 -VMRam 2048MB -VMNetwork LAN -VMMacAddress "00:15:5A:AA:AA:13" -VMCpu 1
This entry was posted in Powershell. Bookmark the permalink.

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *

*


fyra × 8 =

24 121 Spam Comments Blocked so far by Spam Free Wordpress

Följande HTML-taggar och attribut är tillåtna: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>