-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.

grayb3
  • 1
  • 1
  • 1
    Aside #1: [`Invoke-Expression` (`iex`) should generally be avoided](https://stackoverflow.com/a/51252636/45375); definitely [don't use it to invoke an external program or PowerShell script](https://stackoverflow.com/a/57966347/45375). – mklement0 Mar 15 '22 at 01:57
  • 1
    [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/995714) Copy and paste your code here as text – phuclv Mar 15 '22 at 01: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](https://stackoverflow.com/a/50636061/45375) – mklement0 Mar 15 '22 at 01: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_. – mklement0 Mar 15 '22 at 01:59
  • Please also edit your screenshot code into the post itself. Folks here tend to dislike screencaps of code. – codewario Mar 25 '22 at 14:36

1 Answers1

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
santhosh n
  • 36
  • 4