如果我们找到的目录名称中带有“ 1970”,我需要修改我一直在努力发送的电子邮件警报的脚本。我正在尝试使用-Like运算符来完成此操作,但无法使其正常运行。我不知道问题出在我对操作员的使用,还是支票的放置,或者完全是其他问题。
一些注意事项:我已经编辑了电子邮件和SMTP服务器,但是我可以确认代码行本身是可以工作的。
这是我尝试过的:
$limit = (Get-Date).AddDays(0)
$logFile = "C:\STest\Log\log.txt"
#Throw a timestamp on the log file
Add-Content -Path $logFile "=======$(Get-Date)======="
#Get all of the paths for each camera
$paths = Get-ChildItem -Path "C:\Videos\" |Select-Object -ExpandProperty FullName
#for each path we found
foreach ($pa in $paths) {
# Delete directories older than the $limit.
$file = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit }
$file | Remove-Item -Recurse -Force
$file | Select -Expand FullName | Out-File $logFile -append
if ($file.FullName -like '1970'){
Send-MailMessage -to "Joe <[email protected]>" -from "Tod <[email protected]>" -subject "1970 Found" -body "Testing" -SmtpServer mySMTPserver
}
}
我也尝试过:
if($file -like "1970"){ }
if ($file.FullName -like "1970"){ }
if ($file.FullName -like 1970) { }
-like
是通配符运算符。要在字符串中的任何位置匹配“ 1970”,您需要*
在两端添加通配符:
PS C:\> 'something 1970 somethingelse' -like '*1970*'
True
如果要在字符串的开头匹配“ 1970”,请省略开头*
。
要在字符串末尾匹配“ 1970”,请省略结尾*
。
使用通配符?
来匹配任何单个字符,并使用[n-m]
“ globbing”通配符语法来匹配一系列字符。
看:
有关使用通配符匹配的更多信息。
从性能角度看,直接的比较(-eq
,-ne
,-lt
,-gt
)是最快的,其次是通配符匹配(-like
和-notlike
)。正则表达式匹配(-match
和-notmatch
)提供最大的灵活性,但也最慢。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句