Packer.Images/scripts/Remove-Resources.ps1

51 lines
1.3 KiB
PowerShell
Raw Normal View History

[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[string]$VMName,
[Parameter(Mandatory)]
[string]$VSphereFQDN,
[Parameter(Mandatory)]
[string]$VSphereUsername,
[Parameter(Mandatory)]
[string]$VSpherePassword
)
$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
2021-02-13 19:16:31 +00:00
$GetVMSplat = @{
Name = "$($VMName)*"
2021-02-13 19:16:31 +00:00
ErrorAction = 'SilentlyContinue'
}
If ([boolean](Get-VM @GetVMSplat)) {
$RemoveVMSplat = @{
VM = Get-VM @GetVMSplat
2021-02-13 19:16:31 +00:00
DeletePermanently = $True
Confirm = $False
ErrorAction = 'SilentlyContinue'
}
Remove-VM @RemoveVMSplat
}
2021-02-13 19:16:31 +00:00
Disconnect-VIServer * -Confirm:$False
$RemoveItemSplat = @{
Path = '/scratch/*'
Recurse = $True
Force = $True
Confirm = $False
}
Remote-Item @RemoveItemSplat