Powershell v1 从变量中搜索字符串

Powershell v1 从变量中搜索字符串

我正在尝试搜索一个包含从 get-childitem 获取的文件列表的变量,并列出与特定搜索字符串的所有匹配项,但似乎无法获得我想要的结果,我做错了什么?

$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
        }

我尝试了多种不同的方法来编写搜索字符串,包括添加 -SimpleMatch 和更改搜索字符串以匹配正则表达式搜索。

$($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

答案1

您只是想将文件名与当前月份和年份进行匹配?
我没有 v1 可以测试……但我想不出为什么这在 v1 中不起作用。如果我没记错的话,
v1 仍然有一个管道,where-object,让您可以进行简单的名称匹配。

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

相关内容