# vmGroup: name:example directory:C:\ProgramsData\VMware\VirtualMachines\example cloneVMSource: name:Ubuntu22.04.3-dev-000 directory:C:\ProgramsData\VMware\VirtualMachines\basic instances: -name:Ubuntu22.04.3-example-01 hostname:example01 ip :192.168.226.221/24 gateway:192.168.226.2 dns:192.168.226.2,1.1.1.1 hostEntries: -'#The following lines are used for vm_group example' -192.168.226.221example01 -192.168.226.222example02 -192.168.226.223example03 -name:Ubuntu22.04.3-example-02 hostname:example02 ip :192.168.226.222/24 gateway:192.168.226.2 dns:192.168.226.2,1.1.1.1 hostEntries: -'#The following lines are used for vm_group example' -192.168.226.221example01 -192.168.226.222example02 -192.168.226.223example03 -name:Ubuntu22.04.3-example-03 hostname:example03 ip :192.168.226.223/24 gateway:192.168.226.2 dns:192.168.226.2,1.1.1.1 hostEntries: -'#The following lines are used for vm_group example' -192.168.226.221example01 -192.168.226.222example02 -192.168.226.223example03
functionCheckVMStartedStatus{ param ( [string]$vmxPath , [int]$maxChecks = 5 ) $checkCount = 0 do { $runningVMs = & $vmrunPath list $vmStarted = $runningVMs-match [regex]::Escape($vmxPath) $checkCount++ if (-not$vmStarted) { Write-Host"Waiting for the virtual machine to start... ($checkCount/$maxChecks)" Start-Sleep-Seconds5 } } while (-not$vmStarted-and$checkCount-lt$maxChecks) if(-not$vmStarted){ Write-Error"Virtual machine failed to start:$vmxPath" } }
functionDeployVMs{ param( [Parameter(Mandatory=$true)] [object]$config ) $sourceLocation = $config.vmGroup.cloneVMSource.directory $targetLocation = $config.vmGroup.directory if (CheckAllVMsForDeploy -instances$config.vmGroup.instances -sourceLocation$sourceLocation-targetLocation$targetLocation) { foreach ($instancein$config.vmGroup.instances) { $cloneFolderPath = Join-Path$targetLocation$instance.name $sourceVmx = GetVmxPath -directory$sourceLocation-name$config.vmGroup.cloneVMSource.name $destVmx = GetVmxPath -directory$targetLocation-name$instance.name if (-not (Test-Path$cloneFolderPath)) { New-Item-Path$cloneFolderPath-ItemType Directory } Write-Host"Clone virtual machine: $($instance.name)" & $vmrunPath clone $sourceVmx$destVmx full -cloneName="$($instance.name)" Write-Host"Start virtual machine: $($instance.name)" & $vmrunPathstart$destVmx CheckVMStartedStatus -vmxPath$destVmx Write-Host"Modify the virtual machine's hostname: $($instance.name)" & $vmrunPath-T ws -gu$username-gp$password runScriptInGuest $destVmx"""echo '$($instance.hostname)' | tee /etc/hostname && hostname -F /etc/hostname" $netplanConfig = @" network: version: 2 ethernets: ens33: dhcp4: no addresses: [$($instance.ip)] routes: - to: default via: $($instance.gateway) nameservers: addresses: [$($instance.dns)] "@ Write-Host"Modify the virtual machine's network settings: $($instance.name)" & $vmrunPath-T ws -gu$username-gp$password runScriptInGuest $destVmx"""echo '$netplanConfig' | tee /etc/netplan/00-installer-config.yaml" & $vmrunPath-T ws -gu$username-gp$password runScriptInGuest $destVmx"""netplan apply" Write-Host"Modify the virtual machine's hosts: $($instance.name)" foreach ($hostEntryin$instance.hostEntries) { & $vmrunPath-T ws -gu$username-gp$password runScriptInGuest $destVmx"""echo '$hostEntry' | tee -a /etc/hosts" } # Write-Host "重启虚拟机: $($instance.name)" # & $vmrunPath -T ws -gu $username -gp $password runScriptInGuest $destVmx "" "systemctl reboot" } Write-Host"The virtual machine group clone is complete." } else { Write-Error"The Deploy process has stopped due to a failed existence check." } }
functionStartVMs { param( [Parameter(Mandatory=$true)] [object]$config ) $targetLocation = $config.vmGroup.directory if (CheckAllVMs -instances$config.vmGroup.instances -targetLocation$targetLocation) { foreach ($instancein$config.vmGroup.instances) { $destVmx = GetVmxPath -directory$targetLocation-name$instance.name Write-Host"Start the virtual machine: $($instance.name)" & $vmrunPathstart$destVmx } Write-Host"The virtual machine group has been successfully started" } else { Write-Error"The Start process has stopped due to a failed existence check." } }
functionStopVMs { param( [Parameter(Mandatory=$true)] [object]$config ) $targetLocation = $config.vmGroup.directory if (CheckAllVMs -instances$config.vmGroup.instances -targetLocation$targetLocation) { foreach ($instancein$config.vmGroup.instances) { $destVmx = GetVmxPath -directory$targetLocation-name$instance.name Write-Host"Stop the virtual machine: $($instance.name)" & $vmrunPath stop $destVmx } Write-Host"The virtual machine group has been successfully stopped" } else { Write-Error"The Stop process has stopped due to a failed existence check." } }
functionSuspendVMs{ param( [Parameter(Mandatory=$true)] [object]$config ) $targetLocation = $config.vmGroup.directory if (CheckAllVMs -instances$config.vmGroup.instances -targetLocation$targetLocation) { foreach ($instancein$config.vmGroup.instances) { $destVmx = GetVmxPath -directory$targetLocation-name$instance.name Write-Host"Suspend the virtual machine: $($instance.name)" & $vmrunPath suspend $destVmx } Write-Host"The virtual machine group has been successfully suspende." } else { Write-Error"The Suspend process has stopped due to a failed existence check." } }