if ( (Get-PSSnapin -Name VMware.VimAutomation.Core) -eq $null ) {
Add-PSSnapin -Name VMware.VimAutomation.Core
}
$cred = Get-VICredentialStoreItem -File c:\Scripts\cred.xml
Connect-VIServer "1.1.1.1" -User $cred.user -Password $cred.password
$today = Get-Date
$Removed = @()
$NotRemoved = @()
Get-VM | where {$_.name -cmatch "Delete_\d\d\.\d\d\.\d\d"} | foreach {
[datetime]$dt=($_.name.Split("_",2)[1])
if ($dt.Month -eq $today.Month -and $dt.Day -eq $today.Day -and $dt.Year -eq $today.Year) {
$Removed += $_.name
$Removed | Out-File C:\Scripts\deleted.txt -Append #this is new, and it does not work
Remove-VM -VM -DeletePermanently $_ -Confirm:$false
}
else
{
$NotRemoved += $_.name
}
}
(Send-MailMessage -SmtpServer '1.2.3.4' -From "<m@a.com>" -To "<m@acom>" -Subject VMs -Body "VMs that have been deleted: `n `n $($NotRemoved | out-string)")
(Send-MailMessage -SmtpServer '1.2.3.4' -From "<@com>" -To "<@a.com>" -Subject VMs -Body "VMs that are/were scheduled to be deleted, but the dates don't match: `n `n $($Removed | out-string)")
Disconnect-VIServer * -Confirm:$false
Thanks, this is the script, and you helped me with the split on the date and converting it, so thanks for that . I tested this out, and the emails come out just fine, and then I decided to add the txt file log to save the dates long term for VMs that are deleted. This script does not include the new part where I was saving the text file, this is the script that I was initially going to run, and then I decided to add on to it.