0

If you would like a complete breakdown of the script and the results I have obtained so far, you can reference my previous post.

Import the items from the pipeline in to an array

I am trying to figure out how to get only the two fields I need from the array, so I can manipulate the data. Right now the array shows the entire line as the first element in the array.

$Date = (Get-Date -format "MM-dd-yyyy")
$DateTime = (Get-Date)

$Projects = Import-Csv c:\temp\pm_project.csv
#Show me the entire list of Projects
$Projects
$TS_Entries = Get-Content "c:\temp\timesheet\$Date.txt"
#Show me all the chosen entries in the text file
$TS_Entries
#Show me only the lines in the text file that match the regex statement
$TS_Entries = Select-String -Path "C:\temp\TimeSheet\$Date.txt" -Pattern '(?>FOAH|PRJ)\d{1,10}' -allmatches | Select-Object -expand matches | Select-Object -expand Value
$TS_Entries
$TS_Entries | group -NoElement 

I cannot seem to utilize the count element in the array. I need to get a value of each value in Count and multiply them by 15. The only thing I can reference in the list below is Name

Count Name   
----- ----   
    2 FOAH278
    1 FOAH519
    1 FOAH704
    3 FOAH718
    2 FOAH780
NobleMan
  • 517
  • 1
  • 9
  • 28
  • Try [`Flatten-Object`](https://stackoverflow.com/a/46081131/1701026): $TS_Final | Flatten-Object – iRon Nov 08 '17 at 07:19
  • Could you please give a sample line of text ? This would make it a lot easier to help. – Snak3d0c Nov 08 '17 at 11:26
  • @Snak3d0c I have put a link to my other post that shows the results from the script up until the problem area. https://stackoverflow.com/questions/47148500/import-the-items-from-the-pipeline-in-to-an-array – NobleMan Nov 08 '17 at 12:45
  • @Snak3d0c I have updated the results at the bottom of this post before exporting to the csv file. Hope this helps. – NobleMan Nov 08 '17 at 12:57

2 Answers2

0

So i'm not sure i fully understand what you are asking but here goes:

So say you have a txtfile :

C:\temp\TimeSheet\11-06-2017.txt:1:11/06/2017 18:45:56 - This 15 minutes is dedicated to PROJECT 100 FOAH18
C:\temp\TimeSheet\11-06-2017.txt:2:11/06/2017 18:45:58 - This 15 minutes is dedicated to PROJECT 90 FOAH278  
C:\temp\TimeSheet\11-06-2017.txt:3:11/06/2017 18:45:59 - This 15 minutes is dedicated to PROJECT 80 FOAH313  
C:\temp\TimeSheet\11-06-2017.txt:4:11/06/2017 18:46:00 - This 15 minutes is dedicated to PROJECT 70 PRJ0031905  
C:\temp\TimeSheet\11-06-2017.txt:5:11/06/2017 18:46:02 - This 15 minutes is dedicated to PROJECT 60 PRJ0031909  
C:\temp\TimeSheet\11-06-2017.txt:6:11/06/2017 18:46:03 - This 15 minutes is dedicated to PROJECT 50 PRJ0032045

The following script uses .NET regex class

$test = gc "C:\temp\stack.txt" 
$m = [regex]::Matches($test,'(?msi)((?<=Project )\d{0,}).*?(FOAH|PRJ)(\d{1,10})') 

Example of regex: https://regex101.com/r/yhgl9v/1

foreach($match in $m){
    write-host "ID:" $match.Groups[1].Value 
    write-host "Prefix:" $match.Groups[2].Value 
    write-host "Number:" $match.Groups[3].Value 
    ""
}

This will give you:

ID: 100
Prefix: FOAH
Number: 18

ID: 90
Prefix: FOAH
Number: 278

ID: 80
Prefix: FOAH
Number: 313

ID: 70
Prefix: PRJ
Number: 0031905

ID: 60
Prefix: PRJ
Number: 0031909

ID: 50
Prefix: PRJ
Number: 0032045

Is it this you are looking for?

Snak3d0c
  • 626
  • 4
  • 11
  • Everything is perfect at the moment. I have the two values I need in the last block in this post. I just cannot seem to export that data to a csv file. – NobleMan Nov 08 '17 at 15:01
0
$test = gc "C:\temp\stack.txt" 
$m = [regex]::Matches($test,'(?msi)((?<=Project )\d{0,}).*?(FOAH|PRJ)(\d{1,10})') 
$arr=@()
foreach($match in $m){

   $arr += [PSCustomObject]@{  
    id = $match.Groups[1].Value 
    prefix = $match.Groups[2].Value 
    number = $match.Groups[3].Value 
   }
}

$arr | Export-Csv C:\temp\stock.csv -NoTypeInformation -Delimiter ';'

So you create a customobject (hashtable) that works with a value-index system. Once you've filled up your array, you can later on (outside the loop), export it into a csv-file. Your CSV-file will contain:

"id";"prefix";"number"
"100";"FOAH";"18"
"90";"FOAH";"278"
....
Snak3d0c
  • 626
  • 4
  • 11