# Migrated VM Details for deletion
Set-ExecutionPolicy Remotesigned -force
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core}
$input = Get-Content -Path D:\c5142267\Scripts\Migrated-VM-Deletion\VC.txt
$emailidto = Get-Content -Path D:\c5142267\Scripts\Migrated-VM-Deletion\Emailidto.txt
$emailidcc = Get-Content -Path D:\c5142267\Scripts\Migrated-VM-Deletion\Emailidcc.txt
$file="D:\c5142267\Scripts\Migrated-VM-Deletion\Report-Migrated-VM-Deletion.html"
if ( [System.IO.File]::Exists($file) )
{
remove-item -force $file
}
# Created HTML file and Email Text which come first of the file. e.g. Hello Colleagues
ConvertTo-HTML -body "Hello Colleagues,
Please delete old migrated VMs after 3 days which was renamed by you.
"|out-file D:\c5142267\Scripts\Migrated-VM-Deletion\Report-Migrated-VM-Deletion.html -append
foreach($vc in $input) {
Connect-VIServer $vc -WarningAction SilentlyContinue
# Created HTML Table and formating
$a = ""
# Updated VC name in HTML File with Table and formating
ConvertTo-HTML -body "$($vc)"|out-file D:\c5142267\Scripts\Migrated-VM-Deletion\Report-Migrated-VM-Deletion.html -append
# Executable Commands for report
$vm = get-vm -Name *migrat*,*old
Get-VIEvent -Entity $vm -maxsamples 100 | where {$_.Gettype().Name -eq "VmRenamedEvent"} | Sort CreatedTime -Descending |Select @{N="VM Name";E="NewName"},@{N="VM Renamed Time";E="CreatedTime"},@{N="VM Renamed By";E="UserName"} | ConvertTo-HTML -head $a|out-file D:\c5142267\Scripts\Migrated-VM-Deletion\Report-Migrated-VM-Deletion.html -Append
#Get-Cluster | select Name, @{N="VM Count";E={($_ | Get-VM).count}} | ConvertTo-HTML -head $a|out-file D:\c5142267\Scripts\Migrated-VM-Deletion\Report-Migrated-VM-Deletion.html -Append
Disconnect-VIServer -server $vc -Confirm:$false
}
# Signature at the End Of Report
ConvertTo-HTML -body "
Regards,
Abhishek Borole
SAP Global IT - Infrastructure Services
Windows-VMware Team SCA "|out-file D:\c5142267\Scripts\Migrated-VM-Deletion\Report-Migrated-VM-Deletion.html -append
# Send Email to concern team
$smtpServer = "mail.sap.corp"
$MailFrom = "DL_SAP_IT_IS_HIM_SM_IND_2ND_LEVEL_WIN_VM@exchange.sap.corp"
$mailto = $emailidto
$mailcc = $emailidcc
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $MailFrom
$msg.IsBodyHTML = $true
$msg.To.Add($Mailto)
$msg.Cc.Add($Mailcc)
$msg.Subject = "Report : Migrated Old VM Details"
$MailTextT = Get-Content -Path D:\c5142267\Scripts\Migrated-VM-Deletion\Report-Migrated-VM-Deletion.html
$msg.Body = $MailTextT
$smtp.Send($msg)