我有一段代码比较两个值,如果条件满足,它会发送一封电子邮件。但它不起作用,感谢帮助。
代码:
$filesize = Get-ChildItem $filename | Select-Object Length | Format-Wide
$filesize
$num=1265
$num
if("$filesize" -gt "$num")
{
$SMTPServer = "10.20.19.94"
$SMTPPort = 25
$username = "vcenter@somosadc.com"
#Define the receiver of the report
$to = "jeevan.m2@hcl.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);
$smtp.EnableSSL = $false
#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
write-host "Mail Sent"
}
输出:
1262
1265
Mail Sent
如果$ filesize = 1262小于$ num = 1265,为什么会发送电子邮件。它杀了我。
答案 0 :(得分:2)
因为您没有比较两个数字,所以您要比较两个字符串。
从第一个管道中删除Format-Wide
命令,并删除if
条件中参数周围的引号:
$filesize = Get-ChildItem $filename | Select-Object Length
$num = 1265
if($filesize.Length -gt $num) {
<# ... #>
}