VMware Cloud Community
mimo974
Contributor
Contributor

Script adjusment

Hello,

In other case, someone help me to create a alarm and find in other case this script for snapshot after 3 days. But im not an expert with PowerCLI and i have some questions about this script :

1/ When the script will work i just need to put on my computer and start it by powerCLI ?

2/ I wrote in red color line i dont understand

# PowerShell script to check for VM snapshots and send Email report

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer -Server <ip-or-host> -User <username> -Password <password> (i just need replace Ip username by mine but i need to write in clear my password ?)

# HTML formatting

$a = "<style>"

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

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

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

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

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

# Main section of check

Write-Host "Checking VMs for for snapshots"

$date = get-date

$datefile = get-date -uformat '%m-%d-%Y-%H%M%S'

$filename = "c:\dir\Snapshots_" + $datefile + ".htm" (I dont understand this line. Its my current directory on my own computer ?)

# Get list of VMs with snapshots

# Note:  It may take some time for the  Get-VM cmdlet to enumerate VMs in larger environments

$ss = Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)}

Write-Host "   Complete " -ForegroundColor Green

Write-Host "Generating VM snapshot report"

#$ss | Select-Object vm, name, SizeGB, Created, powerstate | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report</H2>"| Out-File $filename

$ss | Select-Object vm, name, SizeGB, Created, powerstate | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report </H2>"| Out-File $filename

Write-Host "   Complete " -ForegroundColor Green

Write-Host "Your snapshot report has been saved to:" $filename

$SMTPServer = <smtp_server>

$SMTPPort = 587

$Username = "email@email.com"

#Define the receiver of the report

$to = "recipient.email@email.com"

$subject = "VM Snapshot Report"

$body = "VM Snapshot Report"

$attachment = new-object Net.Mail.Attachment($filename)

$message = New-Object System.Net.Mail.MailMessage

$message.subject = $subject

$message.body = $body

$message.to.add($to)

$message.from = $username

$message.attachments.add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); (i need to change something here ?)

$smtp.EnableSSL = $true

#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); (i need to change something here ?)

$smtp.send($message)

write-host "Mail Sent"

Thank for you helps.

Reply
0 Kudos
1 Reply
Alex_Romeo
Leadership
Leadership

Hi,

Exactly, you need to enter the IP, username and password. The password here is visible, otherwise you need to add a piece of code for encryption

Connect-VIServer -Server <ip-or-host> -User <username> -Password <password> (i just need replace Ip username by mine but i need to write in clear my password ?)

is the directory on your computer where it is created in a .htm file for Attachment the snapshot creation, you can also change it if you want.

          $filename = "c:\dir\Snapshots_" + $datefile + ".htm" (I dont understand this line. Its my current directory on my own computer ?)

These parameters should not be modified, you need to modify the variables of this setting:

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); (i need to change something here ?)

$smtp.EnableSSL = $true

#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); (i need to change something here ?)

Modify the variables of this setting:

$SMTPServer = <smtp_server>

$SMTPPort = 587

$Username = "email@email.com"

info: for the password in this string I don't see any variable set for the $password

#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password

Alessandro Romeo

Blog: https://www.aleadmin.it/
Reply
0 Kudos