VMware Cloud Community
afhamsani
Enthusiast
Enthusiast
Jump to solution

powercli: cannot validate argument

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos