Assistant creating array?

%3CLINGO-SUB%20id%3D%22lingo-sub-894496%22%20slang%3D%22en-US%22%3EAssistant%20creating%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-894496%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20a%20Powershell%20Amateur!%20I've%20written%20some%20code%20to%20import%20data%20from%20logfile.txt%20and%20export%20the%20results%20with%20export-csv%20to%20a%20weapons_date.%20I'm%20replicating%20each%20line%20but%20I'm%20sure%20I%20might%20be%20able%20to%20do%20this%20better%2C%20perhaps%20with%20an%20array%20or%20loop%20or%20something%3F%3C%2FP%3E%3CP%3EI%20did%20some%20basic%20research%20but%20don't%20fully%20understand%20arrays%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EClear-Host%20%0A%24TestPath%20%3D%20%24null%20%0A%24sourcefile%20%3D%20%22logfile.txt%22%20%0A%24dest%20%3D%20%22weapons_%22%20%0A%24date%20%3D%20(Get-Date%20-Format%20%22%25d%25M%25y-%25H%25m%25s%22)%20%0A%24target%20%3D%20%24dest%2B%24date%2B%22.csv%22%0A%0Aif%20(Test-Path%20%24destinationfile)%20%7BRemove-Item%20%24destinationfile%7D%0A%0AGet-Content%20%24sourcefile%20%7C%20Select-String%20-Pattern%20%22weapon_sks%22%20%7C%20Measure-Object%20-Line%20%7C%20Add-Member%20-MemberType%20NoteProperty%20-Name%20Weapon%20-Value%20SKS%20-PassThru%20%7C%20Export-Csv%20-Path%20%24target%20-NoTypeInformation%0A%0AGet-Content%20%24sourcefile%20%7C%20Select-String%20-Pattern%20%22weapon_rpk%22%20%7C%20Measure-Object%20-Line%20%7C%20Add-Member%20-MemberType%20NoteProperty%20-Name%20Weapon%20-Value%20RPK%20-PassThru%20%7C%20Export-Csv%20-Path%20%0A%24target%20-NoTypeInformation%20-Append%20%0A%0A%5BSystem.Windows.MessageBox%5D%3A%3AShow('Parsing%20Log%20File%20complete'%2C'Logfile%20Outcome'%2C'OK')%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHoping%20to%20build%20array%20to%20make%20file%20much%20shorter%20so%20I%20can%20perhaps%20just%20read%20from%20a%20string%20or%20csv%20with%20list%20of%20names%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-894496%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%20ISE%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epowershell%20script%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-912263%22%20slang%3D%22en-US%22%3ERe%3A%20Assistant%20creating%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-912263%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F420463%22%20target%3D%22_blank%22%3E%40lionheart1981%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3ELooking%20at%20this%20it%20could%20be%20much%20simpler%20but%20without%20seeing%20the%20content%20of%20the%20logs%20it's%20difficult%20to%20say%20for%20sure.%3CBR%20%2F%3E%3CBR%20%2F%3EIt%20looks%20like%20you%20just%20want%20the%20count%20of%20each%20of%20the%20matched%20patterns%20and%20add%20the%20relevant%20property%20as%20per%20the%20match.%20You%20can%20grossly%20simplify%20this%20using%20Group-Object%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EGet-Content%20.%5Ctestfile.txt%20%7C%20Where-Object%20%7B%20%24_%20-match%20'weapon_(sks%7Crpk)'%20%7D%20%7C%0A%20%20%20%20Group-Object%20%7C%0A%20%20%20%20%20%20%20%20Select-Object%20Count%2C%20Name%2C%20%40%7Bname%3D'Weapon'%3Bexpression%3D%7B%24_.name%20-replace%20'.%2B_'%7D%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EThis%20will%20give%20the%20following%20output%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3ECount%20Name%20%20%20%20%20%20%20%20%20Weapon%0A-----%20----%20%20%20%20%20%20%20%20%20%20%20------%0A%20%20%2018%20weapon_rpk%20%20%20rpk%0A%20%20%2011%20weapon_sks%20%20%20sks%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Visitor

Hi,

 

I'm a Powershell Amateur! I've written some code to import data from logfile.txt and export the results with export-csv to a weapons_date. I'm replicating each line but I'm sure I might be able to do this better, perhaps with an array or loop or something?

I did some basic research but don't fully understand arrays

 

Clear-Host 
$TestPath = $null 
$sourcefile = "logfile.txt" 
$dest = "weapons_" 
$date = (Get-Date -Format "%d%M%y-%H%m%s") 
$target = $dest+$date+".csv"

if (Test-Path $destinationfile) {Remove-Item $destinationfile}

Get-Content $sourcefile | Select-String -Pattern "weapon_sks" | Measure-Object -Line | Add-Member -MemberType NoteProperty -Name Weapon -Value SKS -PassThru | Export-Csv -Path $target -NoTypeInformation

Get-Content $sourcefile | Select-String -Pattern "weapon_rpk" | Measure-Object -Line | Add-Member -MemberType NoteProperty -Name Weapon -Value RPK -PassThru | Export-Csv -Path 
$target -NoTypeInformation -Append 

[System.Windows.MessageBox]::Show('Parsing Log File complete','Logfile Outcome','OK')

 

Hoping to build array to make file much shorter so I can perhaps just read from a string or csv with list of names?

 

Cheers!

1 Reply
Highlighted

@lionheart1981 

Looking at this it could be much simpler but without seeing the content of the logs it's difficult to say for sure.

It looks like you just want the count of each of the matched patterns and add the relevant property as per the match. You can grossly simplify this using Group-Object

Get-Content .\testfile.txt | Where-Object { $_ -match 'weapon_(sks|rpk)' } |
    Group-Object |
        Select-Object Count, Name, @{name='Weapon';expression={$_.name -replace '.+_'}}


This will give the following output

Count Name         Weapon
----- ----           ------
   18 weapon_rpk   rpk
   11 weapon_sks   sks