我正在尝试找到与此 Windows shell 命令等效的 powershell,但现在我正在寻找具有今天日期的文件:
findstr /S /C:"Firmware version text" C:\Temp\Filename1* & findstr /S /C:"Class::Method1" C:\Temp\Filename1* & echo "FindingFailures in today files"
我正在测试这个,它在这里打印日期和文件名,但它不打印文件中的文本。我知道我要找的是那个文件,因为我发现它运行的是没有日期部分/powershell 的 windows shell 命令。
这是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"}
}
}
运行它,我看到:
here
3/28/2023 12:00:00 am
3/28/2023 12:00:00 am
Filename1.txt
但它没有显示文件中我知道的行。有什么想法可以解决在文件中查找文本并打印找到的文本的问题吗?可能从文件中打印带有该文本的行?
更新
谢谢圣地亚哥,我正在尝试让日期部分正常工作,但它正在查找旧文件,我只想要今天的日期文件。
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---"
为此,打印输出为:
--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--
所以它正在寻找一个旧文件,但我只想要具有今天日期的文件。今天的时间并不重要。
更新1:
我已经尝试过圣地亚哥所做的一切,但它没有用
Get-Item "C:\Temp\Filename1*" |
Where-Object { $_.LastWriteTime.date -ge [datetime]::Today } |
Write-host $_.Name
错误:
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.
更新2
目录中有多个名称相似的文件,所以也许这就是为什么 Santiago 的有效而我的无效的原因。此外,圣地亚哥在他的解决方案中显示的时间比较对我不起作用。我不确定如何让它工作,而且我没有通过互联网搜索找到有用的结果。这是我目前的尝试:
$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
}
打印:
3/29/2023 上午 12:00:00 - -开始 - - 获取日期:3/29/2023 12:00:00 AM --查找下一个日期的文件-- 文件名11.txt 2023 年 3 月 29 日上午 8:58:09 文件名11.txt 文件名13.txt 2023 年 3 月 23 日上午 9:23:59 文件名13.txt
更新3 我正在尝试 Santiago 显示的内容,但显示错误:
[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
错误:
Select-String : Cannot bind argument to parameter 'Path' because it is null.
但是以下对我有用:
更新4:
$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"}
}
}
印花:
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\...
注意,目录中有多个具有正确名称签名的文件,但现在只有一个具有今天的日期。
你的代码对于你想要做的事情来说似乎过于复杂,可以简化为:
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
这是有效的:
更新4:
$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"}
}
}
印花:
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\...