Aston-HyperV-Code/Host/Create-ALL.ps1
2021-06-30 18:02:29 +02:00

87 lines
3.6 KiB
PowerShell

## Tool to automate VM creation
echo "Shamefully made by Alexandre Simao. Pardon-me dear M. Stallman"
echo "GPLv3"
$VM_list= import-csv ./VM_list.csv -delimiter ","
$i = 0
$j = 1
$VM_Name = $VM_list.VM_name
$Switch_name = $VM_list.Switch_name
$Switch_name2 = $VM_list.Switch_name2
$Switch_Name3 = $VM_list.Switch_name3
$Gen = $VM_list.Gen
$Min_mem = $VM_list.Min_mem
$Mem = $VM_list.Min_mem
$Max_mem = $VM_list.Max_mem
$CPUs = $VM_list.Max_mem
foreach ($VM in $VM_list) {
echo "Adding and configuring ($VM_List.VM_Name[$i])";
if ( ($VM.VM_name[$i]) -Like "SRV*") {
Copy-Item "${env:BASE_VHD_PATH}${env:BASE_SRV}" "${env:VHD_PATH}${VM_NAME}.vhdx"
$Lettre = Mount-VHD -Path $env:VHD_PATH$vm_name.VHDx -PassThru | Get-Disk | Get-Partition | Get-Volume | Sort-Object -Property Size -Descending | Select-Object -First 1;
$DriveLetter = $Lettre.DriveLetter;
$drive = $DriveLetter + ":";
New-Item -Path "${Driveletter}:\conf\" -Name "Base" -ItemType "directory";
Copy-Item "${env:SCRIPTS_PATH}Unattend\FullUnattend-srv.xml" -Destination "$Drive\Windows\Panther\Unattend.xml";
Copy-Item "${env:SCRIPTS_PATH}deploy.cmd" -Destination "${Drive}\conf\deploy.cmd";
mkdir "${Drive}\Tools\Scripts";
Copy-item "${env:SCRIPTS_PATH}Guests\$vm_name.ps1" "${Drive}\Tools\scripts\boot.ps1";
Dismount-VHD "$env:VHD_PATH($VM.VM_name[$i]).vhdx";
New-VM -Name "($VM.VM_name[$i])" -generation "($VM.gen[$i])" -memorystartupbytes "$(VM_list.mem[$i])";
Set-VMHardDiskDrive -VMName "($VM.VM_name[$i])" -Path "$env:VHD_PATH$vm_name.vhdx" -ControllerType SCSI;
Set-VMMemory "($VM.VM_name[$i])" -DynamicMemoryEnabled $true -MinimumBytes $min_mem -StartupBytes $mem -MaximumBytes $max_mem;
Set-VM -Name "($VM.VM_name[$i])" -ProcessorCount "($VM_cpus[$i])"
}
if ( ($VM.VM_name[$i]) -Like "CLI*") {
$Lettre = Mount-VHD -Path $env:VHD_PATH$vm_name.VHDx -PassThru | Get-Disk | Get-Partition | Get-Volume | Sort-Object -Property Size -Descending | Select-Object -First 1;
$DriveLetter = $Lettre.DriveLetter;
$drive = $DriveLetter + ":";
Copy-Item "${env:SCRIPTS_PATH}Unattend\FullUnattend-Win10.xml" -Destination "$Drive\Windows\Panther\Unattend.xml";
Copy-Item "${env:SCRIPTS_PATH}deploy.cmd" -Destination "${Drive}\conf\deploy.cmd";
mkdir "${drive}:\Program Files\BGINFO";
Copy-Item "${env:soft_path}"BGInfo_Deploiement\BGInfo.exe "${drive}\Program Files\BGINFO";
Copy-Item "${env:soft_path}"BGInfo_Deploiement\mescouilles2.bgi "${drive}\Program Files\BGINFO";
Copy-Item "${env:soft_path}"BGInfo_Deploiement\bginfox86.bat "${drive}\Users\AdminLocal\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup";
}
Dismount-VHD "$env:VHD_PATH$vm_name.vhdx";
New-VM -Name "($VM.VM_name[$i])" -generation "($VM.gen[$i])" -memorystartupbytes "$(VM_list.mem[$i])";
Set-VMHardDiskDrive -VMName "($VM.VM_name[$i])" -Path "$env:VHD_PATH$vm_name.vhdx" -ControllerType SCSI;
Set-VMMemory "($VM.VM_name[$i])" -DynamicMemoryEnabled $true -MinimumBytes $min_mem -StartupBytes $mem -MaximumBytes $max_mem;
Set-VM -Name "($VM.VM_name[$i])" -ProcessorCount "($VM_cpus[$i])"
echo "Configuring adapters";
Rename-VMNetworkAdapter -VMName "($VM.VM_name[$i])" -NewName "($VM.switch_name[$i])";
Set-VMNetworkAdapter -VMName "($VM.VM_name[$i])" -Name "($VM.switch_name[$i])" -DeviceNaming on;
while ( ("$VM.switch_name$j"[$i]) -ne $null) {
Add-VMNetworkAdapter -VMName "$vm_name" -SwitchName "$switch_name$j" -Name "$switch_name$j" -DeviceNaming on;
$j++
}
$i++
}
### Starting VMS
echo "Starting VMs"
Start-VM -Name RTR*
### Sleeping 15s
echo "Sleeping 15s"
Sleep 15
Start-VM -Name SRV*