-1

I am fairly new to scripting so this may be a little vague or poorly worded. I also cut some stuff out, it is irrelevant, just names, but this is for someone else so in case they don't want it out there better safe than sorry. ANYWAY

I have a script to run a simple test against my system using another .exe that outputs a CSV file of the result when it is finished. I have it using a stream reader to read the output from the CSV file when I run it and erase the CSV it is running from at the end of the test. I am trying to tweak it to create a new file when a test is run that is titled "date-time.txt" and output that specific test into the time-stamped .txt file

Here is what I have so far, I am not sure if it is easier to piggyback off this code or make a separate function.

code to stream read tests and format

Code for tests

I have tried the following code someone suggested:

enter code here $FileName = (Get-Date -Format "yyMMdd-HHmmss") + ".txt" $File = New-Item -Type File -Path "." -Name $FileName $stream_reader = New-Object System.IO.StreamReader{$File.FullName} 

This creates the file but I cannot figure out where I should put it in my code and the proper way to get the content of the audit.csv into the new file and not disrupt the test.

5
  • 1
    Aside #1: Invoke-Expression (iex) should generally be avoided; definitely don't use it to invoke an external program or PowerShell script. Commented Mar 15, 2022 at 1:57
  • 1
    Why not upload images of code/errors when asking a question? Copy and paste your code here as text Commented Mar 15, 2022 at 1:57
  • Aside #2: It's best to pseudo method syntax: Instead of New-Object SomeType(arg1, ...), use New-Object SomeType [-ArgumentList] arg1, ... - PowerShell cmdlets, scripts and functions are invoked like shell commands, not like methods. That is, no parentheses around the argument list, and whitespace-separated arguments (, constructs an array as a single argument, as needed for -ArgumentList). However, method syntax is required if you use the PSv5+ [SomeType]::new() constructor-call method. See this answer Commented Mar 15, 2022 at 1:58
  • Please edit your question to clarify your intent. New-Item creates a new file, whereas a [System.IO.StreamReader] instance is used for reading an existing file. Commented Mar 15, 2022 at 1:59
  • Please also edit your screenshot code into the post itself. Folks here tend to dislike screencaps of code. Commented Mar 25, 2022 at 14:36

1 Answer 1

0

Instead of using IO.Streamreader, There is a easy way to get the csv file content in PowerShell using the Command Import-CSV.

Example: $csvContent=Import-CSV c:\audit.csv 

Same way you can export the content back to other CSV file

Example: $csvContent|Export-Csv c:\audit1.csv 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.