Page 1 of 1

Export-Csv Incorrect function.

Posted: Sun Dec 01, 2019 10:44 pm
by pls-sapien
Hello,
when i run the function below in Studio i get: Incorrect function.
  1. function retention-file ($file)
  2. {
  3.     $currentfile = import-csv -Path $file
  4.     $currentfile | Select-Object -Last 3 | Export-Csv -Path $file -Force -Confirm:$false -NoTypeInformation
  5. }
here is an example of the csv file:
ComputerName,MAC,IP,Date
"PC-01-01","74-D4-25-B3-77-7A","10.0.0.1","02/12/2019"
"PC-00-00","74-D4-25-B3-77-7B","10.0.0.2","02/12/2019"
"PC-11-01","74-D4-25-B3-77-7C","10.0.0.3","02/12/2019"
"PC-01-11","74-D4-25-B3-77-7D","10.0.0.4","02/12/2019"

it works in ISE...
Any help would be great
Thanks,
Sean

Re: Export-Csv Incorrect function.

Posted: Sun Dec 01, 2019 10:52 pm
by jvierra
Please post the exact and full error message.

Re: Export-Csv Incorrect function.

Posted: Sun Dec 01, 2019 10:57 pm
by jvierra
I will also point out that the style you are using is prone to these kinds of errors. If we use the correct open style of bracketing then this will never happen.

Code: Select all


function retention-file ($file){
    $currentfile = import-csv -Path $file
    $currentfile | Select-Object -Last 3 | Export-Csv -Path $file -Force -Confirm:$false -NoTypeInformation
}
retention-file file.csv
Notice the opening brace is on the end of the calling line and not on the next line. This is one of the best reasons to not use the newer next line brace style.