Packer.Images/scripts/Remove-Resources.ps1
djpbessems b3e33a7027
All checks were successful
continuous-integration/drone/push Build is passing
Avoid race condition between builders
2021-02-14 12:32:40 +01:00

53 lines
1.3 KiB
PowerShell

[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[string]$VMName,
[Parameter(Mandatory)]
[string]$VSphereFQDN,
[Parameter(Mandatory)]
[string]$VSphereUsername,
[Parameter(Mandatory)]
[string]$VSpherePassword,
[Parameter(Mandatory)]
[string]$BuildName
)
$PowerCliConfigurationSplat = @{
Scope = 'User'
ParticipateInCEIP = $False
Confirm = $False
InvalidCertificateAction = 'Ignore'
}
Set-PowerCLIConfiguration @PowerCliConfigurationSplat | Out-Null
$ConnectVIServerSplat = @{
Server = $VSphereFQDN
User = "$VSphereUsername"
Password = "$VSpherePassword"
WarningAction = 'SilentlyContinue'
}
Connect-VIServer @ConnectVIServerSplat | Out-Null
$GetVMSplat = @{
Name = "$($VMName)*"
ErrorAction = 'SilentlyContinue'
}
If ([boolean](Get-VM @GetVMSplat)) {
$RemoveVMSplat = @{
VM = Get-VM @GetVMSplat
DeletePermanently = $True
Confirm = $False
ErrorAction = 'SilentlyContinue'
}
Remove-VM @RemoveVMSplat
}
Disconnect-VIServer * -Confirm:$False
$RemoveItemSplat = @{
Path = "/scratch/$($BuildName)/*"
Recurse = $True
Force = $True
Confirm = $False
}
Remove-Item @RemoveItemSplat