Add VM rebuild pipeline with isolated job chain and admin log API

This commit is contained in:
baodev0
2026-05-29 18:19:38 +07:00
parent 7ae237ee9d
commit 9c68273b4a
27 changed files with 2391 additions and 199 deletions
+63 -35
View File
@@ -1,48 +1,76 @@
# assignIPWindows.ps1
# Deploy to: C:\CLISave\AssignIP\assignIPWindows.ps1 on the vSphere Windows host
#
# Usage: powershell -ExecutionPolicy Bypass -NonInteractive -File assignIPWindows.ps1 \
# <vm_id> <ip> <gateway> <cidr> <vcenter_host> <vcenter_user> <vcenter_password> <guest_password>
#
# Output: SUCCESS | FAIL (exactly one line)
param(
[string]$VMName,
[string]$IP,
[int]$MaskBits,
[string]$Gateway,
[string]$DNS,
[string]$GuestUser,
[string]$GuestPassword
[Parameter(Mandatory = $true)][string]$VmId,
[Parameter(Mandatory = $true)][string]$Ip,
[Parameter(Mandatory = $true)][string]$Gateway,
[Parameter(Mandatory = $true)][string]$Cidr,
[Parameter(Mandatory = $true)][string]$VCenterHost,
[Parameter(Mandatory = $true)][string]$VCenterUser,
[Parameter(Mandatory = $true)][string]$VCenterPassword,
[Parameter(Mandatory = $true)][string]$GuestPassword
)
$sec = ConvertTo-SecureString $GuestPassword -AsPlainText -Force
$cred = New-Object pscredential($GuestUser, $sec)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
try {
$vm = Get-VM -Name $VMName -ErrorAction Stop
Import-Module VMware.VimAutomation.Core -ErrorAction Stop
if ($vm.PowerState -ne 'PoweredOn') {
[pscustomobject]@{ status="error"; message="VM is powered off" } | ConvertTo-Json
exit
}
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -Confirm:$false | Out-Null
if ($vm.ExtensionData.Guest.ToolsStatus -notin @('toolsOk','toolsOld')) {
[pscustomobject]@{ status="error"; message="VMware Tools not ready" } | ConvertTo-Json
exit
}
Connect-VIServer -Server $VCenterHost -User $VCenterUser -Password $VCenterPassword -Force -ErrorAction Stop | Out-Null
$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"
$vm = Get-VM -Id "VirtualMachine-$VmId" -ErrorAction Stop
# PowerShell script executed inside the Windows guest via VMware Tools
$guestScript = @"
$ErrorActionPreference = 'Stop'
try {
# Find the first active ethernet adapter that is not a loopback
$adapter = Get-NetAdapter |
Where-Object { $_.Status -eq 'Up' -and $_.InterfaceDescription -notlike '*Loopback*' } |
Sort-Object -Property InterfaceIndex |
Select-Object -First 1
if (-not $adapter) {
throw 'No active network adapter found'
}
# Remove existing IPv4 addresses and routes on this interface
$adapter |
Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue |
Remove-NetIPAddress -Confirm:$false -ErrorAction SilentlyContinue
Get-NetRoute -InterfaceIndex $adapter.InterfaceIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue |
Remove-NetRoute -Confirm:$false -ErrorAction SilentlyContinue
# Set new static IP
New-NetIPAddress -InterfaceIndex $adapter.InterfaceIndex -IPAddress '$Ip' -PrefixLength $Cidr -DefaultGateway '$Gateway' -ErrorAction Stop | Out-Null
Write-Output 'SUCCESS'
} catch {
Write-Output 'FAIL'
}
"@
$res = Invoke-VMScript -VM $vm -ScriptText $script -GuestCredential $cred -ScriptType PowerShell
[pscustomobject]@{
status="ok"
message="IP updated"
ip=$IP
} | ConvertTo-Json
$result = Invoke-VMScript -VM $vm -ScriptText $guestScript -GuestUser "administrator" -GuestPassword $GuestPassword -ScriptType Powershell -ErrorAction Stop
if ($result.ScriptOutput -match "SUCCESS") {
Write-Output "SUCCESS"
} else {
Write-Output "FAIL"
}
} catch {
[pscustomobject]@{
status="error"
message=$_.Exception.Message
} | ConvertTo-Json
Write-Output "FAIL"
Write-Output $_.Exception.Message
} finally {
Disconnect-VIServer -Confirm:$false -ErrorAction SilentlyContinue | Out-Null
}