我有以下代码,
Write-Output "$(Get-Content $alerfile | Select-String $(Get-Date (Get-Date).AddDays(-1) -Format $filtdate) -Context 0,1000000)"
我只需要返回行首的字符串,我尝试过:
Write-Output "$(Get-Content $alerfile | Select-String ^$(Get-Date (Get-Date).AddDays(-1) -Format $filtdate) -Context 0,1000000)"
但这不起作用。
As Lee_Dailey commented, you need to remove the Write-Output
.
Also, you're overdoing it with the date in the pattern.
Next, the Select-String
cmdlet also has a -Path
parameter, so you don't need Get-Content
for this.
I suggest something like this:
# insert the datetime format you need here; this is just an example $filtdate = 'yyyyMMdd HH:mm:ss' # get yesterdays date formatted using the $filtdate template above $refdate = (Get-Date).AddDays(-1).ToString($filtdate) # find the string beginning with that formatted date and return the results Select-String -Path $alerfile -Pattern "^$refdate" -Context 0,1000000