Hi LUCD ,
I have script for Snapshot & Datastore report.its genrating report but for sending email authetication required.Can you help me adding that lines to script.
Error:Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to
send anonymous mail during MAIL FROM"
At C:\Scripts\Snap&Data\snap&datastore.ps1:176 char:1
+ $smtp.Send($msg)
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
Here is the script:
# Execute Policy
#set-executionpolicy Unrestricted -Force
# Set Variables
$SCRIPT_PARENT = "C:\Scripts\Snap&Data"
$Uname = Get-Content Env:USERNAME
$Comp = Get-Content Env:COMPUTERNAME
# Remove old files
remove-item ($SCRIPT_PARENT + "\Report\V*.html") -force
# Connect VCs from VC_List.txt
$VCs= Get-Content ($SCRIPT_PARENT + "\vc_list.txt") -ErrorAction SilentlyContinue # mention vcenter name where you want to check resources.
$D = get-date -uformat "%m-%d-%Y-%H:%M" # To get a current date.
Write-Host "Connecting to VC" -foregroundcolor yellow
#*****************************************************************************************
foreach($vc in $VCs)
{
Connect-VIServer $VC -WarningAction 0
$outputfile = ($SCRIPT_PARENT + "\Report\$($VC).html") #".\Report\$($VC).html"
Write-Host ""
Write-Host "Collecting details from $VC" -foregroundcolor green
Function Get-SnapshotCreator {
Param (
[string]$VM,
[datetime]$Created
)
(Get-VIEvent -Entity $VM -Types Info -Start $Created.AddSeconds(-10) -Finish $Created.AddSeconds(10) | Where FullFormattedMessage -eq "Task: Create virtual machine snapshot" | Select -ExpandProperty UserName).Split("\")[-1]
}
$Report = Get-VM | Get-Snapshot | Select VM,Name,Description,@{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},@{Name="Creator";Expression={ Get-SnapshotCreator -VM $_.VM -Created $_.Created }},Created,@{Name="Days Old";Expression={ (New-TimeSpan -End (Get-Date) -Start $_.Created).Days }}
If (-not $Report)
{$Report = [PSCustomObject]@{
VM = "n/a"
Name = "n/a"
Description = "n/a"
'Size (GB)' = "n/a"
Creator = "n/a"
Created = "n/a"
'Days Old' = "n/a"
}
}
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#cccccc;color:#000000;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header><caption><font size=3 color=green><h1 align=""center"">Snapshots - $VC</h1></font>
<h4 align=""right""><font size=3 color=""#00008B"">Date: $D </font></h4></caption>
<TR>
<TH><B>VM</B></TH>
<TH><B>Snapshot Name</B></TH>
<TH><B>Description</B></TH>
<TH><B>Size (GB)</B></TH>
<TH><B>Creator</B></TH>
<TH><B>Created</B></TH>
<TH><B>Days Old</B></TH>
</TR>"
Foreach($Entry in $Report)
{if($Entry."Days Old" -gt "03" -and $Entry."Days Old" -ne "n/a") # Days old threshold - Warning / Yellow
{
if($Entry."Days Old" -gt "7") # Days old threshold - Critical / Red
{
$HTML += "<TR bgColor=#FF0000>" # Red
}
else
{
$HTML += "<TR bgColor=#FFE600>" # Yellow
}
}
else
{
$HTML += "<TR bgColor=#7FFF00>" # Neon Green
}
$HTML += "
<TD>$($Entry.VM)</TD>
<TD>$($Entry.Name)</TD>
<TD>$($Entry.Description)</TD>
<TD>$($Entry.SizeGB)</TD>
<TD>$($Entry.Creator)</TD>
<TD>$($Entry.Created)</TD>
<TD>$($Entry."Days Old")</TD>
</TR>"
}
$HTML += "</Table>"
$HTML += "<Table bgcolor='#cccccc' border=1 cellpadding=0 cellspacing=0 id=Header><caption><font size=3 color=green><h1 align=""center"">Datastores - $VC</h1></font>
<h4 align=""right""><font size=3 color=""#00008B"">Date: $D </font></h4></caption>
<TR>
<TH><B>DataStore Name</B></TH>
<TH><B>Free Space (GB)</B></TD>
<TH><B>Capacity (GB)</B></TH>
<TH><B>Provisioned Space (GB)</B></TH>
<TH><B>Free Space (%)</B></TH>
</TR>"
$Result = @()
$Result += Get-View -ViewType Datastore | Where-Object {$_.Name -notlike "datastorenameyouwanttoexcclude*" -and $_.Name -notlike "anotherdatastorenameyouwanttoexclude*"} | Select-Object -Property Name,
@{N="FreeSpaceGB";E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},
@{N="CapacityGB"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},
@{N="ProvisionedSpaceGB";E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},
@{N="FreeSpace";E={[math]::Round(((100* ($_.Summary.FreeSpace/1GB))/ ($_.Summary.Capacity/1GB)),0)}} | sort -Property "FreeSpace"
Foreach($Entry in $Result)
{
if($Entry.FreeSpace -lt "10") # Free space threshold - Warning / Yellow
{
if($Entry.FreeSpace -lt "05") # Free space threshold - Critical / Red
{
$HTML += "<TR bgColor=FF0000>" # Red
}
else
{
$HTML += "<TR bgColor=FFE600>" # Yellow
}
}
else
{
$HTML += "<TR bgColor=7FFF00>" # Neon Green
}
$HTML += "
<TD>$($Entry.Name)</TD>
<TD>$($Entry.FreeSpaceGB)</TD>
<TD>$($Entry.CapacityGB)</TD>
<TD>$($Entry.ProvisionedSpaceGB)</TD>
<TD>$($Entry.FreeSpace)</TD>
</TR>"
}
$HTML += "</TABLE></BODY></HTML>"
$HTML | Out-File $OutputFile
Disconnect-VIServer $VC -Confirm:$false
}
# Send email
# Add email IDs in email_id.txt file
$mailto = Get-Content ($SCRIPT_PARENT + "\email_id.txt") -ErrorAction SilentlyContinue
$SMTPserver = "mail-extern.patec.group"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($SMTPserver)
$msg.From = "donotreply.readvsphere@syntegon.com"
$msg.IsBodyHTML = $true
$msg.To.Add($mailto)
$msg.Subject = "Syntegon Datastore & Snapshot Report - $vcs"
foreach($vc in $vcs)
{
$MailTextT = Get-Content ($SCRIPT_PARENT + "\Report\*.html") -ErrorAction SilentlyContinue
$Sig = "<html><p class=MsoNormal><o:p> </o:p></p><B> Best Regards, <p> Windows Team </B></p></html>"
# $Top = "<html> This Script is executed on Server - <B>$Comp</B> by User - <b> $Uname </b></html>"
$MailText= $Top + $MailTextT + $Sig
}
$msg.Body = $MailText
$smtp.Send($msg)
#*****************************************************************************************