5

I am writing a script that will iterate through folders, grabbing substrings of the folder names as variable values, and then iterate through the log files in each of the folders and get some data out of the log files for output to a .csv file. Where I am running into an issue is with the use of Get-ChildItem with variables I have already set. When I run this line by itself, it does not provide any value:

#running this to see the value of $files
$files = Get-ChildItem $_.FullName $folder

$files does not then contain any value.

Here is the entire portion of the script, for reference and context:

#get all folders from the Logs directory    
$folders = Get-ChildItem "C:\Temp\MPOS\Logs"

#iterate through each folder
foreach ($folder in $folders) {

#set substrings of the folder name to variables
$storeNumber = $folder.Name.Substring(2,3)
$date = $folder.Name.Substring(9,7)
#get all files from the current folder being evaluated
$files = Get-ChildItem $_.FullName $folder

#iterate through each file in the current folder
foreach ($file in $files) {

    #set substring of the file name to a variable
    $registerNumber = $file.Name.Substring(12,4)
    #get content of the file
    $logfileContent = Get-Content $file
    #look for all matches of the string "TransactionNumber"
    $transactions = Select-String -InputObject $logfileContent -Pattern "TransactionNumber" -AllMatches
    #count number of matches from above
    $transactionCount = $transactions.Matches.Count

    #below info is creating the object for the .csv
    $transObject = New-Object PSObject

    $transObject | Add-Member -MemberType NoteProperty -Name "StoreNumber" -Value $storeNumber
    $transObject | Add-Member -MemberType NoteProperty -Name "Sales Date" -Value $date
    $transObject | Add-Member -MemberType NoteProperty -Name "RegisterNumber" -Value $registerNumber
    $transObject | Add-Member -MemberType NoteProperty -Name "Transactions" -Value $transactionCount

    $resultsArray += $transObject
    }
}

$resultsArray | Export-Csv C:\Temp\MPOS\MPOSTransactions.csv -NoTypeInformation
5
  • 1
    Get-ChildItem $_.FullName $folder what does this achieve? Commented Dec 21, 2016 at 20:55
  • @4c74356b41 - apparently nothing, there is no value. What I am trying to do is set $files to all of the files that are in $folder. Does that make sense? Similar to how I am setting $folders to all of the folders contained in "C:\Temp\MPOS\Logs". I need to get all of the files from each of the folders in that directory. I hope that makes sense. Then I would want to iterate through each of those files and gather data. I will add some comments to my code to assist. Commented Dec 21, 2016 at 21:00
  • 1
    $_ means "current object from the pipeline." You are using a foreach statement, not a ForEach-Object loop, so $_ doesn't have a value in the context in which you are trying to use it. Commented Dec 21, 2016 at 21:01
  • @Bill_Stewart - thank you, then what is the correct way to go about it please? Commented Dec 21, 2016 at 21:04
  • @Bill_Stewart - got it, figured it out. changed it to read $folder.FullName - thank you!!! Commented Dec 21, 2016 at 21:06

2 Answers 2

3

Edited code below - changed to read $folder.FullName - working now!

$resultsArray = @()
$folders = Get-ChildItem "C:\Temp\MPOS\Logs"

foreach ($folder in $folders) {

$storeNumber = $folder.Name.Substring(2,3)
$date = $folder.Name.Substring(9,7)
$files = Get-ChildItem $folder.FullName

foreach ($file in $files) {

    $registerNumber = $file.Name.Substring(12,4)
    $logfileContent = Get-Content $file.FullName
    $transactions = Select-String -InputObject $logfileContent -Pattern "TransactionNumber" -AllMatches
    $transactionCount = $transactions.Matches.Count

    $transObject = New-Object PSObject

    $transObject | Add-Member -MemberType NoteProperty -Name "StoreNumber" -Value $storeNumber
    $transObject | Add-Member -MemberType NoteProperty -Name "Sales Date" -Value $date
    $transObject | Add-Member -MemberType NoteProperty -Name "RegisterNumber" -Value $registerNumber
    $transObject | Add-Member -MemberType NoteProperty -Name "Transactions" -Value $transactionCount

    $resultsArray += $transObject
    }
}

$resultsArray | Export-Csv C:\Temp\MPOS\MPOSTransactions.csv -NoTypeInformation
Sign up to request clarification or add additional context in comments.

1 Comment

upvote for the variable.attribute piece. That was my problem and I didn't even know it. I needed the attribute.
0

I had this exact issue, but I found out in my case I was trying to load a variable by piping in a value that also was using Format-Table command.

Example: (this worked) $GetMostRecentFile = $GetLatestFile = Get-ChildItem -Force -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $.LastWriteTime.Date -lt (Get-Date).Date -and $.Name -ne 'Thumbs.db' -and $_.Name -ne '.DS_Store'} | Sort LastWriteTime -Descending | Select-Object -First 1 LastWriteTime,FullName,Length

(this didn't work) $GetLatestFile = Get-ChildItem -Force -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $.LastWriteTime.Date -lt (Get-Date).Date -and $.Name -ne 'Thumbs.db' -and $_.Name -ne '.DS_Store'} | Sort LastWriteTime -Descending | Select-Object -First 1 LastWriteTime,FullName,Length | Format-Table -Wrap

Because I'd been using Format-Table as part of my commands to explore this data and build my queries, I forgot that if you pipe this (with FT) to a variable, the variable won't have the details.

In my case above, I needed to pull $GetMostRecentFile.FullName and $GetMostRecentFile.LastWriteTime pulled into another array. But when I was piping with FT, there was not $GetMostRecentFile.FullName. When I remove

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.