feat: implement JSON-based IP configuration scripts for Windows and Linux and integrate service activation in provisioning flow

This commit is contained in:
pbao9
2026-05-15 09:08:44 +07:00
parent 41de3744a8
commit f158a84e8b
7 changed files with 282 additions and 114 deletions
+48
View File
@@ -0,0 +1,48 @@
param(
[string]$VMName,
[string]$IP,
[int]$MaskBits,
[string]$Gateway,
[string]$DNS,
[string]$GuestUser,
[string]$GuestPassword
)
$sec = ConvertTo-SecureString $GuestPassword -AsPlainText -Force
$cred = New-Object pscredential($GuestUser, $sec)
try {
$vm = Get-VM -Name $VMName -ErrorAction Stop
if ($vm.PowerState -ne 'PoweredOn') {
[pscustomobject]@{ status="error"; message="VM is powered off" } | ConvertTo-Json
exit
}
if ($vm.ExtensionData.Guest.ToolsStatus -notin @('toolsOk','toolsOld')) {
[pscustomobject]@{ status="error"; message="VMware Tools not ready" } | ConvertTo-Json
exit
}
$script = @"
`$adapter = Get-NetAdapter | Where-Object { `$_.Status -eq 'Up' } | Select-Object -First 1
Remove-NetIPAddress -InterfaceIndex `$adapter.ifIndex -Confirm:`$false -ErrorAction SilentlyContinue
Remove-NetRoute -InterfaceIndex `$adapter.ifIndex -Confirm:`$false -ErrorAction SilentlyContinue
New-NetIPAddress -InterfaceIndex `$adapter.ifIndex -IPAddress "$IP" -PrefixLength $MaskBits -DefaultGateway "$Gateway"
Set-DnsClientServerAddress -InterfaceIndex `$adapter.ifIndex -ServerAddresses "$DNS"
"@
$res = Invoke-VMScript -VM $vm -ScriptText $script -GuestCredential $cred -ScriptType PowerShell
[pscustomobject]@{
status="ok"
message="IP updated"
ip=$IP
} | ConvertTo-Json
} catch {
[pscustomobject]@{
status="error"
message=$_.Exception.Message
} | ConvertTo-Json
}