Was trying to construct the script but i think i screwed up at some part in send-mailmessage cmdlet.
My intention is to call back the report i produced in the 2nd last line before send the results via email.
Keep getting this error and i feel like hitting the wall now.
Error i
Send-MailMessage : Cannot validate argument on parameter 'Subject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:71 char:51
+ Send-MailMessage -SmtpServer $smtpServer -Subject $smtpSubject -To $to -From $fr ...
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMess
My scripts are as follows,
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'
#Parameters
$clusterName = 'ukabc'
$vcenter = Get-Content -Path C:\Batch\Scripts\snapshots\vcenters.txt
$stmpSubject= 'VMware Snapshots'
$smtpServer = 'mail.local'
$to = 'af@abc.com'
$from = 'af@abc.com'
$att = 'C:\Batch\Scripts\'
#delete the old snapshot report
Get-ChildItem -Path $ExportPath -Filter snapshot*.csv | Remove-Item
connect-VIServer -Server $vcenter
Get-VM | Get-Snapshot |
select Name,
Created,
@{N='VM';E={$_.VM.Name}},
@{N='VMHost';E={$_.VM.VMHost.Name}},
@{N='Cluster';E={$_.VM.VMHost.Parent.Name}},
@{N='Datacenter';E={
$p = Get-View -Id $_.VM.VMHost.Parent.ExtensionData.Parent -Property Name,Parent
while($p -and $p -isnot [VMware.Vim.Datacenter]){
$p = Get-View -Id $p.Parent -Property Name,Parent
}
if($p){
$p.Name
}
}},
@{N='SizeMB';E={[math]::Round($_.SizeMB,1)}},
@{N='SizeGB';E={[math]::Round($_.SizeGB,1)}},
@{N='User';E={
Get-VIEvent -Start $_.Created.AddMinutes(-1) -Finish $_.Created.AddMinutes(1) |
where{$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'VirtualMachine.createSnapshot'} |
select -Last 1 -ExpandProperty UserName
}} |
#Create report
Export-Csv -Path $att_$(Get-Date -f dd_MM_yyyy).csv -NoTypeInformation -UseCulture
#Send results via email
Send-MailMessage -SmtpServer $smtpServer -Subject $smtpSubject -To $to -From $from -Attachments $att
There's a typo in the Parameter block, it says $stmpSubject, while the Send-MailMessage call uses $smtpSubject
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
There's a typo in the Parameter block, it says $stmpSubject, while the Send-MailMessage call uses $smtpSubject
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
sharp eye,mate! thanks.
one small thing i found that, the -Attachments in send-mailmessage is calling for a folder instead a file as in parameter block.
is there anyway i could call this line "Export-Csv -Path $file_$(Get-Date -f dd_MM_yyyy).csv -NoTypeInformation -UseCulture" so i could use it as -Attachments $att ?
try to use get-help of send-mailmessage but didnt see an option to filter .csv file passing call for path i defined.
You can specify a single file as an attachment.
See the Attachments parameter in the Send-MailMessage doc.
You could do something like this
$reportName = "$att_$(Get-Date -f dd_MM_yyyy).csv"
#Create report
Export-Csv -Path $reportName -NoTypeInformation -UseCulture
#Send results via email
Send-MailMessage -SmtpServer $smtpServer -Subject $smtpSubject -To $to -From $from -Attachments $reportName
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference