Sunday, August 8, 2021

PowerCLI script to create snapshots for multiple VMs

PowerCLI script to create snapshots for multiple VMs


 #### HTML Output Formatting #######

$a = "<style>"

$a = $a + "BODY{background-color:Lavender ;}"

$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"

$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"

$a = $a + "</style>"

################################################################################################


################################################

# Snapshot Information

#################################################

Connect-VIServer vcenter_server

Remove-Item C:\Temp\Snapshot.html

$Date = Get-date -format F

$Name = Read-Host "Enter Your Name"

$Description = Read-Host "Enter the Requester Name & Reason"

$toAddr = Read-Host "Enter the To Email Address"

$vmname = gc C:\Temp\Servers.txt

get-vm $vmname | new-snapshot -name "$Name - $Date" -Description $Description -Quiesce -Memory | Select VM,Created,Name,Description | ConvertTo-html -Head $a -Body "<H2> </H2>" >> C:\Temp\Snapshot.html

$body = [System.IO.File]::ReadAllText('C:\Temp\Snapshot.html')

Send-MailMessage -To $toAddr -From From@Mailaddress.com -Subject "VMware Snapshots Created for the $Description" -Body $body -SmtpServer smtp.Mailaddress.com -BodyAsHtml

Disconnect-viserver vcenter_server -confirm:$false

#################### END of SCRIPT ####################################

No comments:

Post a Comment