Powershell v1 String de pesquisa da variável

2

Estou tentando pesquisar uma variável que contém uma lista de arquivos, obtida de get-childitem, e listar todas as correspondências com uma string de pesquisa específica, mas não consigo obter o resultado que quero, o que estou fazendo errado?

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

Eu tentei várias maneiras diferentes de escrever a string de pesquisa, incluindo a adição do -SimpleMatch e a alteração da string de pesquisa para corresponder à pesquisa de regex.

" $ ($ CurrentDate.year) - $ ($ CurrentDate.ToString ('MM')) - .gz"

== Script completo ==

$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
    
por Nick W. 08.05.2015 / 01:22

1 resposta

0

Você está apenas tentando corresponder nomes de arquivos com o mês atual & ano?
Eu não tenho v1 em qualquer lugar para testar ... mas eu não consigo pensar em uma razão pela qual isso não funcionaria na v1.
IIRC v1 ainda tinha um pipleline, onde-objeto e deixá-lo fazer nome simples correspondências.

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
    
por 08.05.2015 / 17:56

Tags