198 lines
7.9 KiB
PowerShell
198 lines
7.9 KiB
PowerShell
## Tool to automate VM creation
|
|
|
|
echo "Shamefully made by Alexandre Simao. Pardon-me dear M. Stallman"
|
|
echo "GPLv3"
|
|
|
|
### Considered as finished. Need improvement VM side.
|
|
|
|
### Adding SRV-01
|
|
|
|
$vm_name = "SRV-01"
|
|
$switch_name = "Arc-SRV"
|
|
$gen = 2
|
|
$mem = 1024MB
|
|
$min_mem = 512MB
|
|
$max_mem = 2048MB
|
|
$cpus = 2
|
|
|
|
echo "Adding and configuring $vm_name"
|
|
cp "$env:BASE_VHD_PATH$env:BASE_SRV_NAME" "$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 "${Driveletter}:\Tools\Scripts"
|
|
Copy-item "${env:SCRIPTS_PATH}Guests\$vm_name.ps1" "${Drive}\Tools\scripts\boot.ps1"
|
|
|
|
Dismount-VHD "$env:VHD_PATH$vm_name.vhdx"
|
|
|
|
New-VM -Name "$vm_name" -generation "$gen" -memorystartupbytes $mem
|
|
Set-VMHardDiskDrive -VMName "$vm_name" -Path "$env:VHD_PATH$vm_name.vhdx" -ControllerType SCSI
|
|
Set-VMMemory "$vm_name" -DynamicMemoryEnabled $true -MinimumBytes $min_mem -StartupBytes $mem -MaximumBytes $max_mem
|
|
Set-VM -Name "$vm_name" -ProcessorCount "$cpus"
|
|
|
|
echo "Configuring adapters"
|
|
Rename-VMNetworkAdapter -VMName "$vm_name" -NewName "$switch_name"
|
|
Set-VMNetworkAdapter -VMName "$vm_name" -Name "$switch_name" -DeviceNaming on
|
|
|
|
### Adding SRV-03
|
|
|
|
$vm_name = "SRV-01"
|
|
$switch_name = "Arc-SRV"
|
|
$gen = 2
|
|
$mem = 1024MB
|
|
$min_mem = 512MB
|
|
$max_mem = 2048MB
|
|
$cpus = 2
|
|
|
|
echo "Adding and configuring $vm_name"
|
|
cp "$env:BASE_VHD_PATH$env:BASE_SRV_NAME" "$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 "${Driveletter}:\Tools\Scripts"
|
|
Copy-item "${env:SCRIPTS_PATH}Guests\$vm_name.ps1" "${Drive}\Tools\scripts\boot.ps1"
|
|
|
|
Dismount-VHD "$env:VHD_PATH$vm_name.vhdx"
|
|
|
|
New-VM -Name "$vm_name" -generation "$gen" -memorystartupbytes $mem
|
|
Set-VMHardDiskDrive -VMName "$vm_name" -Path "$env:VHD_PATH$vm_name.vhdx" -ControllerType SCSI
|
|
Set-VMMemory "$vm_name" -DynamicMemoryEnabled $true -MinimumBytes $min_mem -StartupBytes $mem -MaximumBytes $max_mem
|
|
Set-VM -Name "$vm_name" -ProcessorCount "$cpus"
|
|
|
|
echo "Configuring adapters"
|
|
Rename-VMNetworkAdapter -VMName "$vm_name" -NewName "$switch_name"
|
|
Set-VMNetworkAdapter -VMName "$vm_name" -Name "$switch_name" -DeviceNaming on
|
|
|
|
### Adding and configuring RTR-01
|
|
|
|
$vm_name = "RTR-01"
|
|
$switch_name = "Arc-CLI"
|
|
$switch_name2 = "Arc-SRV"
|
|
$switch_name3 = "WAN"
|
|
$gen = 2
|
|
$mem = 1024MB
|
|
$min_mem = 512MB
|
|
$max_mem = 2048MB
|
|
$cpus = 2
|
|
|
|
echo "Adding and configuring $vm_name"
|
|
cp "$env:BASE_VHD_PATH$env:BASE_SRV_NAME" "$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 "${Driveletter}:\Tools\Scripts"
|
|
Copy-item "${env:SCRIPTS_PATH}Guests\$vm_name.ps1" "${Drive}\Tools\scripts\boot.ps1"
|
|
|
|
Dismount-VHD "$env:VHD_PATH$vm_name.vhdx"
|
|
|
|
New-VM -Name "$vm_name" -generation "$gen" -memorystartupbytes $mem
|
|
Set-VMHardDiskDrive -VMName "$vm_name" -Path "$env:VHD_PATH$vm_name.vhdx" -ControllerType SCSI
|
|
Set-VMMemory "$vm_name" -DynamicMemoryEnabled $true -MinimumBytes $min_mem -StartupBytes $mem -MaximumBytes $max_mem
|
|
Set-VM -Name "$vm_name" -ProcessorCount "$cpus"
|
|
|
|
echo "Configuring adapters"
|
|
Rename-VMNetworkAdapter -VMName "$vm_name" -NewName "$switch_name"
|
|
Set-VMNetworkAdapter -VMName "$vm_name" -Name "$switch_name" -DeviceNaming on
|
|
|
|
echo "Configuring adapters"
|
|
Rename-VMNetworkAdapter -VMName "$vm_name" -NewName "$switch_name"
|
|
Set-VMNetworkAdapter -VMName "$vm_name" -Name "$switch_name" -DeviceNaming on
|
|
Add-VMNetworkAdapter -VMName "$vm_name" -SwitchName "$switch_name2" -Name "Arc-SRV" -DeviceNaming on
|
|
Add-VMNetworkAdapter -VMName "$vm_name" -SwitchName "$switch_name3" -Name "WAN" -DeviceNaming on
|
|
|
|
# RTR-02
|
|
|
|
$vm_name = "RTR-02"
|
|
$switch_name = "Bou-LAN"
|
|
$switch_name2 = "WAN"
|
|
$gen = 2
|
|
$mem = 1024MB
|
|
$min_mem = 512MB
|
|
$max_mem = 2048MB
|
|
$cpus = 2
|
|
|
|
echo "Adding and configuring $vm_name"
|
|
cp "$env:BASE_VHD_PATH$env:BASE_SRV_NAME" "$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 "${Driveletter}:\Tools\Scripts"
|
|
Copy-item "${env:SCRIPTS_PATH}Guests\$vm_name.ps1" "${Drive}\Tools\scripts\boot.ps1"
|
|
|
|
Dismount-VHD "$env:VHD_PATH$vm_name.vhdx"
|
|
|
|
New-VM -Name "$vm_name" -generation "$gen" -memorystartupbytes $mem
|
|
Set-VMHardDiskDrive -VMName "$vm_name" -Path "$env:VHD_PATH$vm_name.vhdx" -ControllerType SCSI
|
|
Set-VMMemory "$vm_name" -DynamicMemoryEnabled $true -MinimumBytes $min_mem -StartupBytes $mem -MaximumBytes $max_mem
|
|
Set-VM -Name "$vm_name" -ProcessorCount "$cpus"
|
|
|
|
echo "Configuring adapters"
|
|
Rename-VMNetworkAdapter -VMName "$vm_name" -NewName "$switch_name"
|
|
Set-VMNetworkAdapter -VMName "$vm_name" -Name "$switch_name" -DeviceNaming on
|
|
Add-VMNetworkAdapter -VMName "$vm_name" -SwitchName "$switch_name2" -Name "$switch_name2" -DeviceNaming on
|
|
|
|
# RTR-03
|
|
|
|
$vm_name = "RTR-03"
|
|
$switch_name = "Aston"
|
|
$switch_name3 = "WAN"
|
|
$gen = 2
|
|
$mem = 1024MB
|
|
$min_mem = 512MB
|
|
$max_mem = 2048MB
|
|
$cpus = 2
|
|
|
|
echo "Adding and configuring $vm_name"
|
|
cp "$env:BASE_VHD_PATH$env:BASE_SRV_NAME" "$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 "${Driveletter}:\Tools\Scripts"
|
|
Copy-item "${env:SCRIPTS_PATH}Guests\$vm_name.ps1" "${Drive}\Tools\scripts\boot.ps1"
|
|
|
|
Dismount-VHD $env:VHD_PATH$vm_name.vhdx
|
|
|
|
New-VM -Name "$vm_name" -generation "$gen" -memorystartupbytes $mem
|
|
Set-VMHardDiskDrive -VMName "$vm_name" -Path "$env:VHD_PATH$vm_name.vhdx" -ControllerType SCSI
|
|
Set-VMMemory "$vm_name" -DynamicMemoryEnabled $true -MinimumBytes $min_mem -StartupBytes $mem -MaximumBytes $max_mem
|
|
Set-VM -Name "$vm_name" -ProcessorCount "$cpus"
|
|
|
|
echo "Configuring adapters"
|
|
Rename-VMNetworkAdapter -VMName "$vm_name" -NewName "$switch_name"
|
|
Set-VMNetworkAdapter -VMName "$vm_name" -Name "$switch_name" -DeviceNaming on
|
|
Add-VMNetworkAdapter -VMName "$vm_name" -SwitchName "$switch_name2" -Name "$switch_name2" -DeviceNaming on
|
|
|
|
### Starting VMS
|
|
|
|
echo "Starting VMs"
|
|
Start-VM -Name RTR*
|
|
|
|
### Sleeping 15s
|
|
|
|
echo "Sleeping 15s"
|
|
|
|
Sleep 15
|
|
|
|
|
|
Start-VM -Name SRV*
|