Powershell v1searchstring从variables

我想search一个variables,其中包含从get-childitem获取的文件列表,并将所有匹配项列出到特定的searchstring,但似乎无法得到我想要的结果,我做错了什么?

$TargetFiles = Get-Childitem -OutBuffer 2500 |Where {!$_.PSIsContainer} |Select-Object Name,FullName ForEach ($File in (Select-String -Pattern "*$($CurrentDate.year)-$($CurrentDate.ToString('MM'))-*.gz" -InputObject $TargetFiles -SimpleMatch ) ) { write-host $file } 

我已经尝试了许多不同的方法来编写searchstring,包括添加-SimpleMatch并更改searchstring以匹配正则expression式search。

$($ CurrentDate.year) – $($ CurrentDate.ToString('MM')) – .gz”

==完整的脚本==

 $LazyDev = $True # ==================================================================== # # = Start Program = # # ==================================================================== # Write-Host "Firewall Server Log Search Program initiated..." Write-Host "Type exit at any prompt to terminate program. If a search is already in progress press CTRL-C." IF ($LazyDev -ne $True) { Write-Host "Please enter the Date you wish to search." [DateTime] $StartDate = Read-Host 'Enter start date (Format: mm/dd/yyyy)' [DateTime] $EndDate = Read-Host 'Enter end date (Format: mm/dd/yyyy)' $LogFile = Read-Host 'Enter Log File Name.' $SearchString = Read-Host 'Enter search string' } Else { [DateTime] $StartDate = "1/1/2014" [DateTime] $EndDate = "4/17/2015" $LogFile = "Log1" $SearchString = "123456789" } # ==================================================================== # # == Main Loop # ==================================================================== # # Create Required Variables and set their default values $CurrentDate = [DateTime]::Parse($StartDate) $MainLoop_End = 0 $Progress1_current = 0 $Progress1_end = (NEW-TIMESPAN –Start $StartDate –End $EndDate).TotalDays Write-Progress -ID 1 -Activity "Getting File List" -status "Processing: $CurrentDate_Start to $CurrentDate_End ($Progress1_Current`% Complete)" -percentComplete $Progress1_Current $TargetFiles = Get-Childitem -OutBuffer 1000 |Where {!$_.PSIsContainer} |Select-Object Name,FullName While ($CurrentDate -le $EndDate -OR $MainLoop_End -ne 1) { $Progress1_Current = [Math]::floor(([INT]$(NEW-TIMESPAN –Start $StartDate –End $EndDate).TotalDays - [INT]$(NEW-TIMESPAN –Start $CurrentDate –End $EndDate).TotalDays) / [INT]$(NEW-TIMESPAN –Start $StartDate –End $EndDate).TotalDays * 100) Write-Progress -ID 1 -Activity "Grepping Files/Exporting Logs" -status "Processing: $CurrentDate_Start to $CurrentDate_End ($Progress1_Current`% Complete)" -percentComplete $Progress1_Current IF ($Progress1_Current -eq "100") {$MainLoop_End = 1; Continue} # Will allow searching from first date of start date till end of month, but will then auto-correct the start date to the first day of the month. IF ($CurrentDate -eq $StartDate) { [DateTime] $CurrentDate_Start = "$($StartDate.month)/$($StartDate.day)/$($StartDate.year)" } Else { [DateTime] $CurrentDate_Start = "$($CurrentDate.month)/1/$($CurrentDate.year)" } # Will adjust search string to correct end day. If ($CurrentDate.year -eq $EndDate.Year -AND $CurrentDate.Month -eq $EndDate.Month) { [DateTime] $CurrentDate_End = "$($EndDate.month)/$($EndDate.day)/$($EndDate.year) 23:59:59" } Else { [DateTime] $CurrentDate_End = "$($CurrentDate.ToString('MM'))/$([DateTime]::DaysInMonth($CurrentDate.year,$CurrentDate.ToString('MM')))/$($CurrentDate.year) 23:59:59" } IF ($TargetFiles.count -ne $Null) { Write-Host "Processing: $CurrentDate_Start to $CurrentDate_End" $Pattern = "*$($CurrentDate.year)-$($CurrentDate.ToString('MM'))-*.gz" ForEach ($File in (Select-String -Pattern $Pattern -InputObject $TargetFiles -SimpleMatch ) ) { write-host $file Write-Progress -ID 2 -Activity "Processing File(s) ($i of $($TargetFiles.Count))" -status "Processing $($File.name)" -percentComplete (([array]::IndexOf($TargetFiles,$file) / $TargetFiles.Count) * 100) } IF ($i) {Remove-Variable -name i -force |Out-Null} } Else { Write-Host "Skipping: $CurrentDate_Start to $CurrentDate_End - No log files found." } IF ($CurrentDate.month -NE $EndDate.month -OR $CurrentDate.year -NE $EndDate.year) { $CurrentDate = $CurrentDate.AddMonths(1) } Else { $CurrentDate = $CurrentDate_End } #start-sleep 1 } Write-Host Script Complete start-sleep 1000 

你只是试图匹配当前月份和年份的文件名?
我没有v1在任何地方testing….但我想不出在v1为什么这不起作用的原因。
IIRC v1仍然有一个pipleline,在哪里对象,让你做简单的名称匹配。

 PS C:\temp\post> gci Directory: C:\temp\post Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 5/8/2015 10:47 AM 82 2015-05-blah1.gz -a--- 5/8/2015 10:48 AM 82 2015-05-blah1.txt -a--- 5/8/2015 10:47 AM 82 2015-05-blah2.gz -a--- 5/8/2015 10:47 AM 82 2015-06-blah1.gz -a--- 5/8/2015 10:47 AM 82 2015-07-blah1.gz -a--- 5/8/2015 10:47 AM 82 2015-08-blah1.gz PS C:\temp\post> $patrn = (get-date -f "yyyy-MM-") + ".*\.gz" PS C:\temp\post> gci | where { $_.name -match $patrn } | foreach { "do something to: " + $_.name } do something to: 2015-05-blah1.gz do something to: 2015-05-blah2.gz