gpt4 book ai didi

powershell - Powershell Get-ChildItem

转载 作者:行者123 更新时间:2023-12-02 23:57:30 24 4
gpt4 key购买 nike

我写了一个Powershell脚本来检查文件夹的内容,以及是否有LastWriteTime早于20分钟的文件通知我。我遇到的问题是,当我得到结果时,它包括电子邮件正文中的所有文件。我将如何编写该信息以仅获取电子邮件正文中的最新文件名?

$src = 'c:\test'
$sendmail = $false

Get-ChildItem -path $src | ForEach-Object {
#write-host $_.fullname
$dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date)

if ($dtdiff.TotalMinutes -gt 20){
$strbody=$strbody +$_.fullname+ " Last File Modified at " +$_.LastWriteTime +"`r`n"
$sendmail=$true
}
}


#$strbody
if($sendmail -eq $true){
# Email components
$strFromAddress = "abc@xyz.net"
$strToAddress = "abc@xyz.net"
$strMessageSubject = "REPORT"
$strMessageBody = $strbody
$strSendingServer = "smtp.com"
$SMTPPort = "587"
$emailSmtpUser = "abc@xyz.net"
$emailSmtpPass = "test123"
# Email objects
$objSMTPMessage = New-Object System.Net.Mail.MailMessage $strFromAddress, $strToAddress, $strMessageSubject, $strMessageBody
$objSMTPClient = New-Object System.Net.Mail.SMTPClient( $strSendingServer, $SMTPPort )
$objSMTPClient.EnableSsl = $true
$objSMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$objSMTPClient.Send($objSMTPMessage)
}

最佳答案

仅获取最实际的文件:编辑以删除缺陷

Get-ChildItem -path $src | 
Sort LastWriteTime |
Select -last 1 |
ForEach-Object {
#write-host $_.fullname
$dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date)
if ($dtdiff.TotalMinutes -gt 20){
$strbody=$strbody +$_.fullname+ " Last File Modified at " +$_.LastWriteTime +"`r`n"
$sendmail=$true
}
}

关于powershell - Powershell Get-ChildItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41346375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com