PowerShell condition - result length > 0 or something like that - powershell

I have a simple one line command that list contents of some remote share
Get-ChildItem -Path "\\some_path\" -Recurse | Where-Object{ $_.LastWriteTime -lt (Get-Date).AddDays(-1) }
I need to send an email in case there are no files in this folder.
So I found code that should send an email:
if ($Condition) {
Send-MailMessage -From "sender#example.com" -To "receiver#example.com" -Subject "Condition met" -Body "The condition you specified has been met." -SmtpServer "smtp.example.com"
}
However, I'm not sure how should I define this $Condition in case there is nothing in this folder.
Maybe something like this:
$Condition = Get-ChildItem -Path "\\some_path\" -Recurse | Where-Object{ $_.LastWriteTime -lt (Get-Date).AddDays(-1) } | length>0
Thanks.

Related

If path does not exist, skip FOREACH

I have a question that I hope one of you can help me with.
The following script does its job, it sends the email of the folders that are older than 30 days.
I want to add 2 more features...
If the OwnerEmail.txt is not there or empty.. it will not send the email.
If there are not folders older than 30 days, it will not send the email.
I am just having problems using the CONTINUE feature to skip the ForEach
$ParentFolders = Get-ChildItem -Path "C:\Test\WRA" -Directory |
Select-Object -ExpandProperty FullName
foreach ($SubFolder in $ParentFolders) {
$Contact = Get-Content -Path ($SubFolder + "\OwnerEmail.txt")
$emailTime = (Get-Date).ToString("MM/dd/yyyy")
$WRAHTMLReport = Get-ChildItem -Path $SubFolder -Directory |
Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} |
Select-Object Name, CreationTime |
ConvertTo-Html -Head $Header -PreContent "
<h2>Current Nuix Case(s)</h2>
<body>
Report was sent on $emailTime
<br></br>
When you have a moment can you please confirm if any of these cases can be removed from Nuix:
<body>
<br></br> " |
$WRAHTMLReport = $WRAHTMLReport -join ""
Start-Sleep -s 2
Send-MailMessage -SmtpServer mail.company.com -to $Contact -from cubam1#company.com -Subject "Nuix Cases" -Body $WRAHTMLReport -BodyAsHtml
}
So, it could look something like this, note that I'm only showing the relevant parts of the code.
I think in this case, is safe to put the $emailTime outside the loop since you're only gathering the date in MM/dd/yyyy format and, I also added the $folderLimitDate outside the loop.
This variable contains the HTML body
$mailBody = ConvertTo-Html -InputObject $WRAHTMLReport... so remember to change the -Body $mailBody on Send-MailMessage
$emailTime = (Get-Date).ToString("MM/dd/yyyy")
$folderLimitDate = (Get-Date).AddDays(-30)
foreach ($SubFolder in $ParentFolders)
{
$path = Join-Path $SubFolder -ChildPath 'OwnerEmail.txt'
# If OwnerEmail.txt doesn't exist on this path,
# go to next loop iteration
if(-not(Test-Path $path)){ continue }
$Contact = Get-Content $path
$WRAHTMLReport = Get-ChildItem -Path $SubFolder -Directory |
Where-Object LastWriteTime -LT $folderLimitDate |
Select-Object Name, CreationTime
# If this variable is null, go to next loop iteration
if(-not $WRAHTMLReport){ continue }
[string]$mailBody = ConvertTo-Html -InputObject $WRAHTMLReport...
...
...
...

powershell list last log and insert body email

Hello i need to send the last .log and insert in $body with get-content
$Logpath = "D:\Script\"
$LastLog = Get-ChildItem $Logpath | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} | select-object Name -last 1
$fullpath = Write-Output $Logpath $LastLog
$body = Get-Content $fullpath | out-string
PS D:\Script\> Get-ChildItem $Logpath | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} | select-object Name -last 1
Name
----
test05.txt
$fullpath doesn't work and $body i don't know how put up..
thx 4 help im noob
I think this may help you:
$Logpath = "D:\Script\"
$yesterday = (Get-Date).AddDays(-1)
# this will get you a FileInfo object of the latest log file
$LastLog = Get-ChildItem $Logpath -File | Where-Object {$_.LastWriteTime -gt $yesterday} | Select-Object -Last 1
# the FileInfo object has a 'FullName' property
Write-Host "Reading file '$($LastLog.FullName)'"
# to get the content as a single string, use the '-Raw' switch
$body = Get-Content $LastLog.FullName -Raw
# to send this as email, have a look at the 'Send-MailMessage' cmdlet at
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-6
#basic example on 'Send-MailMessage'
Send-MailMessage -To "someone#example.com" -From "me#example.com" -Subject "Log $($LastLog.Name)" -Body $body

Powershell How to get lastwritetime file and log it

I have tried with powershell to check if files in a directory with subfolders do not have a changeover time of 10 hours (LastWriteTime).
If this is the case I would like to log its name and directory in a log.txt file and have an email alert.
So I did that, but it does not really work well ... below any help is appreciated!
$directories ='C:\folders'
$Logfile = "C:\log.txt"
function logit($msg){
$msg='[{0}]{1}' -f [datetime]::Now, $msg
$msg|Out-File $logfile -append
}
if([datetime]::Now - (Get-Item $directories).LastWriteTime -gt
[timespan]'0:10:0:0'){
Send-MailMessage -to "name <name#domain.com>" -from "name
<name#domain.com>" -subject "object" -smtpserver "smtp.domain.com" ..
Out-File $Logfile
Write-Host
}
Thank you
I try this, i get the good result. i'ts possible to modify "comparedate" in hours ?
$compareDate = (Get-Date).AddDays(-1)
#(Get-ChildItem -Path C:\temp\*.* -Filter '*.*' -Recurse | Where-Object {
$_.LastWriteTime -gt $compareDate})
try this:
$compareDate = (Get-Date).AddHours(-10)
Get-ChildItem "C:\temp\" -Recurse -file | Where LastWriteTime -gt $compareDate

Select-String is taking too long. How can I optimize it

I am really trying to speed this script up. I have a directory with about 17k files in it:
$date= Get-Date -Format yyyyMMdd
$dir= "C:\test\$date"
$path=Get-ChildItem -Path $dir -Recurse
$pattern = "<RESULT>FAILED</RESULT>"
$submitted = (select-string -path $path -pattern $pattern | measure-object).Count
select-string -path $path -pattern $pattern | select Path,Filename,Line | Export-Csv -Path "D:\Failed.csv"
if($submitted -eq 0) {
Remove-Item "D:\Failed.csv" -recurse
}
else
{
Send-MailMessage -From "noreply#email.com" -To users#email.com -Subject "Failed Report" -Body "Attached are the failed files. This is for the last 3 hours. There may be files that were already reported." -Attachments "D:\Failed.csv" -SmtpServer 0.0.0.0
Remove-Item "D:\Failed.csv" -recurse
}
If Select-String is taking a long time in the script above, try only doing it once. Also, I see that you check the count and if nothing is going on, you delete that CSV you made. So howabout you count it first, and then only make it if you need to.
...
$submitted = select-string -path $path -pattern $pattern
...
if(($submitted | Measure-Object).Count -gt 0){
...make the csv using the $submitted variable as the source...
...send the csv...
...delete the csv...
}

Images not moving from one UNC to another UNC

I would add this to my other post, but I can't find it. I have been trying to copy images (JPG and JPEG) from one UNC (source) to two UNCs (destinations) that are three days old. I only need to copy the images, not the folders they are contained in.
I do not get any errors, the email received from the script tells me no images were copied. The destinations do not have any images copied to them and there are images in the source that are newer than three days from todays date. I am completely lost. I have been on Google all day trying to figure this out to no avail so far. Any suggestions would be appreciated.
$ImageLocation = "\\vdifiles\Print-Room\FileSrv\Barcode-Order"
$TestSite = "\\10.0.100.3\www2.varietydistributors.com\catalog\newpictures"
$LiveSite = "\\10.0.100.3\Variety Distributors.com\catalog\newpictures"
$WhenFileWritten = (get-date).AddDays(-3)
$IncludeFileTypes = "*.jpeg,*.jpg"
$LogFile = "C:\Temp\FilesCopied.log"
$EmailServer = "emailserver.me.com"
$EmailFrom = "fromme#me.com"
$AuthUser = "user"
$AuthPass = "pass"
$XHeaderInfo = "X-Header: This email was sent from Laservault."
$EmailTo = "tome#me.com"
$EmailSubject = "Barcode Images Summary."
$EmailBodySent = "Attached to this email is a log file of what images were copied from $ImageLocation to $TestSite and $LiveSite as well as sent to AS2 to send over to Order Stream."
$EmailBodyNotSent = "There were no files in $ImageLocation that matched $WhenFileWritten days old. So, nothing was sent to $TestSite, $LiveSite or AS2."
$TheImages = get-childitem "Microsoft.PowerShell.Core\FileSystem::$ImageLocation" -recurse | where-object {$_.Extension -eq $IncludeFileTypes -and $_.LastWriteTime -gt $WhenFileWritten -and $_.PsIsContainer -eq $false}
foreach ($Images in $TheImages) {
$FileCount = $Images.count
if ($FileCount -gt 0) {
copy-item -path $Images.FullName -destination "Microsoft.PowerShell.Core\FileSystem::$TestSite" -force
copy-item -path $Images.FullName -destination "Microsoft.PowerShell.Core\FileSystem::$LiveSite" -force
write-host "$FileCount files were copied."
write-output $Images >> $LogFile
\\vdifiles\blat$\blat.exe -attach $LogFile -to $EmailTo -s $EmailSubject -i $EmailFrom -body $EmailBodySent -server $EmailServer -u $AuthUser -pw $AuthPass -f $EmailFrom -x $XHeaderInfo
remove-item $LogFile
} else {
\\vdifiles\blat$\blat.exe -to $EmailTo -s $EmailSubject -i $EmailFrom -body $EmailBodyNotSent -server $EmailServer -u $AuthUser -pw $AuthPass -f $EmailFrom -x $XHeaderInfo
}
}
Your original post: Copy-Item : Cannot bind argument to parameter 'Path' because it is null
I copied my answer from there here:
The issue is with the Get-ChildItem not returning anything. It's a small problem because of the -include and -exclude parameters you are feeding into it. You are defining the variable like this:
$IncludeFileTypes = "*.jpeg,*.jpg"
$ExcludeFileTypes = "*.psd,*.tif,*.pdf"
The problem is that the -include and -exclude parameters are expecting a string array (String[]) what you are giving them is a straight string.
We can prove this, if you create a file with the name "a.jpeg,a.jpg", the get-childitem cmdlt will return the file.
The solution is to change your $IncludeFileTypes and $ExcludeFileTypes variables to a string array, and it should return the results that you are expecting:
$IncludeFileTypes = "*.jpeg","*.jpg"
$ExcludeFileTypes = "*.psd","*.tif","*.pdf"
The where-object is not finding any match for any files.
Amend $IncludeFileTypes = "*.jpeg,*.jpg" to $IncludeFileTypes = #("*.jpeg","*.jpg")
Then pass that array of file types into the get-childitem command using the -include Parameter (-filter only takes one filetype, as a string) and remove the $_.Extension check from the where-object command.
e.g.
$TheImages = get-childitem "Microsoft.PowerShell.Core\FileSystem::$ImageLocation" -recurse -include $IncludeFileTypes | where-object {$_.LastWriteTime -gt $WhenFileWritten -and $_.PsIsContainer -eq $false}
You are also checking the count of the wrong variable to get your file count, and it shouldn't be inside the foreach loop.
Get the file count, then if it's greater than zero, iterate through the list if files, copying them.
Try:
$FileCount = $TheImages.count
if ($FileCount -gt 0) {
foreach ($Images in $TheImages) {
And also
write-output $TheImages >> $LogFile
Instead of
write-output $Images >> $LogFile
You are declaring the list wrong, it should be like this:
$IncludeFileTypes = #("*.jpeg","*.jpg")
Try searching with the -Include like this:
$TheImages = get-childitem "Microsoft.PowerShell.Core\FileSystem::$ImageLocation" -recurse -Include $IncludeFileTypes | where-object {$_.LastWriteTime -gt $WhenFileWritten -and $_.PsIsContainer -eq $false}
And check if the variable is empty after that:
if($TheImages -ne $null)
{
#move...
#send email...
#etc
}
Hope this solves your issue.