1

Goal

I'm trying to create a log file in powershell. I'll read it in at the beginning of the script, run the script, then write out the log to the same place. It's a script that will be run everyday so I'll read it in, update it, and then write it back out each day when the script is run

What I've done

Define array

I started by defining the array like this:

# Define today's date
$today = Get-Date -Format "MM/dd/yyyy"

# Define array
$Robjects_log = @(
    [pscustomobject]@{
log_date = $today;
log_covid_cases = "Not Checked";  
log_allspecimens = "Not Checked";  
log_snapshot = "Not Checked";
log_r_script = "No";
log_final_status  = "Not Checked";
log_continue_checking = "Yes"}
    ) 

I can do indexing and changing values like I'd like

# Index and select
$Robjects_log[$_.log_date -eq $today].log_date

# change a value
$Robjects_log[$_.log_date -eq $today].log_date = "tuesday"

# return entire row where log_date is equal to tuesday
$Robjects_log[$_.log_date -eq "tuesday"]

Write out array

I've tried doing this in two ways:

  1. Formatted as table

# Youll have to change this file path!
$Robjects_log | Format-Table -AutoSize | Out-File -FilePath "C:/R_projects/russ_stuff/Robjects_log.txt"
  1. Not formatted as table

# Youll have to change this file path!
$Robjects_log | Out-File -FilePath "C:/R_projects/russ_stuff/Robjects_log.txt"

Read array back in

I've tried reading the array back in a variety of different ways including the following:

$old_log = Get-Content -Path "C:/R_projects/russ_stuff/Robjects_log.txt"

Basically I tried a lot of what was suggested here with both variations of the write step: How to save each line of text file as array through powershell

Where I'm stuck

The log reads in fine and when I type $old_log I see the log, but when I try to index like I did above I get a bunch of errors or blank elements. So the question is how do I write out and read back in an array so that I can keep indexing and changing values in the same way as before I did the write out step?

P.S. I'm super new at power shell. I apologize if this is easy or I missed a place where this has already been answered. Thank you!!

What I've tried

I've tried various combinations of writing out and reading in arrays.

What I haven't tried

redefining the array piece by piece in a loop or resorting to regular expressions. I'm hoping there's an easier way 😅

What I want

I want to be able to read a text file in as an array that I can index and manipulate with standard array functions.

5
  • 4
    Well, you're exporting $Robjects_log as a plain text table representation of the object and then expect PowerShell to know it was a table representation when importing it back, which clearly PowerShell does not know and it is not how data should be exported. Export it as Csv $Robjects_log | Export-Csv thing.csv then import back $csv = Import-Csv thing.csv then you're dealing with objects back not with plain strings. Feb 3, 2023 at 23:28
  • 2
    As an aside, I don't think $Robjects_log[$_.log_date -eq $today] does what you expect it to do, because $Robjects_log[$_.log_date -eq "aaa"] returns the same result set. You probably want $Robjects_log | where-object { $_.log_date -eq $today }...
    – mclayton
    Feb 4, 2023 at 0:28
  • There is a similar question I answered yesterday that may help. See : stackoverflow.com/questions/75332027/…
    – jdweng
    Feb 4, 2023 at 11:31
  • Thank you @SantiagoSquarzon, your solution was easy and solved my problem! Unless you'd like to write it yourself, I'll write your comment out as a full answer for other people who might stumble across this. :)
    – Russ
    Feb 4, 2023 at 20:00
  • @mclayton, Thank you for that aside! I started learning powershell just this week so it's very helpful to know that something was wrong 😅. I'm not sure what SO best practice is, but I'm probably going to edit my original question to use your correct filtering code so nobody comes across this post later and copies my wrong code haha.
    – Russ
    Feb 4, 2023 at 20:03

0

Your Answer

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

Browse other questions tagged or ask your own question.