0

I'm trying to find the powershell equivalent of this Windows shell command, except now I'm looking for files with today's date:

findstr /S /C:"Firmware version text" C:\Temp\Filename1* & findstr /S /C:"Class::Method1" C:\Temp\Filename1* & echo "FindingFailures in today files"

I'm testing this, and it's printing here and the dates, and filename, but it's not printing the text in the file. I know that what I'm looking for is in that file, since I found it running the windows shell command without the date part/powershell.

This is the powershell:

 Set-Location -Path "C:\Temp\"
 Get-Item Filename1* | 
 Foreach { 
     Write-Host "here"
     $lastupdatetime=$_.LastWriteTime
     $nowtime = get-date
     Write-Host $nowtime.Date
     Write-Host (Get-Date).Date
     if ($nowtime.Date -eq (Get-Date).Date) 
     {
           Write-Host $_.Name
           if(Select-String -Path $_.Name -Pattern "'Firmware version text'" ) {write-host "Found Firmware version"}
           if(Select-String -Path $_.Name -Pattern "'Class::Method1'" )  {write-host "Found Class::Method1"}
     }
 }

Running it, I see:

here
3/28/2023 12:00:00 am
3/28/2023 12:00:00 am
Filename1.txt

But it's not showing the lines in the file that I know are there. Any ideas how to get it to fix the issue with finding the text in the file(s) and print that it's found? Possibly print the line with that text from the file?

Update

Thank you Santiago, I'm trying to get the date portion to work, but it's finding older files and I just want today's dated files.

write-host "---start---"
 write-host "get date:"(get-date).date
 write-host "--find files in date next--"
 Get-Item "C:\Temp\Filename1*" | Foreach {$_.Name;$_.LastWriteTime
 Where-Object  { $_.LastWriteTime.date -ge (get-date).Date } |
           Write-host $_.Name
           write-host $_.LastWriteTime
           Write-host "-----mid---"
           #Select-String  -path $_.Name -Pattern "'Firmware version text'"
           #Select-Object Filename, LineNumber, Line, Path  
           } 
  
  Write-host "-----end---"

For this the printouts are:

--start--
get date: 3/282023 12:00:00 AM
--find files in date next--
Filename1.txt

Tuesday, March 28, 2023 3:57:15 PM
3/28/2023 3:57:15 PM
----mid--
Filename11.txt
Thursday, March 23, 2023 9:23:59 AM
3/23/2023 9:23:59 AM
---mid--
---end--

So it's finding an older file, but I just want files with today's date. Time today isn't important.

Update1:

I had tried exactly what Santiago had, but it wasn't working

Get-Item "C:\Temp\Filename1*" | 
Where-Object { $_.LastWriteTime.date -ge [datetime]::Today } | 
  Write-host $_.Name

error:

Write-host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters 
that take pipeline input.

Update2

There are multiple files in the dir by that similar name, so maybe that's why Santiago's works but mine doesn't. Also, the time comparison isn't working for me that Santiago shows in his solution. I'm not sure how to get that to work and I'm not finding helpful results with an internet search. This is my current try:

$nowtime = (get-date).Date
 write-host $nowtime
 write-host "---start---"
 write-host "get date:"(get-date).date
 write-host "--find files in date next--"
 Get-Item "C:\Temp\Filename1*" | 
 Foreach { $_.Name; write-host $_.LastWriteTime
 Where-Object  { $nowtime -eq $_.LastWriteTime.date  } |
           write-host "here"
           Write-host $_.Name
 }

which prints:

3/29/2023 12:00:00 AM ---start--- get date: 3/29/2023 12:00:00 AM --find files in date next-- filename11.txt 3/29/2023 8:58:09 AM filename11.txt filename13.txt 3/23/2023 9:23:59 AM filename13.txt

Update3 I'm trying exactly what Santiago is showing, and it's showing the error:

[datetime]::Today
 write-host "---start---"
 write-host "get date:"(get-date).date
 write-host "--find files in date next--"
Get-Item "C:\Temp\Filename1*" | 
 Where-Object  { $_.LastWriteTime.date -ge [datetime]::Today } |
          Select-String  -path $_.Name -Pattern "'Firmware version text'" |
           Select-Object Filename, LineNumber, Line, Path  

Error:

Select-String : Cannot bind argument to parameter 'Path' because it is null.

However the following is working for me:

Update4:

$nowtime = (get-date).Date
Get-Item "C:\Temp\Filename1*" | 
 Foreach { $lastupdatetime=$_.LastWriteTime  $_.LastWriteTime
 if  ( $nowtime -eq $lastupdatetime.date  ) {
           Select-String  -path $_.FullName -Pattern "Firmware version "  | 
              Select-Object Filename, LineNumber, Line, Path;
              if(Select-String -path $_.FullName -Pattern "Firmware version text2"  )
                {Select-String -path $_.FullName -Pattern "Class::Method1"}
}
}

Prints:

Filename                                                LineNumber Line                                                                                                                        Path        
--------                                                ---------- ----                                                                                                                        ----        
filename1.txt         10 01-17-2023 12:22:27 PM     Firmware version : unknown                                                                          C:\Temp\...
filename1.txt         46 01-17-2023 12:22:27 PM     Firmware version : FW="K.02"                                                         C:\Temp\...
filename1.txt.txt          8 01-17-2023 12:22:17 PM     Error in text1():  Class::Method1() failed.                                     C:\Temp\...
filename1.txt.txt         11 Class::Method1() failed.                                                                                     C:\Temp\...
filename1.txt.txt         97 01-17-2023 12:22:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        176 01-17-2023 12:22:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        177 01-17-2023 12:23:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        180 01-17-2023 12:23:25 PM     Error in text2::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        181 01-17-2023 12:23:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        182 01-17-2023 12:23:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        183 01-17-2023 12:24:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        184 01-17-2023 12:24:25 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        185 01-17-2023 12:24:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        186 01-17-2023 12:24:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        203 01-17-2023 12:25:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        206 01-17-2023 12:25:21 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...

Note, there's multiple files in the dir with the correct name signature, but right now only one has today's date.

Michele
  • 3,617
  • 12
  • 47
  • 81
  • you're using `Select-String` as your condition to output those `write-host` so this is expected. You should capture the output from the cmdlet and output the `.Line` property values... also `$lastupdatetime` not sure what its for, its never being used – Santiago Squarzon Mar 28 '23 at 18:03
  • @SantiagoSquarzon - do you have an example? It's not printing my write-host for "Firmware Version text", which I expected. Feel free to post as an answer. – Michele Mar 28 '23 at 18:17

2 Answers2

0

Your code seems overly complicated for what it seems you're looking to do, could be reduced to this:

Get-Item "C:\Temp\Filename1*" |
    Where-Object { $_.LastWriteTime -ge [datetime]::Today } |
    Select-String -Pattern "'Firmware version text'|'Class::Method1'" |
    Select-Object Filename, LineNumber, Line, Path
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • I think I mis-communicated and I meant that the file date found would need to be the same as today's date. I'm not finding that this would work in the update – Michele Mar 28 '23 at 20:09
  • @Michele isnt that what `Where-Object { $_.LastWriteTime -ge [datetime]::Today }` is supposed to do? find files which's modified date is greater than or equal to today... – Santiago Squarzon Mar 28 '23 at 20:12
  • when I write-host [datetime]::Today, it's empty. I'm not sure why that isn't working. When I ran the code with that, it's printing the files dated March 23 as well. See update what I have right now in the question. – Michele Mar 28 '23 at 20:17
  • I don't know, the post you have posted is not the code I have written for the answer so – Santiago Squarzon Mar 28 '23 at 20:18
  • correction, write-host [datetime]::Today prints [datetime]::Today – Michele Mar 28 '23 at 20:18
  • like I said, when I tried what you had, it wasn't working for the date filter for me. – Michele Mar 28 '23 at 20:23
  • Get-Item "C:\Temp\Filename1*" | Where-Object { $_.LastWriteTime.date -ge [datetime]::Today } | Write-host $_.Name gives the error Write-host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. – Michele Mar 28 '23 at 20:27
  • @Michele If you insist on using `Write-Host`, then at least use brackets: `write-host ([datetime]::Today)`. Beter still just output `[datetime]::Today` – Theo Mar 29 '23 at 12:35
  • @Theo - It's printing the date when I [datetime]::Today, but I get an error when I do what Santiago shows. No offense intended to anyone of course. I do have something that looks like it's working now, so I will post that soon. – Michele Mar 29 '23 at 17:05
0

This is working:

Update4:

$nowtime = (get-date).Date
Get-Item "C:\Temp\Filename1*" | 
 Foreach { $lastupdatetime=$_.LastWriteTime  $_.LastWriteTime
 if  ( $nowtime -eq $lastupdatetime.date  ) {
           Select-String  -path $_.FullName -Pattern "Firmware version "  | 
              Select-Object Filename, LineNumber, Line, Path;
              if(Select-String -path $_.FullName -Pattern "Firmware version text2"  )
                {Select-String -path $_.FullName -Pattern "Class::Method1"}
}
}

Prints:

Filename                                                LineNumber Line                                                                                                                        Path        
--------                                                ---------- ----                                                                                                                        ----        
filename1.txt         10 01-17-2023 12:22:27 PM     Firmware version : unknown                                                                          C:\Temp\...
filename1.txt         46 01-17-2023 12:22:27 PM     Firmware version : FW="K.02"                                                         C:\Temp\...
filename1.txt.txt          8 01-17-2023 12:22:17 PM     Error in text1():  Class::Method1() failed.                                     C:\Temp\...
filename1.txt.txt         11 Class::Method1() failed.                                                                                     C:\Temp\...
filename1.txt.txt         97 01-17-2023 12:22:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        176 01-17-2023 12:22:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        177 01-17-2023 12:23:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        180 01-17-2023 12:23:25 PM     Error in text2::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        181 01-17-2023 12:23:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        182 01-17-2023 12:23:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        183 01-17-2023 12:24:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        184 01-17-2023 12:24:25 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        185 01-17-2023 12:24:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        186 01-17-2023 12:24:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        203 01-17-2023 12:25:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        206 01-17-2023 12:25:21 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
Michele
  • 3,617
  • 12
  • 47
  • 81